From de19ac896306c412864fe228d42a83a8b54fff24 Mon Sep 17 00:00:00 2001 From: Jeff Edwards Date: Wed, 17 Jul 2024 10:37:43 -0700 Subject: [PATCH 1/2] Updates to account for internal environments that may want pip-compile locking --- hatch_pip_compile/cli.py | 2 +- pyproject.toml | 2 +- requirements.txt | 2 +- requirements/requirements-test.txt | 4 ++-- tests/test_cli.py | 2 +- tests/test_lock.py | 1 + 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 8d99b83..3d122e6 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -135,7 +135,7 @@ def _get_supported_environments(cls) -> set[str]: The name of the environments """ result = subprocess.run( - args=["hatch", "env", "show", "--json"], + args=["hatch", "env", "show", "--json", "--internal"], capture_output=True, check=True, ) diff --git a/pyproject.toml b/pyproject.toml index 6178dc6..067f754 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ "Programming Language :: Python :: Implementation :: PyPy" ] dependencies = [ - "hatch>=1.7.0,<2", + "hatch>=1.10.0,<2", "pip-tools>=6", "click", "rich" diff --git a/requirements.txt b/requirements.txt index 7225a02..0cb19ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ # This file is autogenerated by hatch-pip-compile with Python 3.11 # # - click -# - hatch<2,>=1.7.0 +# - hatch<2,>=1.10.0 # - pip-tools>=6 # - rich # diff --git a/requirements/requirements-test.txt b/requirements/requirements-test.txt index 3fb4777..0b9bc1e 100644 --- a/requirements/requirements-test.txt +++ b/requirements/requirements-test.txt @@ -1,14 +1,14 @@ # # This file is autogenerated by hatch-pip-compile with Python 3.11 # -# [constraints] requirements.txt (SHA256: f944fb6ce9cada73ee126b3fa6f6999e1fb2e71c2fc2f9bc2efb209448582a3f) +# [constraints] requirements.txt (SHA256: d028590864497bfc7d3dd95812728c9e9737aedba6edc137af8f20260b5b19db) # # - pytest # - pytest-cov # - tomlkit # - pytest-xdist # - click -# - hatch<2,>=1.7.0 +# - hatch<2,>=1.10.0 # - pip-tools>=6 # - rich # diff --git a/tests/test_cli.py b/tests/test_cli.py index e2868e2..394d262 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -42,7 +42,7 @@ def test_cli_no_args_mocked(pip_compile: PipCompileFixture, subprocess_run: Mock 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 + args=["hatch", "env", "show", "--json", "--internal"], capture_output=True, check=True ) 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 From c1b90cf6df23e7f8b201509df08bc42f850b5588 Mon Sep 17 00:00:00 2001 From: Jeff Edwards Date: Thu, 18 Jul 2024 10:40:09 -0700 Subject: [PATCH 2/2] Update to make it choose to include internal environments based on hatch version --- hatch_pip_compile/cli.py | 21 ++++++++++++++++++++- pyproject.toml | 2 +- requirements.txt | 2 +- requirements/requirements-test.txt | 4 ++-- tests/test_cli.py | 29 +++++++++++++++++++++++------ 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 3d122e6..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", "--internal"], + 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/pyproject.toml b/pyproject.toml index 067f754..6178dc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ "Programming Language :: Python :: Implementation :: PyPy" ] dependencies = [ - "hatch>=1.10.0,<2", + "hatch>=1.7.0,<2", "pip-tools>=6", "click", "rich" diff --git a/requirements.txt b/requirements.txt index 0cb19ec..7225a02 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ # This file is autogenerated by hatch-pip-compile with Python 3.11 # # - click -# - hatch<2,>=1.10.0 +# - hatch<2,>=1.7.0 # - pip-tools>=6 # - rich # diff --git a/requirements/requirements-test.txt b/requirements/requirements-test.txt index 0b9bc1e..3fb4777 100644 --- a/requirements/requirements-test.txt +++ b/requirements/requirements-test.txt @@ -1,14 +1,14 @@ # # This file is autogenerated by hatch-pip-compile with Python 3.11 # -# [constraints] requirements.txt (SHA256: d028590864497bfc7d3dd95812728c9e9737aedba6edc137af8f20260b5b19db) +# [constraints] requirements.txt (SHA256: f944fb6ce9cada73ee126b3fa6f6999e1fb2e71c2fc2f9bc2efb209448582a3f) # # - pytest # - pytest-cov # - tomlkit # - pytest-xdist # - click -# - hatch<2,>=1.10.0 +# - hatch<2,>=1.7.0 # - pip-tools>=6 # - rich # diff --git a/tests/test_cli.py b/tests/test_cli.py index 394d262..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", "--internal"], 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: