Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,3 @@ filterwarnings=
# Ignore warnings we cannot do anything about:
# https://github.com/pypa/setuptools/pull/5042#issuecomment-2981138461
ignore:Couldn't import C tracer:coverage.exceptions.CoverageWarning

# https://github.com/pypa/setuptools/issues/5167
ignore:The 'wheel.metadata' package has been made private:DeprecationWarning
120 changes: 24 additions & 96 deletions setuptools/tests/test_core_metadata.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

import functools
import importlib
import io
from email import message_from_string
from email.generator import Generator
from email.message import EmailMessage, Message
from email.message import EmailMessage
from email.parser import Parser
from email.policy import EmailPolicy
from inspect import cleandoc
Expand All @@ -15,11 +14,9 @@
import jaraco.path
import pytest
from packaging.metadata import Metadata
from packaging.requirements import Requirement

from setuptools import _reqs, sic
from setuptools import sic
from setuptools._core_metadata import rfc822_escape, rfc822_unescape
from setuptools.command.egg_info import egg_info, write_requirements
from setuptools.config import expand, setupcfg
from setuptools.dist import Distribution

Expand Down Expand Up @@ -384,36 +381,32 @@ def dist(self, request, monkeypatch, tmp_path):
yield setupcfg.apply_configuration(Distribution({}), config)

@pytest.mark.uses_network
def test_equivalent_output(self, tmp_path, dist):
"""Ensure output from setuptools is equivalent to the one from `pypa/wheel`"""
# Generate a METADATA file using pypa/wheel for comparison
wheel_metadata = importlib.import_module("wheel.metadata")
pkginfo_to_metadata = getattr(wheel_metadata, "pkginfo_to_metadata", None)

if pkginfo_to_metadata is None: # pragma: nocover
pytest.xfail(
"wheel.metadata.pkginfo_to_metadata is undefined, "
"(this is likely to be caused by API changes in pypa/wheel"
)

# Generate an simplified "egg-info" dir for pypa/wheel to convert
def test_pkg_info_roundtrip(self, tmp_path, dist):
"""Ensure PKG-INFO round trips according to pypa/wheel's methodology"""
# Generate an simplified "egg-info" with PKG-INFO
pkg_info = _get_pkginfo(dist)
egg_info_dir = tmp_path / "pkg.egg-info"
egg_info_dir.mkdir(parents=True)
(egg_info_dir / "PKG-INFO").write_text(pkg_info, encoding="utf-8")
write_requirements(egg_info(dist), egg_info_dir, egg_info_dir / "requires.txt")

# Get pypa/wheel generated METADATA but normalize requirements formatting
metadata_msg = pkginfo_to_metadata(egg_info_dir, egg_info_dir / "PKG-INFO")
metadata_str = _normalize_metadata(metadata_msg)
pkg_info_msg = message_from_string(pkg_info)
pkg_info_str = _normalize_metadata(pkg_info_msg)
# Emulate the way old versions of wheel.bdist_wheel used to parse and regenerate
# the message, then ensures the metadata generated by setuptools is compatible.
with io.StringIO(pkg_info) as buffer:
msg = Parser(EmailMessage).parse(buffer)

# Compare setuptools PKG-INFO x pypa/wheel METADATA
assert metadata_str == pkg_info_str
serialization_policy = EmailPolicy(
utf8=True,
mangle_from_=False,
max_line_length=0,
)
with io.BytesIO() as buffer:
out = io.TextIOWrapper(buffer, encoding="utf-8")
Generator(out, policy=serialization_policy).flatten(msg)
out.flush()
regenerated = buffer.getvalue()

# Make sure it parses/serializes well in pypa/wheel
_assert_roundtrip_message(pkg_info)
raw_metadata = bytes(pkg_info, "utf-8")
# Normalise newlines to avoid test errors on Windows:
raw_metadata = b"\n".join(raw_metadata.splitlines())
regenerated = b"\n".join(regenerated.splitlines())
assert regenerated == raw_metadata


class TestPEP643:
Expand Down Expand Up @@ -542,71 +535,6 @@ def _makedist(**attrs):
return dist


def _assert_roundtrip_message(metadata: str) -> None:
"""Emulate the way wheel.bdist_wheel parses and regenerates the message,
then ensures the metadata generated by setuptools is compatible.
"""
with io.StringIO(metadata) as buffer:
msg = Parser(EmailMessage).parse(buffer)

serialization_policy = EmailPolicy(
utf8=True,
mangle_from_=False,
max_line_length=0,
)
with io.BytesIO() as buffer:
out = io.TextIOWrapper(buffer, encoding="utf-8")
Generator(out, policy=serialization_policy).flatten(msg)
out.flush()
regenerated = buffer.getvalue()

raw_metadata = bytes(metadata, "utf-8")
# Normalise newlines to avoid test errors on Windows:
raw_metadata = b"\n".join(raw_metadata.splitlines())
regenerated = b"\n".join(regenerated.splitlines())
assert regenerated == raw_metadata


def _normalize_metadata(msg: Message) -> str:
"""Allow equivalent metadata to be compared directly"""
# The main challenge regards the requirements and extras.
# Both setuptools and wheel already apply some level of normalization
# but they differ regarding which character is chosen, according to the
# following spec it should be "-":
# https://packaging.python.org/en/latest/specifications/name-normalization/

# Related issues:
# https://github.com/pypa/packaging/issues/845
# https://github.com/pypa/packaging/issues/644#issuecomment-2429813968

extras = {x.replace("_", "-"): x for x in msg.get_all("Provides-Extra", [])}
reqs = [
_normalize_req(req, extras)
for req in _reqs.parse(msg.get_all("Requires-Dist", []))
]
del msg["Requires-Dist"]
del msg["Provides-Extra"]

# Ensure consistent ord
for req in sorted(reqs):
msg["Requires-Dist"] = req
for extra in sorted(extras):
msg["Provides-Extra"] = extra

# TODO: Handle lack of PEP 643 implementation in pypa/wheel?
del msg["Metadata-Version"]

return msg.as_string()


def _normalize_req(req: Requirement, extras: dict[str, str]) -> str:
"""Allow equivalent requirement objects to be compared directly"""
as_str = str(req).replace(req.name, req.name.replace("_", "-"))
for norm, orig in extras.items():
as_str = as_str.replace(orig, norm)
return as_str


def _get_pkginfo(dist: Distribution):
with io.StringIO() as fp:
dist.metadata.write_pkg_file(fp)
Expand Down
Loading