Skip to content

Commit 4406034

Browse files
authored
Merge pull request #1208 from dnicolodi/rm-setuptools
2 parents fd0646e + 2ca55db commit 4406034

File tree

4 files changed

+104
-183
lines changed

4 files changed

+104
-183
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_check.py

+62-138
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
import textwrap
1615

17-
import build
1816
import pretend
1917
import pytest
2018

@@ -50,45 +48,30 @@ def test_fails_no_distributions(caplog):
5048
]
5149

5250

53-
def build_package(src_path, project_files, distribution="sdist"):
54-
"""
55-
Build a source distribution similar to `python3 -m build --sdist`.
56-
57-
Returns the absolute path of the built distribution.
58-
"""
59-
project_files = {
60-
"pyproject.toml": (
61-
"""
62-
[build-system]
63-
requires = ["setuptools"]
64-
build-backend = "setuptools.build_meta"
65-
"""
66-
),
67-
**project_files,
68-
}
69-
70-
for filename, content in project_files.items():
71-
(src_path / filename).write_text(textwrap.dedent(content))
72-
73-
builder = build.ProjectBuilder(src_path)
74-
return builder.build(distribution, str(src_path / "dist"))
51+
def build_sdist_with_metadata(path, metadata):
52+
name = "test"
53+
version = "1.2.3"
54+
sdist = helpers.build_archive(
55+
path,
56+
f"{name}-{version}",
57+
"tar.gz",
58+
{
59+
f"{name}-{version}/README": "README",
60+
f"{name}-{version}/PKG-INFO": metadata,
61+
},
62+
)
63+
return str(sdist)
7564

7665

77-
@pytest.mark.parametrize("distribution", ["sdist", "wheel"])
7866
@pytest.mark.parametrize("strict", [False, True])
79-
def test_warns_missing_description(distribution, strict, tmp_path, capsys, caplog):
80-
sdist = build_package(
67+
def test_warns_missing_description(strict, tmp_path, capsys, caplog):
68+
sdist = build_sdist_with_metadata(
8169
tmp_path,
82-
{
83-
"setup.cfg": (
84-
"""
85-
[metadata]
86-
name = test-package
87-
version = 0.0.1
88-
"""
89-
),
90-
},
91-
distribution=distribution,
70+
"""\
71+
Metadata-Version: 2.1
72+
Name: test
73+
Version: 1.2.3
74+
""",
9275
)
9376

9477
assert check.check([sdist], strict=strict) is strict
@@ -111,54 +94,19 @@ def test_warns_missing_description(distribution, strict, tmp_path, capsys, caplo
11194
]
11295

11396

114-
def test_warns_missing_file(tmp_path, capsys, caplog):
115-
sdist = build_package(
97+
def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
98+
sdist = build_sdist_with_metadata(
11699
tmp_path,
117-
{
118-
"setup.cfg": (
119-
"""
120-
[metadata]
121-
name = test-package
122-
version = 0.0.1
123-
long_description = file:README.rst
124-
long_description_content_type = text/x-rst
125-
"""
126-
),
127-
},
128-
)
100+
"""\
101+
Metadata-Version: 2.1
102+
Name: test-package
103+
Version: 1.2.3
104+
Description-Content-Type: text/x-rst
129105
130-
assert not check.check([sdist])
131106
132-
assert capsys.readouterr().out == f"Checking {sdist}: PASSED with warnings\n"
107+
============
133108
134-
assert caplog.record_tuples == [
135-
(
136-
"twine.commands.check",
137-
logging.WARNING,
138-
"`long_description` missing.",
139-
),
140-
]
141-
142-
143-
def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
144-
sdist = build_package(
145-
tmp_path,
146-
{
147-
"setup.cfg": (
148-
"""
149-
[metadata]
150-
name = test-package
151-
version = 0.0.1
152-
long_description = file:README.rst
153-
long_description_content_type = text/x-rst
154-
"""
155-
),
156-
"README.rst": (
157-
"""
158-
============
159-
"""
160-
),
161-
},
109+
""",
162110
)
163111

164112
assert check.check([sdist])
@@ -177,25 +125,17 @@ def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
177125

178126

179127
def test_fails_rst_no_content(tmp_path, capsys, caplog):
180-
sdist = build_package(
128+
sdist = build_sdist_with_metadata(
181129
tmp_path,
182-
{
183-
"setup.cfg": (
184-
"""
185-
[metadata]
186-
name = test-package
187-
version = 0.0.1
188-
long_description = file:README.rst
189-
long_description_content_type = text/x-rst
190-
"""
191-
),
192-
"README.rst": (
193-
"""
194-
test-package
195-
============
196-
"""
197-
),
198-
},
130+
"""\
131+
Metadata-Version: 2.1
132+
Name: test-package
133+
Version: 1.2.3
134+
Description-Content-Type: text/x-rst
135+
136+
test-package
137+
============
138+
""",
199139
)
200140

201141
assert check.check([sdist])
@@ -214,27 +154,19 @@ def test_fails_rst_no_content(tmp_path, capsys, caplog):
214154

215155

216156
def test_passes_rst_description(tmp_path, capsys, caplog):
217-
sdist = build_package(
157+
sdist = build_sdist_with_metadata(
218158
tmp_path,
219-
{
220-
"setup.cfg": (
221-
"""
222-
[metadata]
223-
name = test-package
224-
version = 0.0.1
225-
long_description = file:README.rst
226-
long_description_content_type = text/x-rst
227-
"""
228-
),
229-
"README.rst": (
230-
"""
231-
test-package
232-
============
233-
234-
A test package.
235-
"""
236-
),
237-
},
159+
"""\
160+
Metadata-Version: 2.1
161+
Name: test-package
162+
Version: 1.2.3
163+
Description-Content-Type: text/x-rst
164+
165+
test-package
166+
============
167+
168+
A test package.
169+
""",
238170
)
239171

240172
assert not check.check([sdist])
@@ -246,26 +178,18 @@ def test_passes_rst_description(tmp_path, capsys, caplog):
246178

247179
@pytest.mark.parametrize("content_type", ["text/markdown", "text/plain"])
248180
def test_passes_markdown_description(content_type, tmp_path, capsys, caplog):
249-
sdist = build_package(
181+
sdist = build_sdist_with_metadata(
250182
tmp_path,
251-
{
252-
"setup.cfg": (
253-
f"""
254-
[metadata]
255-
name = test-package
256-
version = 0.0.1
257-
long_description = file:README.md
258-
long_description_content_type = {content_type}
259-
"""
260-
),
261-
"README.md": (
262-
"""
263-
# test-package
264-
265-
A test package.
266-
"""
267-
),
268-
},
183+
f"""\
184+
Metadata-Version: 2.1
185+
Name: test-package
186+
Version: 1.2.3
187+
Description-Content-Type: {content_type}
188+
189+
# test-package
190+
191+
A test package.
192+
""",
269193
)
270194

271195
assert not check.check([sdist])

0 commit comments

Comments
 (0)