Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
stub_response = pretend.stub(
is_redirect=True,
status_code=301,
headers={"location": "https://test.pypi.org/legacy/"},
)
stub_repository = pretend.stub(
upload=lambda package: stub_response, close=lambda: None
)
upload_settings.create_repository = lambda: stub_repository
with pytest.raises(exceptions.RedirectDetected) as err:
upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
assert "https://test.pypi.org/legacy/" in err.value.args[0]
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(exceptions.InvalidPyPIUploadURL):
upload.upload(
upload_settings,
[
helpers.WHEEL_FIXTURE,
helpers.SDIST_FIXTURE,
helpers.NEW_SDIST_FIXTURE,
helpers.NEW_WHEEL_FIXTURE,
],
def test_deprecated_repo(make_settings):
with pytest.raises(exceptions.UploadToDeprecatedPyPIDetected) as err:
upload_settings = make_settings(
"""
[pypi]
repository: https://pypi.python.org/pypi/
username:foo
password:bar
"""
)
upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
assert all(
text in err.value.args[0]
for text in [
"https://pypi.python.org/pypi/",
"https://upload.pypi.org/legacy/",
"https://test.pypi.org/legacy/",
"https://packaging.python.org/",
]
def test_exception_for_http_status(verbose, upload_settings, stub_response, capsys):
upload_settings.verbose = verbose
stub_response.is_redirect = False
stub_response.status_code = 403
stub_response.text = "Invalid or non-existent authentication information"
stub_response.raise_for_status = pretend.raiser(requests.HTTPError)
with pytest.raises(requests.HTTPError):
upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
captured = capsys.readouterr()
assert RELEASE_URL not in captured.out
if verbose:
assert stub_response.text in captured.out
assert "--verbose" not in captured.out
else:
assert stub_response.text not in captured.out
assert "--verbose" in captured.out
def test_success_when_gpg_is_run(upload_settings, stub_repository, monkeypatch):
"""Add GPG signature generated by gpg command to uploaded package."""
# Indicate that upload() should run_gpg() to generate the signature, which
# we'll stub out to use WHEEL_FIXTURE + ".asc"
upload_settings.sign = True
upload_settings.sign_with = "gpg"
monkeypatch.setattr(
package_file.PackageFile,
"run_gpg",
pretend.call_recorder(lambda cls, gpg_args: None),
)
# Upload an unsigned distribution
result = upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
assert result is None
# The signature shoud be added via package.sign()
package = stub_repository.upload.calls[0].args[0]
assert len(package.run_gpg.calls) == 1
assert helpers.WHEEL_FIXTURE in package.run_gpg.calls[0].args[1]
assert package.gpg_signature == (
"twine-1.5.0-py2.py3-none-any.whl.asc",
b"signature",
)
def test_success_with_pre_signed_distribution(upload_settings, stub_repository):
"""Add GPG signature provided by user to uploaded package."""
# Upload a pre-signed distribution
result = upload.upload(
upload_settings, [helpers.WHEEL_FIXTURE, helpers.WHEEL_FIXTURE + ".asc"]
)
assert result is None
# The signature shoud be added via package.add_gpg_signature()
package = stub_repository.upload.calls[0].args[0]
assert package.gpg_signature == (
"twine-1.5.0-py2.py3-none-any.whl.asc",
b"signature",
)
def test_print_packages_if_verbose(upload_settings, capsys):
"""Print the path and file size of each distribution attempting to be uploaded."""
dists_to_upload = {
helpers.WHEEL_FIXTURE: "15.4 KB",
helpers.SDIST_FIXTURE: "20.8 KB",
helpers.NEW_SDIST_FIXTURE: "26.1 KB",
helpers.NEW_WHEEL_FIXTURE: "21.9 KB",
}
upload_settings.verbose = True
result = upload.upload(upload_settings, dists_to_upload.keys())
assert result is None
captured = capsys.readouterr()
for filename, size in dists_to_upload.items():
assert captured.out.count(f"{filename} ({size})") == 1
pypi_user = None
pypi_pass = None
print("No PYPI user information in environment")
comp = comp_names[component]
distpath = os.path.join(comp.path, 'dist', '*')
distpath = os.path.realpath(os.path.abspath(distpath))
dists = glob.glob(distpath)
if pypi_user is None:
args = ['twine', 'upload', distpath]
else:
args = ['twine', 'upload', '-u', pypi_user, '-p', pypi_pass, distpath]
# Invoke upload this way since subprocess call of twine cli has cross platform issues
upload(dists, 'pypi', False, None, pypi_user, pypi_pass, None, None, '~/.pypirc', False, None, None, None)
The pypi username and password must either be specified in a ~/.pypirc file
or in environment variables PYPI_USER and PYPI_PASS
"""
if 'PYPI_USER' in os.environ and 'PYPI_PASS' in os.environ:
pypi_user = os.environ['PYPI_USER']
pypi_pass = os.environ['PYPI_PASS']
else:
pypi_user = None
pypi_pass = None
print("No PYPI user information in environment")
packages = glob.glob(dist_dir)
# Invoke upload this way since subprocess call of twine cli has cross platform issues
twine_upload(packages, 'pypi', False, None, pypi_user, pypi_pass, None, None, '~/.pypirc', False, None, None, None)