Skip to content

Commit

Permalink
Use a better way to construct file names in test_commands
Browse files Browse the repository at this point in the history
There is no need for the strings to be real file paths and using
string manipulation to build the file names is error prone. Define
simple helpers that construct the file names as needed. This makes the
tests intent and implementation easier to understand.
  • Loading branch information
dnicolodi committed Jan 11, 2025
1 parent 4406034 commit 2f8f562
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dataclasses
import os

import pytest

from tests import helpers
from twine import commands
from twine import exceptions

Expand Down Expand Up @@ -52,41 +52,69 @@ def test_find_dists_handles_real_files():
assert expected == files


class Signed(str):

@property
def signature(self):
return f"{self}.asc"

def attestation(self, what):
return f"{self}.{what}.attestation"


@dataclasses.dataclass
class Distribution:
name: str
version: str

@property
def sdist(self):
return Signed(f"dist/{self.name}-{self.version}.tar.gz")

@property
def wheel(self):
return Signed(f"dist/{self.name}-{self.version}-py2.py3-none-any.whl")


def test_split_inputs():
"""Split inputs into dists, signatures, and attestations."""
a = Distribution("twine", "1.5.0")
b = Distribution("twine", "1.6.5")

inputs = [
helpers.WHEEL_FIXTURE,
helpers.WHEEL_FIXTURE + ".asc",
helpers.WHEEL_FIXTURE + ".build.attestation",
helpers.WHEEL_FIXTURE + ".publish.attestation",
helpers.SDIST_FIXTURE,
helpers.SDIST_FIXTURE + ".asc",
helpers.NEW_WHEEL_FIXTURE,
helpers.NEW_WHEEL_FIXTURE + ".frob.attestation",
helpers.NEW_SDIST_FIXTURE,
a.wheel,
a.wheel.signature,
a.wheel.attestation("build"),
a.wheel.attestation("build.publish"),
a.sdist,
a.sdist.signature,
b.wheel,
b.wheel.attestation("frob"),
b.sdist,
]

inputs = commands._split_inputs(inputs)

assert inputs.dists == [
helpers.WHEEL_FIXTURE,
helpers.SDIST_FIXTURE,
helpers.NEW_WHEEL_FIXTURE,
helpers.NEW_SDIST_FIXTURE,
a.wheel,
a.sdist,
b.wheel,
b.sdist,
]

expected_signatures = {
os.path.basename(dist) + ".asc": dist + ".asc"
for dist in [helpers.WHEEL_FIXTURE, helpers.SDIST_FIXTURE]
assert inputs.signatures == {
"twine-1.5.0-py2.py3-none-any.whl.asc": a.wheel.signature,
"twine-1.5.0.tar.gz.asc": a.sdist.signature,
}
assert inputs.signatures == expected_signatures

assert inputs.attestations_by_dist == {
helpers.WHEEL_FIXTURE: [
helpers.WHEEL_FIXTURE + ".build.attestation",
helpers.WHEEL_FIXTURE + ".publish.attestation",
a.wheel: [
a.wheel.attestation("build"),
a.wheel.attestation("build.publish"),
],
a.sdist: [],
b.wheel: [
b.wheel.attestation("frob"),
],
helpers.SDIST_FIXTURE: [],
helpers.NEW_WHEEL_FIXTURE: [helpers.NEW_WHEEL_FIXTURE + ".frob.attestation"],
helpers.NEW_SDIST_FIXTURE: [],
b.sdist: [],
}

0 comments on commit 2f8f562

Please sign in to comment.