From cdebaeb1a6ee113ad48eda343ce3604507c6e454 Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Tue, 17 Dec 2024 23:00:22 +0100 Subject: [PATCH] Use a better way to construct file names in test_commands 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. --- tests/test_commands.py | 77 +++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index c5aa31a2..06ea272e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,8 +1,8 @@ +import dataclasses import os import pytest -from tests import helpers from twine import commands from twine import exceptions @@ -52,41 +52,70 @@ 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: [], }