Skip to content

Commit

Permalink
Test metadata as produced by _core_metadata (#4029)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Aug 29, 2023
2 parents 39895e4 + 0f64181 commit 1ef36f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ testing-integration =
jaraco.envs>=2.2
build[virtualenv]
filelock>=3.4.0
packaging

docs =
# upstream
Expand Down
4 changes: 4 additions & 0 deletions setuptools/tests/config/test_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import pytest
from ini2toml.api import Translator
from packaging.metadata import Metadata

import setuptools # noqa ensure monkey patch to metadata
from setuptools.dist import Distribution
Expand Down Expand Up @@ -428,6 +429,9 @@ def core_metadata(dist) -> str:
dist.metadata.write_pkg_file(buffer)
pkg_file_txt = buffer.getvalue()

# Make sure core metadata is valid
Metadata.from_email(pkg_file_txt, validate=True) # can raise exceptions

skip_prefixes = ()
skip_lines = set()
# ---- DIFF NORMALISATION ----
Expand Down
17 changes: 16 additions & 1 deletion setuptools/tests/test_core_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pytest

from packaging.metadata import Metadata

from setuptools import sic, _reqs
from setuptools.dist import Distribution
from setuptools._core_metadata import rfc822_escape, rfc822_unescape
Expand Down Expand Up @@ -166,6 +168,9 @@ def test_read_metadata(name, attrs):
PKG_INFO = io.StringIO()

metadata_out.write_pkg_file(PKG_INFO)
PKG_INFO.seek(0)
pkg_info = PKG_INFO.read()
assert _valid_metadata(pkg_info)

PKG_INFO.seek(0)
metadata_in = dist_class()
Expand Down Expand Up @@ -281,9 +286,12 @@ def test_maintainer_author(name, attrs, tmpdir):
dist.metadata.write_pkg_info(fn_s)

with io.open(str(fn.join('PKG-INFO')), 'r', encoding='utf-8') as f:
raw_pkg_lines = f.readlines()
pkg_info = f.read()

assert _valid_metadata(pkg_info)

# Drop blank lines and strip lines from default description
raw_pkg_lines = pkg_info.splitlines()
pkg_lines = list(filter(None, raw_pkg_lines[:-2]))

pkg_lines_set = set(pkg_lines)
Expand Down Expand Up @@ -333,6 +341,8 @@ def test_parity_with_metadata_from_pypa_wheel(tmp_path):
dist.metadata.write_pkg_file(fp)
pkg_info = fp.getvalue()

assert _valid_metadata(pkg_info)

# Ensure Requires-Dist is present
expected = [
'Metadata-Version:',
Expand Down Expand Up @@ -378,3 +388,8 @@ def test_parity_with_metadata_from_pypa_wheel(tmp_path):
assert metadata_msg.as_string() == pkg_info_msg.as_string()
assert metadata_deps == pkg_info_deps
assert metadata_extras == pkg_info_extras


def _valid_metadata(text: str) -> bool:
metadata = Metadata.from_email(text, validate=True) # can raise exceptions
return metadata is not None
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[testenv]
deps =
# Ideally all the dependencies should be set as "extras"
# workaround for pypa/build#630
build[virtualenv] @ git+https://github.com/jaraco/build@bugfix/630-importlib-metadata
build[virtualenv] @ git+https://github.com/pypa/build@59c1f87
# ^-- pypa/build#630, use dev version while we wait for the new release
packaging @ git+https://github.com/pypa/packaging@7e68d82
# ^-- use dev version while we wait for the new release
setenv =
PYTHONWARNDEFAULTENCODING = 1
SETUPTOOLS_ENFORCE_DEPRECATION = 1
Expand Down

0 comments on commit 1ef36f2

Please sign in to comment.