diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 8d99b83..651e598 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -134,16 +134,35 @@ def _get_supported_environments(cls) -> set[str]: List[str] The name of the environments """ + + show_command = ["hatch", "env", "show", "--json"] + + if cls._get_hatch_version() >= (1, 10): + # Versions greater than 1.10 have built in internal environments + show_command.append("--internal") + result = subprocess.run( - args=["hatch", "env", "show", "--json"], + args=show_command, capture_output=True, check=True, ) + environment_dict: dict[str, Any] = json.loads(result.stdout) return { key for key, value in environment_dict.items() if value.get("type") == "pip-compile" } + @staticmethod + def _get_hatch_version() -> tuple[int, ...]: + result = subprocess.run( + args=["hatch", "--version"], + capture_output=True, + check=True, + ) + # Version is the last portion of the string + version_string = result.stdout.strip().split()[-1] + return tuple([int(p) for p in version_string.split(b".") if p.isdigit()]) + @click.command("hatch-pip-compile") @click.version_option(version=__version__, prog_name=__application__) diff --git a/tests/test_cli.py b/tests/test_cli.py index e2868e2..009a787 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -32,18 +32,35 @@ def test_cli_version() -> None: assert f"hatch-pip-compile, version {__version__}" in result.output -def test_cli_no_args_mocked(pip_compile: PipCompileFixture, subprocess_run: Mock) -> None: +@pytest.mark.parametrize( + "hatch_version, expected_args", + [ + (b"Hatch, version 1.7.0", ["hatch", "env", "show", "--json"]), + (b"Hatch, version 1.12.0", ["hatch", "env", "show", "--json", "--internal"]), + ], +) +def test_cli_no_args_mocked( + hatch_version: bytes, + expected_args: list[str], + pip_compile: PipCompileFixture, + subprocess_run: Mock, +) -> None: """ Test the CLI with no arguments - mock the result """ runner = CliRunner() with runner.isolated_filesystem(temp_dir=pip_compile.isolation): + version_mock = Mock() + version_mock.stdout = hatch_version + environment_mock = Mock() + environment_mock.stdout = b"{}" + subprocess_run.side_effect = [ + version_mock, + environment_mock, + ] _ = runner.invoke(cli=cli) - assert subprocess_run.call_count == 1 - subprocess_run.assert_called_once() - subprocess_run.assert_called_with( - args=["hatch", "env", "show", "--json"], capture_output=True, check=True - ) + assert subprocess_run.call_count == 2 + subprocess_run.assert_called_with(args=expected_args, capture_output=True, check=True) def test_cli_no_args(pip_compile: PipCompileFixture) -> None: diff --git a/tests/test_lock.py b/tests/test_lock.py index 10d0b93..29d2a0d 100644 --- a/tests/test_lock.py +++ b/tests/test_lock.py @@ -1,6 +1,7 @@ """ Testing the `lock` module """ + from textwrap import dedent from packaging.requirements import Requirement