summaryrefslogtreecommitdiffstats
path: root/tests/test_self_packaging.py
blob: abe09f85be4ae7d54a42738ec85953469c73f24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# These tests check the sdist, path, and wheel of build to ensure that all are valid.

import subprocess
import sys
import tarfile
import zipfile

from pathlib import Path

import pytest


DIR = Path(__file__).parent.resolve()
MAIN_DIR = DIR.parent

sdist_files = {
    'LICENSE',
    'PKG-INFO',
    'README.md',
    'pyproject.toml',
    'setup.cfg',
    'setup.py',
    'src',
    'src/build',
    'src/build.egg-info',
    'src/build.egg-info/PKG-INFO',
    'src/build.egg-info/SOURCES.txt',
    'src/build.egg-info/dependency_links.txt',
    'src/build.egg-info/entry_points.txt',
    'src/build.egg-info/requires.txt',
    'src/build.egg-info/top_level.txt',
    'src/build/__init__.py',
    'src/build/__main__.py',
    'src/build/env.py',
    'src/build/py.typed',
    'src/build/util.py',
}

wheel_files = {
    'build/__init__.py',
    'build/__main__.py',
    'build/env.py',
    'build/py.typed',
    'build/util.py',
    'dist-info/LICENSE',
    'dist-info/METADATA',
    'dist-info/RECORD',
    'dist-info/WHEEL',
    'dist-info/entry_points.txt',
    'dist-info/top_level.txt',
}


def test_build_sdist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)

    subprocess.run(
        [
            sys.executable,
            '-m',
            'build',
            '--sdist',
            '--outdir',
            str(tmpdir),
        ],
        check=True,
    ).stdout

    (sdist,) = tmpdir.visit('*.tar.gz')

    with tarfile.open(str(sdist), 'r:gz') as tar:
        simpler = {n.split('/', 1)[-1] for n in tar.getnames()[1:]}

    assert simpler == sdist_files


@pytest.mark.parametrize('args', ((), ('--wheel',)), ids=('from_sdist', 'direct'))
def test_build_wheel(monkeypatch, tmpdir, args):

    monkeypatch.chdir(MAIN_DIR)

    subprocess.run(
        [
            sys.executable,
            '-m',
            'build',
            *args,
            '--outdir',
            str(tmpdir),
        ],
        check=True,
    )

    (wheel,) = tmpdir.visit('*.whl')

    with zipfile.ZipFile(str(wheel)) as z:
        names = z.namelist()

    trimmed = {n for n in names if 'dist-info' not in n}
    trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if 'dist-info' in n}

    assert trimmed == wheel_files