diff --git a/changelog.d/530.bugfix.rst b/changelog.d/530.bugfix.rst new file mode 100644 index 00000000..9aff9d1a --- /dev/null +++ b/changelog.d/530.bugfix.rst @@ -0,0 +1,2 @@ +Running ``semver`` directly from a wheel with ``python`` now displays the CLI help +instead of failing with ``ModuleNotFoundError``. diff --git a/src/semver/__main__.py b/src/semver/__main__.py index c6adb834..edc648da 100644 --- a/src/semver/__main__.py +++ b/src/semver/__main__.py @@ -14,14 +14,14 @@ import sys from typing import List, Optional -from semver import cli - def main(cliargs: Optional[List[str]] = None) -> int: if __package__ == "": path = os.path.dirname(os.path.dirname(__file__)) sys.path[0:0] = [path] + from semver import cli + return cli.main(cliargs) diff --git a/tests/test_pysemver-cli.py b/tests/test_pysemver-cli.py index 9933b0dd..2f59adad 100644 --- a/tests/test_pysemver-cli.py +++ b/tests/test_pysemver-cli.py @@ -1,17 +1,20 @@ +import subprocess +import sys from argparse import Namespace from contextlib import contextmanager +from pathlib import Path from unittest.mock import patch import pytest from semver import ( + __main__, cmd_bump, cmd_check, cmd_compare, cmd_nextver, createparser, main, - __main__, ) @@ -138,12 +141,25 @@ def test_should_process_check_iscalled_with_valid_version(capsys): @pytest.mark.parametrize("package_name", ["", "semver"]) def test_main_file_should_call_cli_main(package_name): - with patch("semver.__main__.cli.main") as mocked_main: + with patch("semver.cli.main") as mocked_main: with patch("semver.__main__.__package__", package_name): __main__.main() mocked_main.assert_called_once() +def test_main_file_should_run_package_directly(): + package_dir = Path(__main__.__file__).parent + + result = subprocess.run( + [sys.executable, "-I", "-S", str(package_dir), "--help"], + capture_output=True, + text=True, + ) + + assert result.returncode == 0, result.stderr + assert result.stdout.startswith("usage: semver") + + @pytest.mark.parametrize( "cliargs", [