Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of URL requirements with git+file scheme #264

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 1 addition & 3 deletions packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def __init__(self, requirement_string: str) -> None:
if parsed_url.scheme == "file":
if urllib.parse.urlunparse(parsed_url) != req.url:
raise InvalidRequirement("Invalid URL given")
elif not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc
):
elif not parsed_url.scheme:
raise InvalidRequirement(f"Invalid URL: {req.url}")
self.url: Optional[str] = req.url
else:
Expand Down
22 changes: 19 additions & 3 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ def test_url_and_marker(self):
req, "test", "http://example.com", extras=[], marker='os_name == "a"'
)

def test_invalid_url(self):
@pytest.mark.parametrize(
"invalid_url", ["/foo/bar", "/foo", "./foo/bar", "./foo", "foo"]
)
def test_invalid_url(self, invalid_url):
with pytest.raises(InvalidRequirement) as e:
Requirement("name @ gopher:/foo/com")
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
Requirement("name @ " + invalid_url)
assert "Invalid URL: " in str(e.value)
assert "gopher:/foo/com" in str(e.value)
assert invalid_url in str(e.value)

def test_file_url(self):
req = Requirement("name @ file:///absolute/path")
Expand All @@ -160,6 +163,19 @@ def test_invalid_file_urls(self):
with pytest.raises(InvalidRequirement):
Requirement("name @ file:/.")

def test_vcs_url(self):
req = Requirement("name @ git+https://g.c/u/r.git")
self._assert_requirement(req, "name", "git+https://g.c/u/r.git")
req = Requirement("name @ git+file:///data/repo")
self._assert_requirement(req, "name", "git+file:///data/repo")

@pytest.mark.parametrize(
"url", ["gopher:/foo/com", "mailto:me@example.com", "c:/foo/bar"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that here, c is an URL scheme, not a drive letter.

)
def test_exotic_urls(self, url):
req = Requirement("name @ " + url)
self._assert_requirement(req, "name", url)

def test_extras_and_url_and_marker(self):
req = Requirement("name [fred,bar] @ http://foo.com ; python_version=='2.7'")
self._assert_requirement(
Expand Down