Skip to content

Commit cf9295f

Browse files
authored
Log all upload responses with --verbose (#859)
* Log all upload responses with --verbose * Use requests_toolbelt to log request/response * Revert "Use requests_toolbelt to log request/response" This reverts commit b458254. * Log request details * Fix failing tests * Add new test * Add changelog entry
1 parent a60c565 commit cf9295f

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

changelog/859.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Log all upload responses with ``--verbose``.

tests/test_upload.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
def stub_response():
3333
"""Mock successful upload of a package."""
3434
return pretend.stub(
35-
is_redirect=False, status_code=201, raise_for_status=lambda: None
35+
is_redirect=False,
36+
url="https://test.pypi.org/legacy/",
37+
status_code=200,
38+
reason="OK",
39+
text=None,
40+
raise_for_status=lambda: None,
3641
)
3742

3843

@@ -138,6 +143,25 @@ def test_print_packages_if_verbose(upload_settings, capsys):
138143
assert captured.out.count(f"{filename} ({size})") == 1
139144

140145

146+
def test_print_response_if_verbose(upload_settings, stub_response, capsys):
147+
"""Print details about the response from the repostiry."""
148+
upload_settings.verbose = True
149+
150+
result = upload.upload(
151+
upload_settings,
152+
[helpers.WHEEL_FIXTURE, helpers.SDIST_FIXTURE],
153+
)
154+
assert result is None
155+
156+
captured = capsys.readouterr()
157+
response_log = (
158+
f"Response from {stub_response.url}:\n"
159+
f"{stub_response.status_code} {stub_response.reason}"
160+
)
161+
162+
assert captured.out.count(response_log) == 2
163+
164+
141165
def test_success_with_pre_signed_distribution(upload_settings, stub_repository):
142166
"""Add GPG signature provided by user to uploaded package."""
143167
# Upload a pre-signed distribution
@@ -186,7 +210,8 @@ def test_exception_for_http_status(verbose, upload_settings, stub_response, caps
186210

187211
stub_response.is_redirect = False
188212
stub_response.status_code = 403
189-
stub_response.text = "Invalid or non-existent authentication information"
213+
stub_response.reason = "Invalid or non-existent authentication information"
214+
stub_response.text = stub_response.reason
190215
stub_response.raise_for_status = pretend.raiser(requests.HTTPError)
191216

192217
with pytest.raises(requests.HTTPError):
@@ -288,8 +313,11 @@ def test_exception_for_redirect(
288313

289314
stub_response = pretend.stub(
290315
is_redirect=True,
316+
url=redirect_url,
291317
status_code=301,
292318
headers={"location": redirect_url},
319+
reason="Redirect",
320+
text="",
293321
)
294322

295323
stub_repository = pretend.stub(
@@ -323,7 +351,9 @@ def test_prints_skip_message_for_response(
323351
):
324352
upload_settings.skip_existing = True
325353

326-
stub_response.status_code = 409
354+
stub_response.status_code = 400
355+
stub_response.reason = "File already exists"
356+
stub_response.text = stub_response.reason
327357

328358
# Do the upload, triggering the error response
329359
stub_repository.package_is_uploaded = lambda package: False

tests/test_utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,7 @@ def test_check_status_code_for_missing_status_code(
282282

283283
captured = capsys.readouterr()
284284

285-
if verbose:
286-
assert captured.out.count("Content received from server:\nForbidden\n") == 1
287-
else:
288-
assert captured.out.count("--verbose option") == 1
285+
assert captured.out.count("--verbose option") == 0 if verbose else 1
289286

290287

291288
@pytest.mark.parametrize(

twine/commands/upload.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
139139
continue
140140

141141
resp = repository.upload(package)
142+
logger.info(f"Response from {resp.url}:\n{resp.status_code} {resp.reason}")
143+
if resp.text:
144+
logger.info(resp.text)
142145

143146
# Bug 92. If we get a redirect we should abort because something seems
144147
# funky. The behaviour is not well defined and redirects being issued

twine/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ def check_status_code(response: requests.Response, verbose: bool) -> None:
204204
"Retry with the --verbose option for more details."
205205
)
206206

207-
if response.text:
208-
logger.info("Content received from server:\n{}".format(response.text))
209-
210207
raise err
211208

212209

0 commit comments

Comments
 (0)