#!/usr/bin/python3

"""Complete an argument list matching sources with their tests."""

import argparse
from pathlib import Path


def source_from_test(path: Path) -> Path:
    """Return a source from its test path."""
    match path.parent.name:
        case "tests":
            return path.parent.parent / path.name.removeprefix("test_")
        case "templatetags_tests":
            return (
                path.parent.parent
                / "templatetags"
                / path.name.removeprefix("test_")
            )
        case _:
            raise ValueError(f"Unrecognized source: {path}")


def test_from_source(path: Path) -> Path:
    """Return a test path from a non-test source."""
    match path.parent.name:
        case "templatetags":
            return (
                path.parent.parent / "templatetags_tests" / f"test_{path.name}"
            )
        case _:
            return path.parent / "tests" / f"test_{path.name}"


def main() -> None:
    """Script entry point."""
    parser = argparse.ArgumentParser(
        description="Add related test files to a source list."
    )
    parser.add_argument(
        "sources", nargs="+", type=Path, help="Source file names"
    )
    args = parser.parse_args()

    for source in args.sources:
        if source.name.startswith("test_"):
            print(source_from_test(source))
            print(source)
        else:
            print(source)
            print(test_from_source(source))


if __name__ == '__main__':
    main()
