|
8 | 8 | from email.message import EmailMessage, Message
|
9 | 9 | from email.parser import Parser
|
10 | 10 | from email.policy import EmailPolicy
|
| 11 | +from inspect import cleandoc |
11 | 12 | from pathlib import Path
|
12 | 13 | from unittest.mock import Mock
|
13 | 14 |
|
@@ -411,6 +412,72 @@ def test_equivalent_output(self, tmp_path, dist):
|
411 | 412 | _assert_roundtrip_message(pkg_info)
|
412 | 413 |
|
413 | 414 |
|
| 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 | + |
| 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 | + |
414 | 481 | def _assert_roundtrip_message(metadata: str) -> None:
|
415 | 482 | """Emulate the way wheel.bdist_wheel parses and regenerates the message,
|
416 | 483 | then ensures the metadata generated by setuptools is compatible.
|
@@ -482,6 +549,10 @@ def _get_pkginfo(dist: Distribution):
|
482 | 549 | return fp.getvalue()
|
483 | 550 |
|
484 | 551 |
|
| 552 | +def _get_metadata(dist: Distribution | None = None): |
| 553 | + return message_from_string(_get_pkginfo(dist or _makedist())) |
| 554 | + |
| 555 | + |
485 | 556 | def _valid_metadata(text: str) -> bool:
|
486 | 557 | metadata = Metadata.from_email(text, validate=True) # can raise exceptions
|
487 | 558 | return metadata is not None
|
0 commit comments