Skip to content

Commit

Permalink
Add exception for E405; add test for E410
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Saky <[email protected]>
  • Loading branch information
adityasaky committed Oct 12, 2019
1 parent a31ba7b commit b36d6b8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
26 changes: 20 additions & 6 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from twine import package, cli, exceptions
import twine
from twine.utils import DEFAULT_REPOSITORY, TEST_REPOSITORY
from twine.exceptions import PyPIMethodNotAllowed, \
UploadToDeprecatedPyPIDetected

import helpers

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

@pytest.mark.parametrize('repo_url', [
"https://upload.pypi.org/",
"https://test.pypi.org/"
"https://test.pypi.org/",
"https://pypi.org/"
])
def test_check_status_code_for_wrong_repo_url(repo_url, make_settings, capsys):
def test_check_status_code_for_wrong_repo_url(repo_url, make_settings):
upload_settings = make_settings()

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

with pytest.raises(HTTPError):
with pytest.raises(PyPIMethodNotAllowed):
upload.upload(upload_settings, [
WHEEL_FIXTURE, SDIST_FIXTURE, NEW_SDIST_FIXTURE, NEW_WHEEL_FIXTURE
])

captured = capsys.readouterr()
assert captured.out.count(DEFAULT_REPOSITORY) == 1
assert captured.out.count(TEST_REPOSITORY) == 1

@pytest.mark.parametrize('repo_url', [
"https://pypi.python.org",
"https://testpypi.python.org"
])
def test_check_status_code_for_deprecated_pypi_url(repo_url):
response = pretend.stub(
status_code=410,
url=repo_url
)

# value of Verbose doesn't matter for this check
with pytest.raises(UploadToDeprecatedPyPIDetected):
upload.check_status_code(response, False)
28 changes: 18 additions & 10 deletions twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,25 @@ def check_status_code(response, verbose):
if (response.status_code == 410 and
response.url.startswith(("https://pypi.python.org",
"https://testpypi.python.org"))):
print("It appears you're uploading to pypi.python.org (or "
"testpypi.python.org). You've received a 410 error response. "
"Uploading to those sites is deprecated. The new sites are "
"pypi.org and test.pypi.org. Try using "
f"{DEFAULT_REPOSITORY} "
f"(or {TEST_REPOSITORY}) to upload your packages "
"instead. These are the default URLs for Twine now. More at "
"https://packaging.python.org/guides/migrating-to-pypi-org/ ")
raise exceptions.\
UploadToDeprecatedPyPIDetected(f"It appears you're uploading to "
f"pypi.python.org (or "
f"testpypi.python.org). You've "
f"received a 410 error response. "
f"Uploading to those sites is "
f"deprecated. The new sites are "
f"pypi.org and test.pypi.org. Try "
f"using {DEFAULT_REPOSITORY} (or "
f"{TEST_REPOSITORY}) to upload your"
f" packages instead. These are the "
f"default URLs for Twine now. More "
f"at https://packaging.python.org/"
f"guides/migrating-to-pypi-org/.")
elif response.status_code == 405 and "pypi.org" in response.url:
print(f"You probably want one of these two URLs: {DEFAULT_REPOSITORY} "
f"or {TEST_REPOSITORY}. Check your --repository-url value.")
raise exceptions.PyPIMethodNotAllowed(f"You probably want one of these"
f"two URLs: {DEFAULT_REPOSITORY}"
f"or {TEST_REPOSITORY}. Check "
f"your --repository-url value.")
try:
response.raise_for_status()
except HTTPError as err:
Expand Down
8 changes: 8 additions & 0 deletions twine/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ class InvalidDistribution(TwineException):
"""Raised when a distribution is invalid."""

pass


class PyPIMethodNotAllowed(TwineException):
"""Raised when --repository-url contains pypi.org but the upload
method is not supported.
"""

pass

0 comments on commit b36d6b8

Please sign in to comment.