From 8a7874fb1cf6253cf4c57e7401c3f4c73bca67be Mon Sep 17 00:00:00 2001 From: George Zhang Date: Mon, 26 Feb 2024 13:27:38 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Replace=20temporary=20lockfile?= =?UTF-8?q?=20name=20properly=20on=20Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿ› Replace temporary lockfile name properly on Windows * ๐Ÿงช replace_temporary_lockfile #76 --------- Co-authored-by: juftin --- hatch_pip_compile/lock.py | 17 ++++++++++++----- tests/test_lock.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/hatch_pip_compile/lock.py b/hatch_pip_compile/lock.py index 375d9c4..75f3ed9 100644 --- a/hatch_pip_compile/lock.py +++ b/hatch_pip_compile/lock.py @@ -39,11 +39,7 @@ def process_lock(self, lockfile: pathlib.Path) -> None: prefix = dedent(raw_prefix).strip() joined_dependencies = "\n".join([f"# - {dep}" for dep in self.environment.dependencies]) lockfile_text = lockfile.read_text() - cleaned_input_file = re.sub( - rf"-r \S*/{self.environment.name}\.in", - f"hatch.envs.{self.environment.name}", - lockfile_text, - ) + cleaned_input_file = self.replace_temporary_lockfile(lockfile_text=lockfile_text) if self.environment.piptools_constraints_file is not None: lockfile_contents = self.environment.piptools_constraints_file.read_bytes() cross_platform_contents = lockfile_contents.replace(b"\r\n", b"\n") @@ -167,3 +163,14 @@ def read_lock_requirements(self) -> list[Requirement]: session=PipSession(), ) return [ireq.req for ireq in install_requirements] # type: ignore[misc] + + def replace_temporary_lockfile(self, lockfile_text: str) -> str: + """ + Replace the temporary lockfile with the new lockfile + """ + cleaned_input_file = re.sub( + rf"-r \S*[\\/]{self.environment.name}\.in", + f"hatch.envs.{self.environment.name}", + lockfile_text, + ) + return cleaned_input_file diff --git a/tests/test_lock.py b/tests/test_lock.py index 5a1dd49..10d0b93 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 packaging.version import Version @@ -67,3 +68,41 @@ def test_read_lock_requirements(pip_compile: PipCompileFixture) -> None: "ruff==0.1.6", "typing-extensions==4.8.0", } + + +def test_replace_temporary_lockfile_windows(pip_compile: PipCompileFixture) -> None: + """ + Regex Replace Temporary File Path: Windows + """ + lock_raw = r""" + httpx==0.22.0 + # via -r C:\Users\xxx\AppData\Local\Temp\tmp_kn984om\default.in + """ + lock_body = dedent(lock_raw).strip() + cleaned_text = pip_compile.default_environment.piptools_lock.replace_temporary_lockfile( + lock_body + ) + expected_raw = r""" + httpx==0.22.0 + # via hatch.envs.default + """ + assert cleaned_text == dedent(expected_raw).strip() + + +def test_replace_temporary_lockfile_unix(pip_compile: PipCompileFixture) -> None: + """ + Regex Replace Temporary File Path: Unix + """ + lock_raw = r""" + httpx==0.22.0 + # via -r /tmp/tmp_kn984om/default.in + """ + lock_body = dedent(lock_raw).strip() + cleaned_text = pip_compile.default_environment.piptools_lock.replace_temporary_lockfile( + lock_body + ) + expected_raw = r""" + httpx==0.22.0 + # via hatch.envs.default + """ + assert cleaned_text == dedent(expected_raw).strip()