Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3850.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resolve nested requirement paths relative to remote requirements file URLs.
9 changes: 8 additions & 1 deletion src/pdm/formats/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ def _parse_line(self, filename: str, line: str) -> None:
if args.editable:
self.requirements.append(parse_requirement(" ".join(args.editable), True))
if args.requirement:
referenced_requirements = Path(filename).parent.joinpath(args.requirement).as_posix()
reference = urllib.parse.urlparse(args.requirement)
source = urllib.parse.urlparse(filename)
if reference.scheme in ("http", "https", "file"):
referenced_requirements = args.requirement
elif source.scheme in ("http", "https", "file"):
referenced_requirements = urllib.parse.urljoin(filename, args.requirement)
else:
referenced_requirements = Path(filename).parent.joinpath(args.requirement).as_posix()
self.parse_file(referenced_requirements)

def parse_lines(self, lines: Iterable[str], filename: str = "<temp file>") -> None:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ def test_convert_requirements_file_with_tab_before_comment(project):
assert result["dependencies"] == ["webassets==2.0"]


@pytest.mark.parametrize(
"reference,expected_url",
[
("child.txt", "https://example.com/base/child.txt"),
("../child.txt", "https://example.com/child.txt"),
("https://other.example/child.txt", "https://other.example/child.txt"),
],
)
def test_remote_requirements_resolve_nested_reference(mocker, reference, expected_url):
session = mocker.Mock()
session.get.side_effect = [
mocker.Mock(is_error=False, text=f"-r {reference}"),
mocker.Mock(is_error=False, text="webassets==2.0"),
]
parser = requirements.RequirementParser(session)

parser.parse_file("https://example.com/base/requirements.txt")

assert [call.args[0] for call in session.get.call_args_list] == [
"https://example.com/base/requirements.txt",
expected_url,
]
assert [req.as_line() for req in parser.requirements] == ["webassets==2.0"]


def test_convert_requirements_file_without_name(project, vcs):
req_file = project.root.joinpath("reqs.txt")
project.root.joinpath("reqs.txt").write_text("git+https://github.com/test-root/demo.git\n")
Expand Down
Loading