Fix NameError when handling InvalidRequirement in install_req_from_req_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.
This commit is contained in:
Andy Freeland 2019-04-19 02:37:33 -07:00 committed by Chris Jerdonek
parent c8e9caa8a7
commit bb14ff42b0
3 changed files with 15 additions and 1 deletions

1
news/6419.bugfix Normal file
View File

@ -0,0 +1 @@
Fix ``NameError`` when handling an invalid requirement.

View File

@ -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,

View File

@ -4,6 +4,7 @@ import tempfile
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,
)
@ -49,6 +50,18 @@ class TestInstallRequirementBuildDirectory(object):
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