File: test_simplest_c.py

package info (click to toggle)
scikit-build-core 0.11.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,408 kB
  • sloc: python: 13,380; ansic: 140; cpp: 134; sh: 27; fortran: 18; makefile: 7
file content (153 lines) | stat: -rw-r--r-- 4,623 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import tarfile
import zipfile
from pathlib import Path

import pytest

from scikit_build_core.build import build_sdist, build_wheel

from pathutils import contained

DIR = Path(__file__).parent.resolve()
SIMPLEST = DIR / "packages/simplest_c"


def test_pep517_sdist(tmp_path, monkeypatch):
    dist = tmp_path.resolve() / "dist"
    monkeypatch.chdir(SIMPLEST)

    out = build_sdist(str(dist))

    (sdist,) = dist.iterdir()
    assert sdist.name == "simplest-0.0.1.tar.gz"
    assert sdist == dist / out

    with tarfile.open(sdist) as f:
        file_names = set(f.getnames())
        assert file_names == {
            f"simplest-0.0.1/{x}"
            for x in (
                "CMakeLists.txt",
                "pyproject.toml",
                ".gitignore",
                "src/module.c",
                "src/simplest/__init__.py",
                "src/simplest/_module.pyi",
                "src/simplest/data.txt",
                "src/simplest/sdist_only.txt",
                "src/not_a_package/simple.txt",
                "src/simplest/excluded.txt",
                "PKG-INFO",
            )
        }


@pytest.mark.compile
@pytest.mark.configure
@pytest.mark.parametrize(
    "component", [[], ["PythonModule"], ["PythonModule", "Generated"]]
)
def test_pep517_wheel(tmp_path, monkeypatch, virtualenv, component):
    dist = tmp_path / "dist"
    dist.mkdir()
    monkeypatch.chdir(SIMPLEST)
    out = build_wheel(str(dist), config_settings={"install.components": component})
    (wheel,) = dist.glob("simplest-0.0.1-*.whl")
    assert wheel == dist / out

    virtualenv.install(wheel)

    with zipfile.ZipFile(wheel) as zf:
        file_paths = {Path(n) for n in zf.namelist()}

    file_names = {p.parts[0] for p in file_paths}
    simplest_pkg = {p.name for p in contained(file_paths, "simplest")}
    filtered_pkg = {x for x in simplest_pkg if not x.startswith("_module")}

    if not component or "PythonModule" in component:
        assert filtered_pkg != simplest_pkg
    else:
        assert filtered_pkg == simplest_pkg

    expected_wheel_files = {
        "__init__.py",
        "data.txt",
        "excluded.txt",
        "sdist_only.txt",
    }

    if not component:
        expected_wheel_files.add("generated_ignored.txt")
        expected_wheel_files.add("generated_no_wheel.txt")

    if not component or "Generated" in component:
        expected_wheel_files.add("generated.txt")

    assert len(filtered_pkg) == len(simplest_pkg) - 2
    assert {"simplest-0.0.1.dist-info", "simplest"} == file_names
    assert expected_wheel_files == filtered_pkg
    # Note that generated_ignored.txt is here because all CMake installed files are
    # present, CMake has the final say.

    version = virtualenv.execute("from simplest import square; print(square(2))")
    assert version == "4.0"


@pytest.mark.compile
@pytest.mark.configure
def test_pep517_wheel_incexl(tmp_path, monkeypatch, virtualenv):
    dist = tmp_path / "dist"
    dist.mkdir()
    monkeypatch.chdir(SIMPLEST)

    out = build_wheel(
        str(dist),
        {
            "sdist.include": "src/simplest/*included*.txt",
            "sdist.exclude": "src/simplest/*excluded*.txt",
            "wheel.exclude": [
                "simplest/sdist_only.txt",
                "simplest/generated_no_wheel.txt",
            ],
            "wheel.packages": ["src/simplest", "src/not_a_package"],
        },
    )

    (wheel,) = dist.glob("simplest-0.0.1-*.whl")
    assert wheel == dist / out

    virtualenv.install(wheel)

    with zipfile.ZipFile(wheel) as zf:
        file_paths = {Path(n) for n in zf.namelist()}
    file_names = {p.parts[0] for p in file_paths}

    simplest_pkg = {x.name for x in contained(file_paths, "simplest")}
    not_a_pkg = {x.name for x in contained(file_paths, "not_a_package")}
    metadata_items = set(contained(file_paths, "simplest-0.0.1.dist-info"))

    assert {
        Path("licenses/LICENSE.txt"),
        Path("metadata_file.txt"),
        Path("RECORD"),
        Path("METADATA"),
        Path("WHEEL"),
    } == metadata_items

    filtered_pkg = {x for x in simplest_pkg if not x.startswith("_module")}

    assert len(filtered_pkg) == len(simplest_pkg) - 2
    assert {"simplest-0.0.1.dist-info", "simplest", "not_a_package"} == file_names
    assert {
        "__init__.py",
        "data.txt",
        "ignored_included.txt",
        "generated.txt",
        "generated_ignored.txt",
    } == filtered_pkg
    assert {"simple.txt"} == not_a_pkg

    version = virtualenv.execute(
        "from simplest import square; print(square(2))",
    )
    assert version == "4.0"