From 8b9552b7bef55b1fe71fd4bf82451cd404582a14 Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Wed, 18 Dec 2024 13:38:09 +0100 Subject: [PATCH] Move build_archive() from test_sdist to common helpers module This will allow to reuse it in later commits. Change the function to return a pathlib.Path instead of a string because that is what is used internally and because it is easy to turn that into a string when needed, but it is a bit more cumbersome to do the opposite. --- tests/helpers.py | 29 +++++++++++++++++++++++++ tests/test_sdist.py | 53 +++++++++++---------------------------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index da28a336..b1ea8068 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -13,8 +13,12 @@ # limitations under the License. """Test functions useful across twine's tests.""" +import io import os import pathlib +import tarfile +import textwrap +import zipfile TESTS_DIR = pathlib.Path(__file__).parent FIXTURES_DIR = os.path.join(TESTS_DIR, "fixtures") @@ -22,3 +26,28 @@ WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0-py2.py3-none-any.whl") NEW_SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5.tar.gz") NEW_WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5-py2.py3-none-any.whl") + + +def build_archive(path, name, archive_format, files): + filepath = path / f"{name}.{archive_format}" + + if archive_format == "tar.gz": + with tarfile.open(filepath, "x:gz") as archive: + for mname, content in files.items(): + if isinstance(content, tarfile.TarInfo): + content.name = mname + archive.addfile(content) + else: + data = textwrap.dedent(content).encode("utf8") + member = tarfile.TarInfo(mname) + member.size = len(data) + archive.addfile(member, io.BytesIO(data)) + return filepath + + if archive_format == "zip": + with zipfile.ZipFile(filepath, mode="w") as archive: + for mname, content in files.items(): + archive.writestr(mname, textwrap.dedent(content)) + return filepath + + raise ValueError(format) diff --git a/tests/test_sdist.py b/tests/test_sdist.py index 27bbabc8..892932ca 100644 --- a/tests/test_sdist.py +++ b/tests/test_sdist.py @@ -1,9 +1,6 @@ -import io import os import pathlib import tarfile -import textwrap -import zipfile import pytest @@ -11,6 +8,7 @@ from twine import sdist from .helpers import TESTS_DIR +from .helpers import build_archive @pytest.fixture( @@ -30,31 +28,6 @@ def archive_format(request): return request.param -def build_archive(path, name, archive_format, files): - filepath = path / f"{name}.{archive_format}" - - if archive_format == "tar.gz": - with tarfile.open(filepath, "x:gz") as archive: - for mname, content in files.items(): - if isinstance(content, tarfile.TarInfo): - content.name = mname - archive.addfile(content) - else: - data = textwrap.dedent(content).encode("utf8") - member = tarfile.TarInfo(mname) - member.size = len(data) - archive.addfile(member, io.BytesIO(data)) - return str(filepath) - - if archive_format == "zip": - with zipfile.ZipFile(filepath, mode="w") as archive: - for mname, content in files.items(): - archive.writestr(mname, textwrap.dedent(content)) - return str(filepath) - - raise ValueError(format) - - def test_read_example(example_sdist): """Parse metadata from a valid sdist file.""" metadata = example_sdist.read() @@ -79,7 +52,7 @@ def test_formar_not_supported(): def test_read(archive_format, tmp_path): """Read PKG-INFO from a valid sdist.""" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", archive_format, @@ -93,7 +66,7 @@ def test_read(archive_format, tmp_path): }, ) - metadata = sdist.SDist(filename).read() + metadata = sdist.SDist(str(filepath)).read() assert b"Metadata-Version: 1.1" in metadata assert b"Name: test" in metadata assert b"Version: 1.2.3" in metadata @@ -101,7 +74,7 @@ def test_read(archive_format, tmp_path): def test_missing_pkg_info(archive_format, tmp_path): """Raise an exception when sdist does not contain PKG-INFO.""" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", archive_format, @@ -111,12 +84,12 @@ def test_missing_pkg_info(archive_format, tmp_path): ) with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"): - sdist.SDist(filename).read() + sdist.SDist(str(filepath)).read() def test_invalid_pkg_info(archive_format, tmp_path): """Raise an exception when PKG-INFO does not contain ``Metadata-Version``.""" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", archive_format, @@ -130,12 +103,12 @@ def test_invalid_pkg_info(archive_format, tmp_path): ) with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"): - sdist.SDist(filename).read() + sdist.SDist(str(filepath)).read() def test_pkg_info_directory(archive_format, tmp_path): """Raise an exception when PKG-INFO is a directory.""" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", archive_format, @@ -150,7 +123,7 @@ def test_pkg_info_directory(archive_format, tmp_path): ) with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"): - sdist.SDist(filename).read() + sdist.SDist(str(filepath)).read() def test_pkg_info_not_regular_file(tmp_path): @@ -159,7 +132,7 @@ def test_pkg_info_not_regular_file(tmp_path): link.type = tarfile.LNKTYPE link.linkname = "README" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", "tar.gz", @@ -170,12 +143,12 @@ def test_pkg_info_not_regular_file(tmp_path): ) with pytest.raises(exceptions.InvalidDistribution, match="PKG-INFO is not a reg"): - sdist.SDist(filename).read() + sdist.SDist(str(filepath)).read() def test_multiple_top_level(archive_format, tmp_path): """Raise an exception when there are too many top-level members.""" - filename = build_archive( + filepath = build_archive( tmp_path, "test-1.2.3", archive_format, @@ -191,7 +164,7 @@ def test_multiple_top_level(archive_format, tmp_path): ) with pytest.raises(exceptions.InvalidDistribution, match="Too many top-level"): - sdist.SDist(filename).read() + sdist.SDist(str(filepath)).read() def test_py_version(example_sdist):