Skip to content

Commit 360eb5a

Browse files
committed
Move check_status_code back to utils; move tests
Signed-off-by: Aditya Saky <[email protected]>
1 parent 98c0d30 commit 360eb5a

File tree

4 files changed

+55
-55
lines changed

4 files changed

+55
-55
lines changed

tests/test_upload.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,7 @@ def test_check_status_code_for_wrong_repo_url(repo_url, make_settings):
298298
# override defaults to use incorrect URL
299299
upload_settings.repository_config['repository'] = repo_url
300300

301-
with pytest.raises(exceptions.InvalidPyPIUploadURL):
301+
with pytest.raises(twine.exceptions.InvalidPyPIUploadURL):
302302
upload.upload(upload_settings, [
303303
WHEEL_FIXTURE, SDIST_FIXTURE, NEW_SDIST_FIXTURE, NEW_WHEEL_FIXTURE
304304
])
305-
306-
307-
@pytest.mark.parametrize('repo_url', [
308-
"https://pypi.python.org",
309-
"https://testpypi.python.org"
310-
])
311-
def test_check_status_code_for_deprecated_pypi_url(repo_url):
312-
response = pretend.stub(
313-
status_code=410,
314-
url=repo_url
315-
)
316-
317-
# value of Verbose doesn't matter for this check
318-
with pytest.raises(exceptions.UploadToDeprecatedPyPIDetected):
319-
upload.check_status_code(response, False)

tests/test_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import textwrap
1717

1818
import pytest
19+
import pretend
1920

20-
from twine import utils
21+
from twine import utils, exceptions
2122

2223
import helpers
2324

@@ -389,3 +390,18 @@ def t(foo=False):
389390
t(1)
390391

391392
assert t(foo=True)
393+
394+
395+
@pytest.mark.parametrize('repo_url', [
396+
"https://pypi.python.org",
397+
"https://testpypi.python.org"
398+
])
399+
def test_check_status_code_for_deprecated_pypi_url(repo_url):
400+
response = pretend.stub(
401+
status_code=410,
402+
url=repo_url
403+
)
404+
405+
# value of Verbose doesn't matter for this check
406+
with pytest.raises(exceptions.UploadToDeprecatedPyPIDetected):
407+
utils.check_status_code(response, False)

twine/commands/upload.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from twine import settings
2121
from twine import utils
2222

23-
import requests
24-
2523

2624
def skip_upload(response, skip_existing, package):
2725
filename = package.basefilename
@@ -50,41 +48,6 @@ def skip_upload(response, skip_existing, package):
5048
(response.status_code == 403 and msg_403 in response.text)))
5149

5250

53-
def check_status_code(response, verbose):
54-
"""Generate a helpful message based on the response from the repository.
55-
56-
Raise a custom exception for recognized errors. Otherwise, print the
57-
response content (based on the verbose option) before re-raising the
58-
HTTPError.
59-
"""
60-
if response.status_code == 410 and "pypi.python.org" in response.url:
61-
raise exceptions.UploadToDeprecatedPyPIDetected(
62-
f"It appears you're uploading to pypi.python.org (or "
63-
f"testpypi.python.org). You've received a 410 error response. "
64-
f"Uploading to those sites is deprecated. The new sites are "
65-
f"pypi.org and test.pypi.org. Try using {utils.DEFAULT_REPOSITORY}"
66-
f" (or {utils.TEST_REPOSITORY}) to upload your packages instead. "
67-
f"These are the default URLs for Twine now. More at "
68-
f"https://packaging.python.org/guides/migrating-to-pypi-org/.")
69-
elif response.status_code == 405 and "pypi.org" in response.url:
70-
raise exceptions.InvalidPyPIUploadURL(
71-
f"It appears you're trying to upload to pypi.org but have an "
72-
f"invalid URL. You probably want one of these two URLs: "
73-
f"{utils.DEFAULT_REPOSITORY} or {utils.TEST_REPOSITORY}. Check "
74-
f"your --repository-url value.")
75-
76-
try:
77-
response.raise_for_status()
78-
except requests.HTTPError as err:
79-
if response.text:
80-
if verbose:
81-
print('Content received from server:\n{}'.format(
82-
response.text))
83-
else:
84-
print('NOTE: Try --verbose to see response content.')
85-
raise err
86-
87-
8851
def upload(upload_settings, dists):
8952
dists = _find_dists(dists)
9053

@@ -136,7 +99,7 @@ def upload(upload_settings, dists):
13699
print(skip_message)
137100
continue
138101

139-
check_status_code(resp, upload_settings.verbose)
102+
utils.check_status_code(resp, upload_settings.verbose)
140103

141104
uploaded_packages.append(package)
142105

twine/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import configparser
2323
from urllib.parse import urlparse, urlunparse
2424

25+
import requests
2526

2627
try:
2728
import keyring # noqa
@@ -128,6 +129,41 @@ def normalize_repository_url(url):
128129
return urlunparse(parsed)
129130

130131

132+
def check_status_code(response, verbose):
133+
"""Generate a helpful message based on the response from the repository.
134+
135+
Raise a custom exception for recognized errors. Otherwise, print the
136+
response content (based on the verbose option) before re-raising the
137+
HTTPError.
138+
"""
139+
if response.status_code == 410 and "pypi.python.org" in response.url:
140+
raise exceptions.UploadToDeprecatedPyPIDetected(
141+
f"It appears you're uploading to pypi.python.org (or "
142+
f"testpypi.python.org). You've received a 410 error response. "
143+
f"Uploading to those sites is deprecated. The new sites are "
144+
f"pypi.org and test.pypi.org. Try using {DEFAULT_REPOSITORY} (or "
145+
f"{TEST_REPOSITORY}) to upload your packages instead. These are "
146+
f"the default URLs for Twine now. More at "
147+
f"https://packaging.python.org/guides/migrating-to-pypi-org/.")
148+
elif response.status_code == 405 and "pypi.org" in response.url:
149+
raise exceptions.InvalidPyPIUploadURL(
150+
f"It appears you're trying to upload to pypi.org but have an "
151+
f"invalid URL. You probably want one of these two URLs: "
152+
f"{DEFAULT_REPOSITORY} or {TEST_REPOSITORY}. Check your "
153+
f"--repository-url value.")
154+
155+
try:
156+
response.raise_for_status()
157+
except requests.HTTPError as err:
158+
if response.text:
159+
if verbose:
160+
print('Content received from server:\n{}'.format(
161+
response.text))
162+
else:
163+
print('NOTE: Try --verbose to see response content.')
164+
raise err
165+
166+
131167
def get_userpass_value(cli_value, config, key, prompt_strategy=None):
132168
"""Gets the username / password from config.
133169

0 commit comments

Comments
 (0)