Skip to content

Commit

Permalink
Simplify generation of test packages used in test_check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnicolodi committed Dec 19, 2024
1 parent 8b9552b commit 1d76b1c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 143 deletions.
200 changes: 62 additions & 138 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import textwrap

import build
import pretend
import pytest

Expand Down Expand Up @@ -50,45 +48,30 @@ def test_fails_no_distributions(caplog):
]


def build_package(src_path, project_files, distribution="sdist"):
"""
Build a source distribution similar to `python3 -m build --sdist`.
Returns the absolute path of the built distribution.
"""
project_files = {
"pyproject.toml": (
"""
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
"""
),
**project_files,
}

for filename, content in project_files.items():
(src_path / filename).write_text(textwrap.dedent(content))

builder = build.ProjectBuilder(src_path)
return builder.build(distribution, str(src_path / "dist"))
def build_sdist_with_metadata(path, metadata):
name = "test"
version = "1.2.3"
sdist = helpers.build_archive(
path,
f"{name}-{version}",
"tar.gz",
{
f"{name}-{version}/README": "README",
f"{name}-{version}/PKG-INFO": metadata,
},
)
return str(sdist)


@pytest.mark.parametrize("distribution", ["sdist", "wheel"])
@pytest.mark.parametrize("strict", [False, True])
def test_warns_missing_description(distribution, strict, tmp_path, capsys, caplog):
sdist = build_package(
def test_warns_missing_description(strict, tmp_path, capsys, caplog):
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
"""
),
},
distribution=distribution,
"""\
Metadata-Version: 2.1
Name: test
Version: 1.2.3
""",
)

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


def test_warns_missing_file(tmp_path, capsys, caplog):
sdist = build_package(
def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
},
)
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst
assert not check.check([sdist])
assert capsys.readouterr().out == f"Checking {sdist}: PASSED with warnings\n"
============
assert caplog.record_tuples == [
(
"twine.commands.check",
logging.WARNING,
"`long_description` missing.",
),
]


def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
sdist = build_package(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
============
"""
),
},
""",
)

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


def test_fails_rst_no_content(tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
test-package
============
"""
),
},
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst
test-package
============
""",
)

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


def test_passes_rst_description(tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
test-package
============
A test package.
"""
),
},
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst
test-package
============
A test package.
""",
)

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

@pytest.mark.parametrize("content_type", ["text/markdown", "text/plain"])
def test_passes_markdown_description(content_type, tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
f"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.md
long_description_content_type = {content_type}
"""
),
"README.md": (
"""
# test-package
A test package.
"""
),
},
f"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: {content_type}
# test-package
A test package.
""",
)

assert not check.check([sdist])
Expand Down
5 changes: 0 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ deps =
pretend
pytest
pytest-socket
build
coverage
# Needed on 3.12 and newer due to setuptools not being pre-installed
# in fresh venvs.
# See: https://github.com/python/cpython/issues/95299
setuptools
passenv =
PYTEST_ADDOPTS
setenv =
Expand Down

0 comments on commit 1d76b1c

Please sign in to comment.