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

Warn if package url is a vcs or an archive url with invalid scheme #8465

Merged
merged 5 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions news/8128.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warn if package url is a vcs or an archive url with invalid scheme
5 changes: 3 additions & 2 deletions src/pip/_internal/index/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ def _get_html_page(link, session=None):
# Check for VCS schemes that do not support lookup as web pages.
vcs_scheme = _match_vcs_scheme(url)
if vcs_scheme:
logger.debug('Cannot look at %s URL %s', vcs_scheme, link)
logger.warning('Cannot look at %s URL %s because it does not support '
'lookup as web pages.', vcs_scheme, link)
return None

# Tack index.html onto file:// URLs that point to directories
Expand All @@ -450,7 +451,7 @@ def _get_html_page(link, session=None):
try:
resp = _get_html_response(url, session=session)
except _NotHTTP:
logger.debug(
logger.warning(
'Skipping page %s because it looks like an archive, and cannot '
'be checked by HEAD.', link,
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
)
Expand Down
32 changes: 29 additions & 3 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ def test_get_html_response_archive_to_http_scheme(url, content_type):
assert ctx.value.args == (content_type, "HEAD")


@pytest.mark.parametrize(
"url",
[
("ftp://python.org/python-3.7.1.zip"),
("file:///opt/data/pip-18.0.tar.gz"),
],
)
def test_get_html_page_invalid_content_type_archive(caplog, url):
"""`_get_html_page()` should warn if an archive URL is not HTML
and therefore cannot be used for a HEAD request.
"""
caplog.set_level(logging.WARNING)
link = Link(url)

session = mock.Mock(PipSession)

assert _get_html_page(link, session=session) is None
assert ('pip._internal.index.collector',
logging.WARNING,
'Skipping page {} because it looks like an archive, and cannot '
'be checked by HEAD.'.format(
url)) \
in caplog.record_tuples


@pytest.mark.parametrize(
"url",
[
Expand Down Expand Up @@ -463,15 +488,16 @@ def test_get_html_page_invalid_scheme(caplog, url, vcs_scheme):

Only file:, http:, https:, and ftp: are allowed.
"""
with caplog.at_level(logging.DEBUG):
with caplog.at_level(logging.WARNING):
page = _get_html_page(Link(url), session=mock.Mock(PipSession))

assert page is None
assert caplog.record_tuples == [
(
"pip._internal.index.collector",
logging.DEBUG,
"Cannot look at {} URL {}".format(vcs_scheme, url),
logging.WARNING,
"Cannot look at {} URL {} because it does not support "
"lookup as web pages.".format(vcs_scheme, url),
),
]

Expand Down