|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tarfile |
| 6 | +from pathlib import Path |
| 7 | +from zipfile import ZipFile |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +LICENSE_EXPRESSION = "LicenseRef-python-hwpx-NonCommercial" |
| 13 | + |
| 14 | + |
| 15 | +def _build_distribution(tmp_path: Path, distribution: str) -> Path: |
| 16 | + pytest.importorskip("build") |
| 17 | + |
| 18 | + project_root = Path(__file__).resolve().parents[1] |
| 19 | + build_args = [ |
| 20 | + sys.executable, |
| 21 | + "-m", |
| 22 | + "build", |
| 23 | + f"--{distribution}", |
| 24 | + "--outdir", |
| 25 | + str(tmp_path), |
| 26 | + ] |
| 27 | + subprocess.run(build_args, cwd=project_root, check=True) |
| 28 | + |
| 29 | + pattern = "*.whl" if distribution == "wheel" else "*.tar.gz" |
| 30 | + return next(tmp_path.glob(pattern)) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("distribution", ["wheel", "sdist"]) |
| 34 | +def test_built_distributions_expose_custom_license_metadata( |
| 35 | + tmp_path: Path, distribution: str |
| 36 | +) -> None: |
| 37 | + artifact = _build_distribution(tmp_path, distribution) |
| 38 | + |
| 39 | + if distribution == "wheel": |
| 40 | + with ZipFile(artifact) as wheel_archive: |
| 41 | + members = set(wheel_archive.namelist()) |
| 42 | + metadata_name = next( |
| 43 | + name for name in members if name.endswith(".dist-info/METADATA") |
| 44 | + ) |
| 45 | + metadata = wheel_archive.read(metadata_name).decode("utf-8") |
| 46 | + |
| 47 | + assert f"License-Expression: {LICENSE_EXPRESSION}" in metadata |
| 48 | + assert "License-File: LICENSE" in metadata |
| 49 | + assert "Classifier: License ::" not in metadata |
| 50 | + assert any(name.endswith(".dist-info/licenses/LICENSE") for name in members) |
| 51 | + return |
| 52 | + |
| 53 | + with tarfile.open(artifact, "r:gz") as sdist_archive: |
| 54 | + members = sdist_archive.getnames() |
| 55 | + pkg_info_name = next(name for name in members if name.endswith("/PKG-INFO")) |
| 56 | + pkg_info_member = sdist_archive.extractfile(pkg_info_name) |
| 57 | + assert pkg_info_member is not None |
| 58 | + metadata = pkg_info_member.read().decode("utf-8") |
| 59 | + |
| 60 | + assert f"License-Expression: {LICENSE_EXPRESSION}" in metadata |
| 61 | + assert "License-File: LICENSE" in metadata |
| 62 | + assert "Classifier: License ::" not in metadata |
| 63 | + assert any(name.endswith("/LICENSE") for name in members) |
0 commit comments