From abaffa8c3dda0f390fc3a463daecfc4ee7953785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 27 Jan 2020 20:03:33 +0100 Subject: [PATCH] Relax PEP 508 url validation. --- packaging/requirements.py | 4 +--- tests/test_requirements.py | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packaging/requirements.py b/packaging/requirements.py index 971fb8fb..dbc9f46f 100644 --- a/packaging/requirements.py +++ b/packaging/requirements.py @@ -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: diff --git a/tests/test_requirements.py b/tests/test_requirements.py index 3360b5ed..dbdb57d7 100644 --- a/tests/test_requirements.py +++ b/tests/test_requirements.py @@ -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") + 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") @@ -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"] + ) + 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(