Skip to content

Commit

Permalink
Add a unit test to test the absolute path change
Browse files Browse the repository at this point in the history
  • Loading branch information
hellozee committed Jul 27, 2024
1 parent af28bcf commit 7072473
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/pip/_internal/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ def parse(
self, filename: str, constraint: bool
) -> Generator[ParsedLine, None, None]:
"""Parse a given file, yielding parsed lines."""
filename = os.path.abspath(filename)
self._parsed_files[filename] = None # The primary requirements file passed
self._parsed_files[os.path.abspath(filename)] = (
None # The primary requirements file passed
)
yield from self._parse_and_recurse(filename, constraint)

def _parse_and_recurse(
Expand Down
44 changes: 39 additions & 5 deletions tests/unit/test_req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest

import pip._internal.exceptions
import pip._internal.req.req_file # this will be monkeypatched
from pip._internal.exceptions import InstallationError, RequirementsFileParseError
from pip._internal.index.package_finder import PackageFinder
Expand Down Expand Up @@ -358,21 +359,54 @@ def test_recursive_requirements_file(
# When the passed requirements file recursively references itself
with pytest.raises(
RecursionError,
match=f"{req_files[0]} recursively references itself"
f" in {req_files[req_file_count - 1]}",
match=(
f"{req_files[0]} recursively references itself"
f" in {req_files[req_file_count - 1]}"
),
):
list(parse_requirements(filename=str(req_files[0]), session=session))

# When one of other the requirements file recursively references itself
req_files[req_file_count - 1].write_text(f"-r {req_files[req_file_count - 2]}")
with pytest.raises(
RecursionError,
match=f"{req_files[req_file_count - 2]} recursively references itself "
f"in {req_files[req_file_count - 1]} and again in"
f" {req_files[req_file_count - 3]}",
match=(
f"{req_files[req_file_count - 2]} recursively references itself "
f"in {req_files[req_file_count - 1]} and again in"
f" {req_files[req_file_count - 3]}"
),
):
list(parse_requirements(filename=str(req_files[0]), session=session))

def test_recursive_relative_requirements_file(
self, monkeypatch: pytest.MonkeyPatch, tmpdir: Path, session: PipSession
) -> None:
root_req_file = tmpdir / "root.txt"
(tmpdir / "nest" / "nest").mkdir(parents=True)
level_1_req_file = tmpdir / "nest" / "level_1.txt"
level_2_req_file = tmpdir / "nest" / "nest" / "level_2.txt"

root_req_file.write_text("-r nest/level_1.txt")
level_1_req_file.write_text("-r nest/level_2.txt")
level_2_req_file.write_text("-r ../../root.txt")

with pytest.raises(
RecursionError,
match=(
f"{root_req_file} recursively references itself in {level_2_req_file}"
),
):
list(parse_requirements(filename=str(root_req_file), session=session))

# If we don't use absolute path, it keeps on chaining the filename resulting in
# a huge filename, since a != a/b/c/../../
monkeypatch.setattr(os.path, "abspath", lambda x: x)
with pytest.raises(
pip._internal.exceptions.InstallationError,
match=r"Could not open requirements file: \[Errno 36\] File name too long:",
):
list(parse_requirements(filename=str(root_req_file), session=session))

def test_options_on_a_requirement_line(self, line_processor: LineProcessor) -> None:
line = (
'SomeProject --global-option="yo3" --global-option "yo4" '
Expand Down

0 comments on commit 7072473

Please sign in to comment.