| 1 |
# These tests check the sdist, path, and wheel of build to ensure that all are valid. |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import tarfile |
| 8 |
import zipfile |
| 9 |
|
| 10 |
from pathlib import Path, PurePosixPath |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
|
| 15 |
DIR = Path(__file__).parent.resolve() |
| 16 |
MAIN_DIR = DIR.parent |
| 17 |
|
| 18 |
|
| 19 |
sdist_files = { |
| 20 |
'.dockerignore', |
| 21 |
'.gitignore', |
| 22 |
'CHANGELOG.rst', |
| 23 |
'LICENSE', |
| 24 |
'PKG-INFO', |
| 25 |
'README.md', |
| 26 |
'docs/conf.py', |
| 27 |
'pyproject.toml', |
| 28 |
'src/build/py.typed', |
| 29 |
'tests/constraints.txt', |
| 30 |
'tests/packages/test-cant-build-via-sdist/some-file-that-is-needed-for-build.txt', |
| 31 |
'tests/packages/test-no-project/empty.txt', |
| 32 |
'tests/packages/test-setuptools/MANIFEST.in', |
| 33 |
'tox.toml', |
| 34 |
} |
| 35 |
|
| 36 |
sdist_patterns = { |
| 37 |
'docs/*.rst', |
| 38 |
'docs/*/*.rst', |
| 39 |
'docs/_static/*', |
| 40 |
'src/build/*.py', |
| 41 |
'src/build/_compat/*.py', |
| 42 |
'tests/*.py', |
| 43 |
'tests/packages/*/*.py', |
| 44 |
'tests/packages/*/*/*.py', |
| 45 |
'tests/packages/*/pyproject.toml', |
| 46 |
'tests/packages/*/setup.*', |
| 47 |
} |
| 48 |
|
| 49 |
sdist_files |= {str(PurePosixPath(p.relative_to(MAIN_DIR))) for path in sdist_patterns for p in MAIN_DIR.glob(path)} |
| 50 |
|
| 51 |
wheel_files = { |
| 52 |
'build/__init__.py', |
| 53 |
'build/__main__.py', |
| 54 |
'build/_builder.py', |
| 55 |
'build/_compat/__init__.py', |
| 56 |
'build/_compat/importlib.py', |
| 57 |
'build/_compat/tarfile.py', |
| 58 |
'build/_compat/tomllib.py', |
| 59 |
'build/_ctx.py', |
| 60 |
'build/_exceptions.py', |
| 61 |
'build/_types.py', |
| 62 |
'build/_util.py', |
| 63 |
'build/env.py', |
| 64 |
'build/py.typed', |
| 65 |
'build/util.py', |
| 66 |
'dist-info/licenses/LICENSE', |
| 67 |
'dist-info/METADATA', |
| 68 |
'dist-info/RECORD', |
| 69 |
'dist-info/WHEEL', |
| 70 |
'dist-info/entry_points.txt', |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.network |
| 75 |
def test_build_sdist(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 76 |
monkeypatch.chdir(MAIN_DIR) |
| 77 |
|
| 78 |
subprocess.run( |
| 79 |
[ |
| 80 |
sys.executable, |
| 81 |
'-m', |
| 82 |
'build', |
| 83 |
'--sdist', |
| 84 |
'--outdir', |
| 85 |
str(tmp_path), |
| 86 |
], |
| 87 |
check=True, |
| 88 |
) |
| 89 |
|
| 90 |
(sdist,) = tmp_path.glob('*.tar.gz') |
| 91 |
|
| 92 |
with tarfile.open(str(sdist), 'r:gz') as tar: |
| 93 |
simpler = {n.split('/', 1)[-1] for n in tar.getnames() if '/__pycache__/' not in n} |
| 94 |
|
| 95 |
assert simpler == sdist_files |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.network |
| 99 |
@pytest.mark.parametrize('args', [(), ('--wheel',)], ids=('from_sdist', 'direct')) |
| 100 |
def test_build_wheel(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, args: tuple[str, ...]) -> None: |
| 101 |
monkeypatch.chdir(MAIN_DIR) |
| 102 |
|
| 103 |
subprocess.run( |
| 104 |
[ |
| 105 |
sys.executable, |
| 106 |
'-m', |
| 107 |
'build', |
| 108 |
*args, |
| 109 |
'--outdir', |
| 110 |
str(tmp_path), |
| 111 |
], |
| 112 |
check=True, |
| 113 |
) |
| 114 |
|
| 115 |
(wheel,) = tmp_path.glob('*.whl') |
| 116 |
|
| 117 |
with zipfile.ZipFile(str(wheel)) as z: |
| 118 |
names = z.namelist() |
| 119 |
|
| 120 |
trimmed = {n for n in names if 'dist-info' not in n} |
| 121 |
trimmed |= {f'dist-info/{n.split("/", 1)[-1]}' for n in names if 'dist-info' in n} |
| 122 |
|
| 123 |
assert trimmed == wheel_files |