From 7b604fb7eae94de59624e0a2791eb7831e10996c Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Fri, 6 Apr 2018 15:20:26 +0200 Subject: [PATCH 1/5] Improved error message in ParseException --- packaging/requirements.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packaging/requirements.py b/packaging/requirements.py index f87c57cc..31e855cf 100644 --- a/packaging/requirements.py +++ b/packaging/requirements.py @@ -92,9 +92,7 @@ def __init__(self, requirement_string): try: req = REQUIREMENT.parseString(requirement_string) except ParseException as e: - raise InvalidRequirement( - "Invalid requirement, parse error at \"{0!r}\"".format( - requirement_string[e.loc:e.loc + 8])) + raise InvalidRequirement("Parse error: {0!r}".format(e)) self.name = req.name if req.url: From 432c8218de2cd1393b639bf6ac49798336a37681 Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Fri, 6 Apr 2018 15:32:06 +0200 Subject: [PATCH 2/5] Added test --- tests/test_requirements.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_requirements.py b/tests/test_requirements.py index 536e712d..dbd40bb7 100644 --- a/tests/test_requirements.py +++ b/tests/test_requirements.py @@ -170,3 +170,8 @@ def test_sys_platform_linux_in(self): assert req.marker is not None assert req.marker.evaluate(dict(sys_platform="foo")) is True assert req.marker.evaluate(dict(sys_platform="bar")) is False + + def test_parseexception_error_msg(self): + with pytest.raises(InvalidRequirement) as e: + Requirement("toto 42") + assert "Invalid Requiremnt" not in e From d0a9134c73af03babadfc4d01356a71c9c8d0700 Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Fri, 8 Jun 2018 09:55:57 +0200 Subject: [PATCH 3/5] Improved error message + test --- packaging/requirements.py | 4 +++- tests/test_requirements.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packaging/requirements.py b/packaging/requirements.py index 31e855cf..70c10c29 100644 --- a/packaging/requirements.py +++ b/packaging/requirements.py @@ -92,7 +92,9 @@ def __init__(self, requirement_string): try: req = REQUIREMENT.parseString(requirement_string) except ParseException as e: - raise InvalidRequirement("Parse error: {0!r}".format(e)) + raise InvalidRequirement("Parse error at \"{0!r}\": {1}".format( + requirement_string[e.loc:e.loc + 8], e.msg + )) self.name = req.name if req.url: diff --git a/tests/test_requirements.py b/tests/test_requirements.py index dbd40bb7..1b1bbf74 100644 --- a/tests/test_requirements.py +++ b/tests/test_requirements.py @@ -174,4 +174,4 @@ def test_sys_platform_linux_in(self): def test_parseexception_error_msg(self): with pytest.raises(InvalidRequirement) as e: Requirement("toto 42") - assert "Invalid Requiremnt" not in e + assert "Expected stringEnd" in e From 8770b3b93acbd05dab759861ac869b0707cf9ab1 Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Fri, 8 Jun 2018 09:56:13 +0200 Subject: [PATCH 4/5] Improved error on invalid url + test --- packaging/requirements.py | 2 +- tests/test_requirements.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packaging/requirements.py b/packaging/requirements.py index 70c10c29..e8008a6d 100644 --- a/packaging/requirements.py +++ b/packaging/requirements.py @@ -101,7 +101,7 @@ def __init__(self, requirement_string): parsed_url = urlparse.urlparse(req.url) if not (parsed_url.scheme and parsed_url.netloc) or ( not parsed_url.scheme and not parsed_url.netloc): - raise InvalidRequirement("Invalid URL given") + raise InvalidRequirement("Invalid URL: {0}".format(req.url)) self.url = req.url else: self.url = None diff --git a/tests/test_requirements.py b/tests/test_requirements.py index 1b1bbf74..156e0755 100644 --- a/tests/test_requirements.py +++ b/tests/test_requirements.py @@ -97,8 +97,9 @@ def test_url_and_marker(self): assert str(parsed.marker) == 'os_name == "a"' def test_invalid_url(self): - with pytest.raises(InvalidRequirement): + with pytest.raises(InvalidRequirement) as e: Requirement("name @ gopher:/foo/com") + assert "Invalid URL: " in e def test_extras_and_url_and_marker(self): req = Requirement( From 97bad7403ff9b7f6383062807ad0100b34e6ed7a Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Fri, 8 Jun 2018 11:27:38 +0200 Subject: [PATCH 5/5] Fixed indents --- tests/test_requirements.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_requirements.py b/tests/test_requirements.py index 156e0755..cd42aa99 100644 --- a/tests/test_requirements.py +++ b/tests/test_requirements.py @@ -99,7 +99,8 @@ def test_url_and_marker(self): def test_invalid_url(self): with pytest.raises(InvalidRequirement) as e: Requirement("name @ gopher:/foo/com") - assert "Invalid URL: " in e + assert "Invalid URL: " in str(e) + assert "gopher:/foo/com" in str(e) def test_extras_and_url_and_marker(self): req = Requirement( @@ -175,4 +176,4 @@ def test_sys_platform_linux_in(self): def test_parseexception_error_msg(self): with pytest.raises(InvalidRequirement) as e: Requirement("toto 42") - assert "Expected stringEnd" in e + assert "Expected stringEnd" in str(e)