Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bffd296

Browse files
committedJan 9, 2025
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.
1 parent fd0646e commit bffd296

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed
 

‎tests/helpers.py

+29
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,41 @@
1313
# limitations under the License.
1414
"""Test functions useful across twine's tests."""
1515

16+
import io
1617
import os
1718
import pathlib
19+
import tarfile
20+
import textwrap
21+
import zipfile
1822

1923
TESTS_DIR = pathlib.Path(__file__).parent
2024
FIXTURES_DIR = os.path.join(TESTS_DIR, "fixtures")
2125
SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0.tar.gz")
2226
WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0-py2.py3-none-any.whl")
2327
NEW_SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5.tar.gz")
2428
NEW_WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5-py2.py3-none-any.whl")
29+
30+
31+
def build_archive(path, name, archive_format, files):
32+
filepath = path / f"{name}.{archive_format}"
33+
34+
if archive_format == "tar.gz":
35+
with tarfile.open(filepath, "x:gz") as archive:
36+
for mname, content in files.items():
37+
if isinstance(content, tarfile.TarInfo):
38+
content.name = mname
39+
archive.addfile(content)
40+
else:
41+
data = textwrap.dedent(content).encode("utf8")
42+
member = tarfile.TarInfo(mname)
43+
member.size = len(data)
44+
archive.addfile(member, io.BytesIO(data))
45+
return filepath
46+
47+
if archive_format == "zip":
48+
with zipfile.ZipFile(filepath, mode="w") as archive:
49+
for mname, content in files.items():
50+
archive.writestr(mname, textwrap.dedent(content))
51+
return filepath
52+
53+
raise ValueError(format)

‎tests/test_sdist.py

+13-40
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import io
21
import os
32
import pathlib
43
import tarfile
5-
import textwrap
6-
import zipfile
74

85
import pytest
96

107
from twine import exceptions
118
from twine import sdist
129

1310
from .helpers import TESTS_DIR
11+
from .helpers import build_archive
1412

1513

1614
@pytest.fixture(
@@ -29,31 +27,6 @@ def archive_format(request):
2927
return request.param
3028

3129

32-
def build_archive(path, name, archive_format, files):
33-
filepath = path / f"{name}.{archive_format}"
34-
35-
if archive_format == "tar.gz":
36-
with tarfile.open(filepath, "x:gz") as archive:
37-
for mname, content in files.items():
38-
if isinstance(content, tarfile.TarInfo):
39-
content.name = mname
40-
archive.addfile(content)
41-
else:
42-
data = textwrap.dedent(content).encode("utf8")
43-
member = tarfile.TarInfo(mname)
44-
member.size = len(data)
45-
archive.addfile(member, io.BytesIO(data))
46-
return str(filepath)
47-
48-
if archive_format == "zip":
49-
with zipfile.ZipFile(filepath, mode="w") as archive:
50-
for mname, content in files.items():
51-
archive.writestr(mname, textwrap.dedent(content))
52-
return str(filepath)
53-
54-
raise ValueError(format)
55-
56-
5730
def test_read_example(example_sdist):
5831
"""Parse metadata from a valid sdist file."""
5932
metadata = example_sdist.read()
@@ -78,7 +51,7 @@ def test_formar_not_supported():
7851

7952
def test_read(archive_format, tmp_path):
8053
"""Read PKG-INFO from a valid sdist."""
81-
filename = build_archive(
54+
filepath = build_archive(
8255
tmp_path,
8356
"test-1.2.3",
8457
archive_format,
@@ -92,15 +65,15 @@ def test_read(archive_format, tmp_path):
9265
},
9366
)
9467

95-
metadata = sdist.SDist(filename).read()
68+
metadata = sdist.SDist(str(filepath)).read()
9669
assert b"Metadata-Version: 1.1" in metadata
9770
assert b"Name: test" in metadata
9871
assert b"Version: 1.2.3" in metadata
9972

10073

10174
def test_missing_pkg_info(archive_format, tmp_path):
10275
"""Raise an exception when sdist does not contain PKG-INFO."""
103-
filename = build_archive(
76+
filepath = build_archive(
10477
tmp_path,
10578
"test-1.2.3",
10679
archive_format,
@@ -110,12 +83,12 @@ def test_missing_pkg_info(archive_format, tmp_path):
11083
)
11184

11285
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
113-
sdist.SDist(filename).read()
86+
sdist.SDist(str(filepath)).read()
11487

11588

11689
def test_invalid_pkg_info(archive_format, tmp_path):
11790
"""Raise an exception when PKG-INFO does not contain ``Metadata-Version``."""
118-
filename = build_archive(
91+
filepath = build_archive(
11992
tmp_path,
12093
"test-1.2.3",
12194
archive_format,
@@ -129,12 +102,12 @@ def test_invalid_pkg_info(archive_format, tmp_path):
129102
)
130103

131104
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
132-
sdist.SDist(filename).read()
105+
sdist.SDist(str(filepath)).read()
133106

134107

135108
def test_pkg_info_directory(archive_format, tmp_path):
136109
"""Raise an exception when PKG-INFO is a directory."""
137-
filename = build_archive(
110+
filepath = build_archive(
138111
tmp_path,
139112
"test-1.2.3",
140113
archive_format,
@@ -149,7 +122,7 @@ def test_pkg_info_directory(archive_format, tmp_path):
149122
)
150123

151124
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
152-
sdist.SDist(filename).read()
125+
sdist.SDist(str(filepath)).read()
153126

154127

155128
def test_pkg_info_not_regular_file(tmp_path):
@@ -158,7 +131,7 @@ def test_pkg_info_not_regular_file(tmp_path):
158131
link.type = tarfile.LNKTYPE
159132
link.linkname = "README"
160133

161-
filename = build_archive(
134+
filepath = build_archive(
162135
tmp_path,
163136
"test-1.2.3",
164137
"tar.gz",
@@ -169,12 +142,12 @@ def test_pkg_info_not_regular_file(tmp_path):
169142
)
170143

171144
with pytest.raises(exceptions.InvalidDistribution, match="PKG-INFO is not a reg"):
172-
sdist.SDist(filename).read()
145+
sdist.SDist(str(filepath)).read()
173146

174147

175148
def test_multiple_top_level(archive_format, tmp_path):
176149
"""Raise an exception when there are too many top-level members."""
177-
filename = build_archive(
150+
filepath = build_archive(
178151
tmp_path,
179152
"test-1.2.3",
180153
archive_format,
@@ -190,7 +163,7 @@ def test_multiple_top_level(archive_format, tmp_path):
190163
)
191164

192165
with pytest.raises(exceptions.InvalidDistribution, match="Too many top-level"):
193-
sdist.SDist(filename).read()
166+
sdist.SDist(str(filepath)).read()
194167

195168

196169
def test_py_version(example_sdist):

0 commit comments

Comments
 (0)
Failed to load comments.