From c9fa2aa95aa0cdaf5c373abbe03c9ad4d7ce1a12 Mon Sep 17 00:00:00 2001 From: Jacob Woliver Date: Thu, 20 Oct 2022 10:14:10 -0400 Subject: [PATCH 1/3] Display error when uploading signed file with no distribution Related to #931. If a user only uploads a signed .asc file without any distribution files, this now throws an error to make it more clear why Twine doesn't do anything. --- AUTHORS | 1 + changelog/931.misc.rst | 1 + tests/test_upload.py | 12 ++++++++++++ twine/commands/upload.py | 8 ++++++++ 4 files changed, 22 insertions(+) create mode 100644 changelog/931.misc.rst diff --git a/AUTHORS b/AUTHORS index 1c12eb8c..d8034db5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -32,3 +32,4 @@ Yesha Maggi Cyril de Catheu (https://catheu.tech/) Thomas Miedema Hugo van Kemenade (https://github.com/hugovk) +Jacob Woliver (jmw.sh) diff --git a/changelog/931.misc.rst b/changelog/931.misc.rst new file mode 100644 index 00000000..5be844d5 --- /dev/null +++ b/changelog/931.misc.rst @@ -0,0 +1 @@ +Throws an error when uploading signed files without any distribution files. \ No newline at end of file diff --git a/tests/test_upload.py b/tests/test_upload.py index 29e39e02..b92ad355 100644 --- a/tests/test_upload.py +++ b/tests/test_upload.py @@ -178,6 +178,18 @@ def test_success_with_pre_signed_distribution(upload_settings, stub_repository): ) +def test_exception_with_only_pre_signed_file(upload_settings, stub_repository): + """Raise an exception when only a signed file is uploaded.""" + # Upload only pre-signed file + with pytest.raises(exceptions.InvalidDistribution) as err: + upload.upload(upload_settings, [helpers.WHEEL_FIXTURE + ".asc"]) + + assert ( + "Cannot upload signed files by themselves, must upload with a " + "corresponding distribution." in err.value.args[0] + ) + + 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 diff --git a/twine/commands/upload.py b/twine/commands/upload.py index d3e4b92d..15d3bcb1 100644 --- a/twine/commands/upload.py +++ b/twine/commands/upload.py @@ -127,6 +127,14 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None: repository = upload_settings.create_repository() uploaded_packages = [] + # If a user tries to upload signature files with no actual distributions, + # then throw an error. + if len(packages_to_upload) == 0 and len(signatures) > 0: + raise exceptions.InvalidDistribution( + "Cannot upload signed files by themselves, must upload with a " + "corresponding distribution." + ) + for package in packages_to_upload: skip_message = ( f"Skipping {package.basefilename} because it appears to already exist" From 0cd972875b255b4076a6e6a4b27f1ab79c29c0b2 Mon Sep 17 00:00:00 2001 From: Jacob Woliver Date: Thu, 20 Oct 2022 11:26:25 -0400 Subject: [PATCH 2/3] Simplify logic --- twine/commands/upload.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/twine/commands/upload.py b/twine/commands/upload.py index 15d3bcb1..b17c0111 100644 --- a/twine/commands/upload.py +++ b/twine/commands/upload.py @@ -127,9 +127,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None: repository = upload_settings.create_repository() uploaded_packages = [] - # If a user tries to upload signature files with no actual distributions, - # then throw an error. - if len(packages_to_upload) == 0 and len(signatures) > 0: + if signatures and not packages_to_upload: raise exceptions.InvalidDistribution( "Cannot upload signed files by themselves, must upload with a " "corresponding distribution." From b0ac0dff96f59f349734fd8fc8fbd73ea4422b64 Mon Sep 17 00:00:00 2001 From: Jacob Woliver Date: Wed, 30 Nov 2022 11:30:52 -0500 Subject: [PATCH 3/3] Clarify error message --- tests/test_upload.py | 2 +- twine/commands/upload.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_upload.py b/tests/test_upload.py index b92ad355..438dbac0 100644 --- a/tests/test_upload.py +++ b/tests/test_upload.py @@ -186,7 +186,7 @@ def test_exception_with_only_pre_signed_file(upload_settings, stub_repository): assert ( "Cannot upload signed files by themselves, must upload with a " - "corresponding distribution." in err.value.args[0] + "corresponding distribution file." in err.value.args[0] ) diff --git a/twine/commands/upload.py b/twine/commands/upload.py index b17c0111..34d3c8c6 100644 --- a/twine/commands/upload.py +++ b/twine/commands/upload.py @@ -130,7 +130,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None: if signatures and not packages_to_upload: raise exceptions.InvalidDistribution( "Cannot upload signed files by themselves, must upload with a " - "corresponding distribution." + "corresponding distribution file." ) for package in packages_to_upload: