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

Improved error message in ParseException #129

Merged
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
8 changes: 4 additions & 4 deletions packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ 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 at \"{0!r}\": {1}".format(
requirement_string[e.loc:e.loc + 8], e.msg
))

self.name = req.name
if req.url:
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
Expand Down
9 changes: 8 additions & 1 deletion tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ 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 str(e)
assert "gopher:/foo/com" in str(e)

def test_extras_and_url_and_marker(self):
req = Requirement(
Expand Down Expand Up @@ -170,3 +172,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 "Expected stringEnd" in str(e)