Skip to content

Commit

Permalink
Move build_archive() from test_sdist to common helpers module
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnicolodi committed Dec 19, 2024
1 parent 46b704b commit 8b9552b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
29 changes: 29 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,41 @@
# 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")
SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0.tar.gz")
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)
53 changes: 13 additions & 40 deletions tests/test_sdist.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import io
import os
import pathlib
import tarfile
import textwrap
import zipfile

import pytest

from twine import exceptions
from twine import sdist

from .helpers import TESTS_DIR
from .helpers import build_archive


@pytest.fixture(
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -93,15 +66,15 @@ 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


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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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):
Expand Down

0 comments on commit 8b9552b

Please sign in to comment.