Skip to content

Commit

Permalink
Fix NameError when handling InvalidRequirement in install_req_from_re…
Browse files Browse the repository at this point in the history
…q_string (#6419)

Previously, an InvalidRequirement would raise a NameError while trying
to raise an InstallationError because `req` was not defined.

Discovered while working on #6402.
  • Loading branch information
rouge8 authored and cjerdonek committed Apr 19, 2019
1 parent c8e9caa commit bb14ff4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/6419.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``NameError`` when handling an invalid requirement.
2 changes: 1 addition & 1 deletion src/pip/_internal/req/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def install_req_from_req_string(
try:
req = Requirement(req_string)
except InvalidRequirement:
raise InstallationError("Invalid requirement: '%s'" % req)
raise InstallationError("Invalid requirement: '%s'" % req_string)

domains_not_allowed = [
PyPI.file_storage_domain,
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from pip._vendor.packaging.requirements import Requirement

from pip._internal.exceptions import InstallationError
from pip._internal.req.constructors import (
install_req_from_line, install_req_from_req_string,
)
Expand Down Expand Up @@ -49,6 +50,18 @@ def test_forward_slash_results_in_a_link(self, tmpdir):

class TestInstallRequirementFrom(object):

def test_install_req_from_string_invalid_requirement(self):
"""
Requirement strings that cannot be parsed by
packaging.requirements.Requirement raise an InstallationError.
"""
with pytest.raises(InstallationError) as excinfo:
install_req_from_req_string("http:/this/is/invalid")

assert str(excinfo.value) == (
"Invalid requirement: 'http:/this/is/invalid'"
)

def test_install_req_from_string_without_comes_from(self):
"""
Test to make sure that install_req_from_string succeeds
Expand Down

0 comments on commit bb14ff4

Please sign in to comment.