test_files.py 7.25 KB
Newer Older
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
import contextlib
import os
import string
import subprocess
import sys
import tarfile
import zipfile

# These tests must be run explicitly
# They require CMake 3.15+ (--install)

DIR = os.path.abspath(os.path.dirname(__file__))
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))


main_headers = {
    "include/pybind11/attr.h",
    "include/pybind11/buffer_info.h",
    "include/pybind11/cast.h",
    "include/pybind11/chrono.h",
    "include/pybind11/common.h",
    "include/pybind11/complex.h",
    "include/pybind11/eigen.h",
    "include/pybind11/embed.h",
    "include/pybind11/eval.h",
    "include/pybind11/functional.h",
27
    "include/pybind11/gil.h",
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    "include/pybind11/iostream.h",
    "include/pybind11/numpy.h",
    "include/pybind11/operators.h",
    "include/pybind11/options.h",
    "include/pybind11/pybind11.h",
    "include/pybind11/pytypes.h",
    "include/pybind11/stl.h",
    "include/pybind11/stl_bind.h",
}

detail_headers = {
    "include/pybind11/detail/class.h",
    "include/pybind11/detail/common.h",
    "include/pybind11/detail/descr.h",
    "include/pybind11/detail/init.h",
    "include/pybind11/detail/internals.h",
44
    "include/pybind11/detail/type_caster_base.h",
45
46
47
    "include/pybind11/detail/typeid.h",
}

48
49
50
51
stl_headers = {
    "include/pybind11/stl/filesystem.h",
}

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
cmake_files = {
    "share/cmake/pybind11/FindPythonLibsNew.cmake",
    "share/cmake/pybind11/pybind11Common.cmake",
    "share/cmake/pybind11/pybind11Config.cmake",
    "share/cmake/pybind11/pybind11ConfigVersion.cmake",
    "share/cmake/pybind11/pybind11NewTools.cmake",
    "share/cmake/pybind11/pybind11Targets.cmake",
    "share/cmake/pybind11/pybind11Tools.cmake",
}

py_files = {
    "__init__.py",
    "__main__.py",
    "_version.py",
    "commands.py",
67
    "py.typed",
68
69
70
    "setup_helpers.py",
}

71
headers = main_headers | detail_headers | stl_headers
72
73
74
75
76
77
78
79
80
src_files = headers | cmake_files
all_files = src_files | py_files


sdist_files = {
    "pybind11",
    "pybind11/include",
    "pybind11/include/pybind11",
    "pybind11/include/pybind11/detail",
81
    "pybind11/include/pybind11/stl",
82
83
84
85
86
87
88
89
    "pybind11/share",
    "pybind11/share/cmake",
    "pybind11/share/cmake/pybind11",
    "pyproject.toml",
    "setup.cfg",
    "setup.py",
    "LICENSE",
    "MANIFEST.in",
90
    "README.rst",
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    "PKG-INFO",
}

local_sdist_files = {
    ".egg-info",
    ".egg-info/PKG-INFO",
    ".egg-info/SOURCES.txt",
    ".egg-info/dependency_links.txt",
    ".egg-info/not-zip-safe",
    ".egg-info/top_level.txt",
}


def test_build_sdist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)

    out = subprocess.check_output(
        [
            sys.executable,
111
112
113
114
            "-m",
            "build",
            "--sdist",
            "--outdir",
115
116
117
118
119
120
            str(tmpdir),
        ]
    )
    if hasattr(out, "decode"):
        out = out.decode()

121
    (sdist,) = tmpdir.visit("*.tar.gz")
122

123
    with tarfile.open(str(sdist), "r:gz") as tar:
124
125
        start = tar.getnames()[0] + "/"
        version = start[9:-1]
126
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
127
128
129
130
131
132
133
134
135
136
137

        with contextlib.closing(
            tar.extractfile(tar.getmember(start + "setup.py"))
        ) as f:
            setup_py = f.read()

        with contextlib.closing(
            tar.extractfile(tar.getmember(start + "pyproject.toml"))
        ) as f:
            pyproject_toml = f.read()

138
139
140
141
142
143
144
145
146
147
        with contextlib.closing(
            tar.extractfile(
                tar.getmember(
                    start + "pybind11/share/cmake/pybind11/pybind11Config.cmake"
                )
            )
        ) as f:
            contents = f.read().decode("utf8")
        assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents

148
    files = {f"pybind11/{n}" for n in all_files}
149
    files |= sdist_files
150
    files |= {f"pybind11{n}" for n in local_sdist_files}
151
152
153
154
155
156
157
158
159
160
    files.add("pybind11.egg-info/entry_points.txt")
    files.add("pybind11.egg-info/requires.txt")
    assert simpler == files

    with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
        contents = (
            string.Template(f.read().decode())
            .substitute(version=version, extra_cmd="")
            .encode()
        )
161
    assert setup_py == contents
162
163
164

    with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
        contents = f.read()
165
    assert pyproject_toml == contents
166
167
168
169
170
171
172
173
174


def test_build_global_dist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
    out = subprocess.check_output(
        [
            sys.executable,
175
176
177
178
            "-m",
            "build",
            "--sdist",
            "--outdir",
179
180
181
            str(tmpdir),
        ]
    )
182

183
184
185
    if hasattr(out, "decode"):
        out = out.decode()

186
    (sdist,) = tmpdir.visit("*.tar.gz")
187

188
    with tarfile.open(str(sdist), "r:gz") as tar:
189
190
        start = tar.getnames()[0] + "/"
        version = start[16:-1]
191
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
192
193
194
195
196
197
198
199
200
201
202

        with contextlib.closing(
            tar.extractfile(tar.getmember(start + "setup.py"))
        ) as f:
            setup_py = f.read()

        with contextlib.closing(
            tar.extractfile(tar.getmember(start + "pyproject.toml"))
        ) as f:
            pyproject_toml = f.read()

203
    files = {f"pybind11/{n}" for n in all_files}
204
    files |= sdist_files
205
    files |= {f"pybind11_global{n}" for n in local_sdist_files}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    assert simpler == files

    with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f:
        contents = (
            string.Template(f.read().decode())
            .substitute(version=version, extra_cmd="")
            .encode()
        )
        assert setup_py == contents

    with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
        contents = f.read()
        assert pyproject_toml == contents


def tests_build_wheel(monkeypatch, tmpdir):
    monkeypatch.chdir(MAIN_DIR)

    subprocess.check_output(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
    )

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

230
    files = {f"pybind11/{n}" for n in all_files}
231
232
233
234
235
236
237
238
239
240
241
242
    files |= {
        "dist-info/LICENSE",
        "dist-info/METADATA",
        "dist-info/RECORD",
        "dist-info/WHEEL",
        "dist-info/entry_points.txt",
        "dist-info/top_level.txt",
    }

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

243
    trimmed = {n for n in names if "dist-info" not in n}
244
    trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
245
246
247
248
249
250
251
252
253
254
255
256
257
    assert files == trimmed


def tests_build_global_wheel(monkeypatch, tmpdir):
    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")

    subprocess.check_output(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
    )

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

258
259
    files = {f"data/data/{n}" for n in src_files}
    files |= {f"data/headers/{n[8:]}" for n in headers}
260
261
262
263
264
265
266
267
268
269
270
271
    files |= {
        "dist-info/LICENSE",
        "dist-info/METADATA",
        "dist-info/WHEEL",
        "dist-info/top_level.txt",
        "dist-info/RECORD",
    }

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

    beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
272
    trimmed = {n[len(beginning) + 1 :] for n in names}
273
274

    assert files == trimmed