locate_test_file

Locate the test file path for a given Python source file.

This utility calculates where a test file should be placed for a given Python source file, following the Python test strategy naming convention:

Source: git-repo/<package_name>/<subpackage>/<module>.py Test: tests/<subpackage>/test_<subpackage>_<module>.py

Given an absolute path to a source file, this script:

  1. Finds the project root (by locating pyproject.toml)

  2. Determines the relative path from project root to source file

  3. Calculates the correct test file path using naming convention

  4. Prints the absolute test file path

This is useful for:

  • IDE integrations that need to jump from source to test file

  • Build tools that generate test files in the correct location

  • Pre-commit hooks that validate tests exist for changed source files

  • Development workflows that automate test file creation

shai_py.subcmd.locate_test_file.calculate_test_file_path(source_file_path: Path, project_root: Path) Path[source]

Calculate the test file path for a given source file.

Applies the naming convention:

tests/<subpackage>/test_<subpackage>_<module>.py

For example:

Source: my_package/math/operations/calculator.py Test: tests/math/operations/test_math_operations_calculator.py

Parameters:
  • source_file_path – Absolute path to the source file.

  • project_root – Absolute path to the project root.

Returns:

Absolute path where the test file should be located.

Raises:

ValueError – If source file is not within the project.

shai_py.subcmd.locate_test_file.main(source_file: str) str[source]

Get the test file path for a Python source file.

Maps source to test by stripping package name and joining path parts: my_pkg/a/b/c.py -> tests/a/b/test_a_b_c.py

Usage:

shai-py test-path --source-file /path/to/my_pkg/a/b/c.py
Parameters:

source_file – Absolute path to the Python source file.