Skip to content

Commit b36d6b8

Browse files
committed
Add exception for E405; add test for E410
Signed-off-by: Aditya Saky <[email protected]>
1 parent a31ba7b commit b36d6b8

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

tests/test_upload.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from twine import package, cli, exceptions
2020
import twine
2121
from twine.utils import DEFAULT_REPOSITORY, TEST_REPOSITORY
22+
from twine.exceptions import PyPIMethodNotAllowed, \
23+
UploadToDeprecatedPyPIDetected
2224

2325
import helpers
2426

@@ -290,19 +292,31 @@ def none_upload(*args, **settings_kwargs):
290292

291293
@pytest.mark.parametrize('repo_url', [
292294
"https://upload.pypi.org/",
293-
"https://test.pypi.org/"
295+
"https://test.pypi.org/",
296+
"https://pypi.org/"
294297
])
295-
def test_check_status_code_for_wrong_repo_url(repo_url, make_settings, capsys):
298+
def test_check_status_code_for_wrong_repo_url(repo_url, make_settings):
296299
upload_settings = make_settings()
297300

298301
# override defaults to use incorrect URL
299302
upload_settings.repository_config['repository'] = repo_url
300303

301-
with pytest.raises(HTTPError):
304+
with pytest.raises(PyPIMethodNotAllowed):
302305
upload.upload(upload_settings, [
303306
WHEEL_FIXTURE, SDIST_FIXTURE, NEW_SDIST_FIXTURE, NEW_WHEEL_FIXTURE
304307
])
305308

306-
captured = capsys.readouterr()
307-
assert captured.out.count(DEFAULT_REPOSITORY) == 1
308-
assert captured.out.count(TEST_REPOSITORY) == 1
309+
310+
@pytest.mark.parametrize('repo_url', [
311+
"https://pypi.python.org",
312+
"https://testpypi.python.org"
313+
])
314+
def test_check_status_code_for_deprecated_pypi_url(repo_url):
315+
response = pretend.stub(
316+
status_code=410,
317+
url=repo_url
318+
)
319+
320+
# value of Verbose doesn't matter for this check
321+
with pytest.raises(UploadToDeprecatedPyPIDetected):
322+
upload.check_status_code(response, False)

twine/commands/upload.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,25 @@ def check_status_code(response, verbose):
6060
if (response.status_code == 410 and
6161
response.url.startswith(("https://pypi.python.org",
6262
"https://testpypi.python.org"))):
63-
print("It appears you're uploading to pypi.python.org (or "
64-
"testpypi.python.org). You've received a 410 error response. "
65-
"Uploading to those sites is deprecated. The new sites are "
66-
"pypi.org and test.pypi.org. Try using "
67-
f"{DEFAULT_REPOSITORY} "
68-
f"(or {TEST_REPOSITORY}) to upload your packages "
69-
"instead. These are the default URLs for Twine now. More at "
70-
"https://packaging.python.org/guides/migrating-to-pypi-org/ ")
63+
raise exceptions.\
64+
UploadToDeprecatedPyPIDetected(f"It appears you're uploading to "
65+
f"pypi.python.org (or "
66+
f"testpypi.python.org). You've "
67+
f"received a 410 error response. "
68+
f"Uploading to those sites is "
69+
f"deprecated. The new sites are "
70+
f"pypi.org and test.pypi.org. Try "
71+
f"using {DEFAULT_REPOSITORY} (or "
72+
f"{TEST_REPOSITORY}) to upload your"
73+
f" packages instead. These are the "
74+
f"default URLs for Twine now. More "
75+
f"at https://packaging.python.org/"
76+
f"guides/migrating-to-pypi-org/.")
7177
elif response.status_code == 405 and "pypi.org" in response.url:
72-
print(f"You probably want one of these two URLs: {DEFAULT_REPOSITORY} "
73-
f"or {TEST_REPOSITORY}. Check your --repository-url value.")
78+
raise exceptions.PyPIMethodNotAllowed(f"You probably want one of these"
79+
f"two URLs: {DEFAULT_REPOSITORY}"
80+
f"or {TEST_REPOSITORY}. Check "
81+
f"your --repository-url value.")
7482
try:
7583
response.raise_for_status()
7684
except HTTPError as err:

twine/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ class InvalidDistribution(TwineException):
101101
"""Raised when a distribution is invalid."""
102102

103103
pass
104+
105+
106+
class PyPIMethodNotAllowed(TwineException):
107+
"""Raised when --repository-url contains pypi.org but the upload
108+
method is not supported.
109+
"""
110+
111+
pass

0 commit comments

Comments
 (0)