|
| 1 | +""" |
| 2 | +Tests for packaging artifacts: sdist and wheel. |
| 3 | +This test was mostly generated using AI assistance. |
| 4 | +""" |
| 5 | + |
| 6 | +import subprocess |
| 7 | +import tarfile |
| 8 | +import zipfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def test_packaging_artifacts(tmp_path: Path) -> None: |
| 13 | + """Build sdist and wheel, then verify their contents.""" |
| 14 | + dist_dir = tmp_path / "dist" |
| 15 | + |
| 16 | + # Build the package |
| 17 | + subprocess.run( |
| 18 | + f"pdm build --dest {dist_dir!s}", |
| 19 | + check=True, |
| 20 | + capture_output=True, |
| 21 | + shell=True, |
| 22 | + ) |
| 23 | + |
| 24 | + # 1. Check sdist |
| 25 | + sdists = list(dist_dir.glob("*.tar.gz")) |
| 26 | + assert len(sdists) == 1, "Expected exactly one sdist (.tar.gz)" |
| 27 | + |
| 28 | + with tarfile.open(sdists[0], "r:gz") as tar: |
| 29 | + sdist_members = tar.getnames() |
| 30 | + |
| 31 | + # Normalize paths: remove the top-level directory (e.g. getmac-1.0.0/) |
| 32 | + sdist_files = [] |
| 33 | + for m in sdist_members: |
| 34 | + parts = m.split("/", 1) |
| 35 | + if len(parts) > 1: |
| 36 | + sdist_files.append(parts[1]) |
| 37 | + else: |
| 38 | + sdist_files.append(m) |
| 39 | + |
| 40 | + # Forbidden files/directories that should never be in the distribution |
| 41 | + forbidden_starts = [ |
| 42 | + ".github", |
| 43 | + ".git", |
| 44 | + ".vscode", |
| 45 | + ".idea", |
| 46 | + "build", |
| 47 | + "dist", |
| 48 | + "venv", |
| 49 | + ".venv", |
| 50 | + ".pdm-python", |
| 51 | + "__pycache__", |
| 52 | + ] |
| 53 | + |
| 54 | + for f in sdist_files: |
| 55 | + for bad in forbidden_starts: |
| 56 | + assert not f.startswith(bad), f"File '{f}' should not be in sdist" |
| 57 | + |
| 58 | + # Essential files that MUST be in sdist |
| 59 | + expected_in_sdist = [ |
| 60 | + "pyproject.toml", |
| 61 | + "README.md", |
| 62 | + "LICENSE", |
| 63 | + "CHANGELOG.md", |
| 64 | + ] |
| 65 | + for expected in expected_in_sdist: |
| 66 | + assert expected in sdist_files, f"'{expected}' missing from sdist" |
| 67 | + |
| 68 | + # Ensure source code and tests are present in sdist |
| 69 | + assert any(f.startswith("getmac/") for f in sdist_files), "Source code missing from sdist" |
| 70 | + assert any(f.startswith("tests/") for f in sdist_files), "Tests missing from sdist" |
| 71 | + |
| 72 | + # 2. Check wheel |
| 73 | + wheels = list(dist_dir.glob("*.whl")) |
| 74 | + assert len(wheels) == 1, "Expected exactly one wheel (.whl)" |
| 75 | + |
| 76 | + with zipfile.ZipFile(wheels[0], "r") as z: |
| 77 | + wheel_files = z.namelist() |
| 78 | + |
| 79 | + # Tests should NOT be in wheel (assuming standard practice for this lib) |
| 80 | + for f in wheel_files: |
| 81 | + assert not f.startswith("tests/"), f"File '{f}' (tests) should not be in wheel" |
| 82 | + for bad in forbidden_starts: |
| 83 | + assert not f.startswith(bad), f"File '{f}' should not be in wheel" |
| 84 | + |
| 85 | + # Ensure package is in wheel |
| 86 | + assert any(f.startswith("getmac/") for f in wheel_files), "Source code missing from wheel" |
| 87 | + assert any(f.endswith(".dist-info/METADATA") for f in wheel_files), ( |
| 88 | + "Metadata missing from wheel" |
| 89 | + ) |
0 commit comments