Skip to content

Commit

Permalink
🐛 Replace temporary lockfile name properly on Windows
Browse files Browse the repository at this point in the history
* 🐛 Replace temporary lockfile name properly on Windows

* 🧪 replace_temporary_lockfile

#76

---------

Co-authored-by: juftin <juftin@juftin.com>
  • Loading branch information
GeeTransit and juftin authored Feb 26, 2024
1 parent 5590128 commit 8a7874f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
17 changes: 12 additions & 5 deletions hatch_pip_compile/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
39 changes: 39 additions & 0 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Testing the `lock` module
"""
from textwrap import dedent

from packaging.requirements import Requirement
from packaging.version import Version
Expand Down Expand Up @@ -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()

0 comments on commit 8a7874f

Please sign in to comment.