Skip to content

Commit ea1295b

Browse files
authored
Merge pull request #7965 from deveshks/warn-on-invalid-index-url
Warn if an invalid URL is passed with --index-url
2 parents ea9cb06 + efd6dd2 commit ea1295b

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

news/7430.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Warn if an invalid URL is passed with --index-url

src/pip/_internal/models/search_scope.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,30 @@ def __init__(
7777
def get_formatted_locations(self):
7878
# type: () -> str
7979
lines = []
80+
redacted_index_urls = []
8081
if self.index_urls and self.index_urls != [PyPI.simple_url]:
81-
lines.append(
82-
'Looking in indexes: {}'.format(', '.join(
83-
redact_auth_from_url(url) for url in self.index_urls))
84-
)
82+
for url in self.index_urls:
83+
84+
redacted_index_url = redact_auth_from_url(url)
85+
86+
# Parse the URL
87+
purl = urllib_parse.urlsplit(redacted_index_url)
88+
89+
# URL is generally invalid if scheme and netloc is missing
90+
# there are issues with Python and URL parsing, so this test
91+
# is a bit crude. See bpo-20271, bpo-23505. Python doesn't
92+
# always parse invalid URLs correctly - it should raise
93+
# exceptions for malformed URLs
94+
if not purl.scheme and not purl.netloc:
95+
logger.warning(
96+
'The index url "{}" seems invalid, '
97+
'please provide a scheme.'.format(redacted_index_url))
98+
99+
redacted_index_urls.append(redacted_index_url)
100+
101+
lines.append('Looking in indexes: {}'.format(
102+
', '.join(redacted_index_urls)))
103+
85104
if self.find_links:
86105
lines.append(
87106
'Looking in links: {}'.format(', '.join(

tests/functional/test_install.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,31 @@ def test_ignore_yanked_file(script, data):
17691769
assert 'Successfully installed simple-2.0\n' in result.stdout, str(result)
17701770

17711771

1772+
def test_invalid_index_url_argument(script, shared_data):
1773+
"""
1774+
Test the behaviour of an invalid --index-url argument
1775+
"""
1776+
1777+
result = script.pip('install', '--index-url', '--user',
1778+
shared_data.find_links3, "Dinner",
1779+
expect_error=True)
1780+
1781+
assert 'WARNING: The index url "--user" seems invalid, ' \
1782+
'please provide a scheme.' in result.stderr, str(result)
1783+
1784+
1785+
def test_valid_index_url_argument(script, shared_data):
1786+
"""
1787+
Test the behaviour of an valid --index-url argument
1788+
"""
1789+
1790+
result = script.pip('install', '--index-url',
1791+
shared_data.find_links3,
1792+
"Dinner")
1793+
1794+
assert 'Successfully installed Dinner' in result.stdout, str(result)
1795+
1796+
17721797
def test_install_yanked_file_and_print_warning(script, data):
17731798
"""
17741799
Test install a "yanked" file and print a warning.

0 commit comments

Comments
 (0)