Skip to content

Commit 2ca55db

Browse files
committed
Simplify generation of test packages used in test_check
These tests used `build` to invoke the `setuptools.build_meta` build backend on a package directory generated on the fly. The tests are only interested in the content of the package metadata and use variations of `setup.cfg` to generate the desired metadata. This can be simplified to the direct generation of sdist archives with synthetic `PKG-INFO` metadata files. This makes the actual metadata the tests are checking obvious and avoid two test dependencies. The tests simplification highlighted that two tests are actually testing the exact same thing. Remove one.
1 parent bffd296 commit 2ca55db

File tree

2 files changed

+62
-143
lines changed

2 files changed

+62
-143
lines changed

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])

tox.ini

-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ deps =
88
pretend
99
pytest
1010
pytest-socket
11-
build
1211
coverage
13-
# Needed on 3.12 and newer due to setuptools not being pre-installed
14-
# in fresh venvs.
15-
# See: https://github.com/python/cpython/issues/95299
16-
setuptools
1712
passenv =
1813
PYTEST_ADDOPTS
1914
setenv =

0 commit comments

Comments
 (0)