Skip to content

Commit

Permalink
Relax PEP 508 url validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Mar 21, 2021
1 parent 456f94f commit 72064fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 1 addition & 3 deletions packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,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: TOptional[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 @@ -100,11 +100,14 @@ def test_url_and_marker(self):
assert parsed.url == "http://example.com"
assert str(parsed.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")
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 @@ -118,6 +121,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"]
)
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

0 comments on commit 72064fd

Please sign in to comment.