From 5de40c83a24cf60341782a65c46c295d17f48bc8 Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 18 Apr 2019 09:18:38 -0700 Subject: [PATCH 1/3] Fix NameError when handling InvalidRequirement in install_req_from_req_string Previously, an InvalidRequirement would raise a NameError while trying to raise an InstallationError because `req` was not defined. Discovered while working on #6402. --- src/pip/_internal/req/constructors.py | 2 +- tests/unit/test_req_install.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/req/constructors.py b/src/pip/_internal/req/constructors.py index 546d14b667d..0f18b6a727d 100644 --- a/src/pip/_internal/req/constructors.py +++ b/src/pip/_internal/req/constructors.py @@ -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, diff --git a/tests/unit/test_req_install.py b/tests/unit/test_req_install.py index 95ce4cf4c5c..196bb0343aa 100644 --- a/tests/unit/test_req_install.py +++ b/tests/unit/test_req_install.py @@ -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, ) @@ -49,6 +50,14 @@ 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): + install_req_from_req_string("http:/this/is/invalid") + def test_install_req_from_string_without_comes_from(self): """ Test to make sure that install_req_from_string succeeds From 3a402847fc24bbb15bc7d67d51b61b55de2ae6d0 Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 18 Apr 2019 09:28:54 -0700 Subject: [PATCH 2/3] Add news file --- news/6419.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/6419.bugfix diff --git a/news/6419.bugfix b/news/6419.bugfix new file mode 100644 index 00000000000..83e539bdd81 --- /dev/null +++ b/news/6419.bugfix @@ -0,0 +1 @@ +Fix NameError when handling InvalidRequirement in install_req_from_req_string. From c6036b758e8b01c6ac1c67bcdd5de1c8fef5dc38 Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 18 Apr 2019 23:26:43 -0700 Subject: [PATCH 3/3] PR feedback --- news/6419.bugfix | 2 +- tests/unit/test_req_install.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/news/6419.bugfix b/news/6419.bugfix index 83e539bdd81..057b81d178c 100644 --- a/news/6419.bugfix +++ b/news/6419.bugfix @@ -1 +1 @@ -Fix NameError when handling InvalidRequirement in install_req_from_req_string. +Fix ``NameError`` when handling an invalid requirement. diff --git a/tests/unit/test_req_install.py b/tests/unit/test_req_install.py index 196bb0343aa..66a84e1ba2a 100644 --- a/tests/unit/test_req_install.py +++ b/tests/unit/test_req_install.py @@ -55,9 +55,13 @@ 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): + 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