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

Fix logging of cached response #7393

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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/7393.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the logging of cached HTTP response shown as downloading.
13 changes: 6 additions & 7 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,15 @@ def _prepare_download(
else:
url = link.url_without_fragment

redacted_url = redact_auth_from_url(url)
logged_url = redact_auth_from_url(url)

if total_length:
logger.info(
"Downloading %s (%s)", redacted_url, format_size(total_length)
)
elif is_from_cache(resp):
logger.info("Using cached %s", redacted_url)
logged_url = '{} ({})'.format(logged_url, format_size(total_length))

if is_from_cache(resp):
logger.info("Using cached %s", logged_url)
else:
logger.info("Downloading %s", redacted_url)
logger.info("Downloading %s", logged_url)

if logger.getEffectiveLevel() > logging.INFO:
show_progress = False
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_operations_prepare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
import logging
import os
import shutil
import sys
Expand All @@ -16,6 +17,7 @@
Downloader,
_copy_source_tree,
_download_http_url,
_prepare_download,
parse_content_disposition,
sanitize_content_filename,
unpack_file_url,
Expand Down Expand Up @@ -246,6 +248,36 @@ def test_download_http_url__no_directory_traversal(tmpdir):
assert actual == ['out_dir_file']


@pytest.mark.parametrize("url, headers, from_cache, expected", [
('http://example.com/foo.tgz', {}, False,
"Downloading http://example.com/foo.tgz"),
('http://example.com/foo.tgz', {'content-length': 2}, False,
"Downloading http://example.com/foo.tgz (2 bytes)"),
('http://example.com/foo.tgz', {'content-length': 2}, True,
"Using cached http://example.com/foo.tgz (2 bytes)"),
('https://files.pythonhosted.org/foo.tgz', {}, False,
"Downloading foo.tgz"),
('https://files.pythonhosted.org/foo.tgz', {'content-length': 2}, False,
"Downloading foo.tgz (2 bytes)"),
('https://files.pythonhosted.org/foo.tgz', {'content-length': 2}, True,
"Using cached foo.tgz"),
])
def test_prepare_download__log(caplog, url, headers, from_cache, expected):
caplog.set_level(logging.INFO)
resp = MockResponse(b'')
resp.url = url
resp.headers = headers
if from_cache:
resp.from_cache = from_cache
link = Link(url)
_prepare_download(resp, link, progress_bar="on")

assert len(caplog.records) == 1
record = caplog.records[0]
assert record.levelname == 'INFO'
assert expected in record.message


@pytest.fixture
def clean_project(tmpdir_factory, data):
tmpdir = Path(str(tmpdir_factory.mktemp("clean_project")))
Expand Down