Skip to content

Commit 0f06e29

Browse files
committed
Add helpers to create test sdists and wheels
1 parent 4406034 commit 0f06e29

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tests/helpers.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,54 @@ def build_archive(path, name, archive_format, files):
4444
archive.addfile(member, io.BytesIO(data))
4545
return filepath
4646

47-
if archive_format == "zip":
47+
if archive_format == "zip" or archive_format == "whl":
4848
with zipfile.ZipFile(filepath, mode="w") as archive:
4949
for mname, content in files.items():
5050
archive.writestr(mname, textwrap.dedent(content))
5151
return filepath
5252

5353
raise ValueError(format)
54+
55+
56+
def build_sdist(path, name, version, files):
57+
content = {
58+
f"{name}-{version}/README": "README",
59+
f"{name}-{version}/PKG-INFO": f"""\
60+
Metadata-Version: 1.1
61+
Name: {name}
62+
Version: {version}
63+
""",
64+
}
65+
66+
# Remap file paths to be under archive root folder.
67+
root = f"{name}-{version}"
68+
for key, value in files.items():
69+
content[f"{root}/{key}"] = value
70+
71+
return build_archive(path, f"{name}-{version}", "tar.gz", content)
72+
73+
74+
def build_wheel(path, name, version, files):
75+
# This does not generate valid a wheel file: the archive lacks the
76+
# .dist-info/RECORD member with the content listing. However, the
77+
# generated files pass validation done by twine and are sufficient
78+
# for testing purposes.
79+
80+
content = {
81+
f"{name}-{version}.dist-info/WHEEL": """\
82+
Wheel-Version: 1.0
83+
Generator: test
84+
Root-Is-Purelib: true
85+
Tag: py3-none-any
86+
""",
87+
f"{name}-{version}.dist-info/METADATA": f"""\
88+
Metadata-Version: 1.1
89+
Name: {name}
90+
Version: {version}
91+
""",
92+
}
93+
94+
# Add wheel content.
95+
content.update(files)
96+
97+
return build_archive(path, f"{name}-{version}-py3-none-any", "whl", content)

0 commit comments

Comments
 (0)