From 61f5f80e66ab8926f50cdd252f4ed1bc625f5490 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:16:15 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20get=20envs=20in=20cli=20with?= =?UTF-8?q?=20hatch=20core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ...instead of using subprocess.run() to run a hatch CLI command, which can be tricky and error prone to extract the output from --- hatch_pip_compile/cli.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 3ddffbb..15f35ee 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -9,12 +9,13 @@ import os import subprocess from typing import Any, Sequence +from pathlib import Path import click import rich.traceback from hatch_pip_compile.__about__ import __application__, __version__ - +from hatch.project.core import Project @dataclasses.dataclass class HatchCommandRunner: @@ -134,12 +135,10 @@ def _get_supported_environments(cls) -> set[str]: List[str] The name of the environments """ - result = subprocess.run( - args=["hatch", "env", "show", "--json"], - capture_output=True, - check=True, + project = Project( + Path.cwd(), ) - environment_dict: dict[str, Any] = json.loads(result.stdout) + environment_dict = project.config.envs.keys() return { key for key, value in environment_dict.items() if value.get("type") == "pip-compile" } From 9d0d8374a8b810ce021a3b0daa30bdd04fc486c6 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:44:35 +0200 Subject: [PATCH 2/5] fix: rm .keys() call + lint errors --- hatch_pip_compile/cli.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 15f35ee..6eacf96 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -5,11 +5,10 @@ from __future__ import annotations import dataclasses -import json import os +from pathlib import Path import subprocess from typing import Any, Sequence -from pathlib import Path import click import rich.traceback @@ -138,7 +137,7 @@ def _get_supported_environments(cls) -> set[str]: project = Project( Path.cwd(), ) - environment_dict = project.config.envs.keys() + environment_dict = project.config.envs return { key for key, value in environment_dict.items() if value.get("type") == "pip-compile" } From 09f6666a746641eb7c9e79d95f2d13641d9a5a6d Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:49:14 +0200 Subject: [PATCH 3/5] chore: import order --- hatch_pip_compile/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index 6eacf96..dfabe83 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -6,15 +6,15 @@ import dataclasses import os -from pathlib import Path import subprocess +from pathlib import Path from typing import Any, Sequence import click import rich.traceback +from hatch.project.core import Project from hatch_pip_compile.__about__ import __application__, __version__ -from hatch.project.core import Project @dataclasses.dataclass class HatchCommandRunner: From 0ecc5fdbbd024dbcb2b396bf62acd6767da0028d Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:54:12 +0200 Subject: [PATCH 4/5] chore: missing line break --- hatch_pip_compile/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hatch_pip_compile/cli.py b/hatch_pip_compile/cli.py index dfabe83..e5ea7ce 100644 --- a/hatch_pip_compile/cli.py +++ b/hatch_pip_compile/cli.py @@ -16,6 +16,7 @@ from hatch_pip_compile.__about__ import __application__, __version__ + @dataclasses.dataclass class HatchCommandRunner: """ From f8ddd4fa3856628ae64fbebbdb333d0b170454cb Mon Sep 17 00:00:00 2001 From: Valentin Oliver Melberg Loftsson <23453691+valentinoli@users.noreply.github.com> Date: Wed, 23 Jul 2025 23:14:45 +0200 Subject: [PATCH 5/5] test: adjust test case --- tests/test_cli.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index e2868e2..21e5d3e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -39,10 +39,19 @@ def test_cli_no_args_mocked(pip_compile: PipCompileFixture, subprocess_run: Mock runner = CliRunner() with runner.isolated_filesystem(temp_dir=pip_compile.isolation): _ = 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 + subprocess_run.assert_called_once_with( + args=[ + "hatch", + "env", + "run", + "--env", + "default", + "--", + "python", + "--version", + ], + capture_output=True, + check=False, )