import pathlib import pytest import click class MockMain: __slots__ = "__package__" def __init__(self, package_name): self.__package__ = package_name @pytest.mark.parametrize( ("path", "main", "expected"), [ ("example.py", None, "example.py"), (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"), ("example", None, "example"), (str(pathlib.Path("example/__main__.py")), "example", "python -m example"), (str(pathlib.Path("example/cli.py")), "example", "python -m example.cli"), (str(pathlib.Path("./example")), "", "example"), ], ) def test_detect_program_name(path, main, expected): assert click.utils._detect_program_name(path, _main=MockMain(main)) == expected