Skip to content

Commit 0adaa83

Browse files
committed
Add tests for Dynamic core metadata for setup.cfg
1 parent fad8675 commit 0adaa83

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

setuptools/tests/test_core_metadata.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from email.message import EmailMessage, Message
99
from email.parser import Parser
1010
from email.policy import EmailPolicy
11+
from inspect import cleandoc
1112
from pathlib import Path
1213
from unittest.mock import Mock
1314

@@ -411,6 +412,72 @@ def test_equivalent_output(self, tmp_path, dist):
411412
_assert_roundtrip_message(pkg_info)
412413

413414

415+
class TestPEP643:
416+
STATIC_CONFIG = {
417+
"setup.cfg": cleandoc(
418+
"""
419+
[metadata]
420+
name = package
421+
version = 0.0.1
422+
author = Foo Bar
423+
author_email = [email protected]
424+
long_description = Long
425+
description
426+
description = Short description
427+
keywords = one, two
428+
[options]
429+
install_requires = requests
430+
"""
431+
),
432+
}
433+
434+
@pytest.mark.parametrize("file", STATIC_CONFIG.keys())
435+
def test_static_config_has_no_dynamic(self, file, tmpdir_cwd):
436+
Path(file).write_text(self.STATIC_CONFIG[file], encoding="utf-8")
437+
metadata = _get_metadata()
438+
assert metadata.get_all("Dynamic") is None
439+
assert metadata.get_all("dynamic") is None
440+
441+
@pytest.mark.parametrize("file", STATIC_CONFIG.keys())
442+
@pytest.mark.parametrize(
443+
"fields",
444+
[
445+
# Single dynamic field
446+
{"requires-python": ("python_requires", ">=3.12")},
447+
{"author-email": ("author_email", "[email protected]")},
448+
{"keywords": ("keywords", ["hello", "world"])},
449+
# Multiple dynamic fields
450+
{
451+
"summary": ("description", "hello world"),
452+
"description": ("long_description", "bla bla bla bla"),
453+
"requires-dist": ("install_requires", ["hello-world"]),
454+
},
455+
],
456+
)
457+
def test_modified_fields_marked_as_dynamic(self, file, fields, tmpdir_cwd):
458+
# We start with a static config
459+
Path(file).write_text(self.STATIC_CONFIG[file], encoding="utf-8")
460+
dist = _makedist()
461+
462+
# ... but then we simulate the effects of a plugin modifying the distribution
463+
for attr, value in fields.values():
464+
# `dist` and `dist.metadata` are complicated...
465+
# Some attributes work when set on `dist`, others on `dist.metadata`...
466+
# Here we set in both just in case (this also avoids calling `_finalize_*`)
467+
setattr(dist, attr, value)
468+
setattr(dist.metadata, attr, value)
469+
470+
# Then we should be able to list the modified fields as Dynamic
471+
metadata = _get_metadata(dist)
472+
assert set(metadata.get_all("Dynamic")) == set(fields)
473+
474+
475+
def _makedist(**attrs):
476+
dist = Distribution(attrs)
477+
dist.parse_config_files()
478+
return dist
479+
480+
414481
def _assert_roundtrip_message(metadata: str) -> None:
415482
"""Emulate the way wheel.bdist_wheel parses and regenerates the message,
416483
then ensures the metadata generated by setuptools is compatible.
@@ -482,6 +549,10 @@ def _get_pkginfo(dist: Distribution):
482549
return fp.getvalue()
483550

484551

552+
def _get_metadata(dist: Distribution | None = None):
553+
return message_from_string(_get_pkginfo(dist or _makedist()))
554+
555+
485556
def _valid_metadata(text: str) -> bool:
486557
metadata = Metadata.from_email(text, validate=True) # can raise exceptions
487558
return metadata is not None

0 commit comments

Comments
 (0)