Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_username_non_interactive_aborts(config):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, cred("user")).password
def test_non_existent_package(register_settings):
"""Test a non-existent package registration"""
stub_repository = pretend.stub()
register_settings.create_repository = lambda: stub_repository
package = "/foo/bar/baz.whl"
with pytest.raises(
exceptions.PackageNotFound,
match=f'"{package}" does not exist on the file system.',
):
register.register(register_settings, package)
"""
)
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_get_repository_config_with_invalid_url(tmpdir, repo_url, message):
"""Raise an exception for a URL with an invalid/missing scheme and/or host."""
pypirc = os.path.join(str(tmpdir), ".pypirc")
with pytest.raises(
exceptions.UnreachableRepositoryURLDetected, match=message,
):
utils.get_repository_from_config(pypirc, "pypi", repo_url)
def test_get_password_keyring_missing_non_interactive_aborts(
entered_username, keyring_missing_get_credentials, config
):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, cred("user")).password
def _handle_package_signing(
self, sign: bool, sign_with: str, identity: Optional[str]
) -> None:
if not sign and identity:
raise exceptions.InvalidSigningConfiguration(
"sign must be given along with identity"
)
self.sign = sign
self.sign_with = sign_with
self.identity = identity
def register(register_settings: settings.Settings, package: str) -> None:
repository_url = cast(str, register_settings.repository_config["repository"])
print(f"Registering package to {repository_url}")
repository = register_settings.create_repository()
if not os.path.exists(package):
raise exceptions.PackageNotFound(
f'"{package}" does not exist on the file system.'
)
resp = repository.register(
package_file.PackageFile.from_filename(package, register_settings.comment)
)
repository.close()
if resp.is_redirect:
raise exceptions.RedirectDetected.from_args(
repository_url, resp.headers["location"],
)
resp.raise_for_status()
def check_repository_url(self) -> None:
"""Verify we are not using legacy PyPI.
:raises:
:class:`~twine.exceptions.UploadToDeprecatedPyPIDetected`
"""
repository_url = cast(str, self.repository_config["repository"])
if repository_url.startswith(
(repository.LEGACY_PYPI, repository.LEGACY_TEST_PYPI)
):
raise exceptions.UploadToDeprecatedPyPIDetected.from_args(
repository_url, utils.DEFAULT_REPOSITORY, utils.TEST_REPOSITORY
)
def _validate_repository_url(repository_url: str) -> None:
"""Validate the given url for allowed schemes and components."""
# Allowed schemes are http and https, based on whether the repository
# supports TLS or not, and scheme and host must be present in the URL
validator = (
rfc3986.validators.Validator()
.allow_schemes("http", "https")
.require_presence_of("scheme", "host")
)
try:
validator.validate(rfc3986.uri_reference(repository_url))
except rfc3986.exceptions.RFC3986Exception as exc:
raise exceptions.UnreachableRepositoryURLDetected(
f"Invalid repository URL: {exc.args[0]}."
)