test_files.py 8.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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))

15
16
17
18
19
20
21
22
23
24
PKGCONFIG = """\
prefix=${{pcfiledir}}/../../
includedir=${{prefix}}/include

Name: pybind11
Description: Seamless operability between C++11 and Python
Version: {VERSION}
Cflags: -I${{includedir}}
"""

25
26
27
28
29
30
31
32
33
34
35
36

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",
37
    "include/pybind11/gil.h",
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    "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",
54
    "include/pybind11/detail/type_caster_base.h",
55
56
57
    "include/pybind11/detail/typeid.h",
}

58
59
60
61
62
eigen_headers = {
    "include/pybind11/eigen/matrix.h",
    "include/pybind11/eigen/tensor.h",
}

63
64
65
66
stl_headers = {
    "include/pybind11/stl/filesystem.h",
}

67
68
69
70
71
72
73
74
75
76
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",
}

77
78
79
80
pkgconfig_files = {
    "share/pkgconfig/pybind11.pc",
}

81
82
83
84
85
py_files = {
    "__init__.py",
    "__main__.py",
    "_version.py",
    "commands.py",
86
    "py.typed",
87
88
89
    "setup_helpers.py",
}

90
headers = main_headers | detail_headers | eigen_headers | stl_headers
91
src_files = headers | cmake_files | pkgconfig_files
92
93
94
95
96
97
98
99
all_files = src_files | py_files


sdist_files = {
    "pybind11",
    "pybind11/include",
    "pybind11/include/pybind11",
    "pybind11/include/pybind11/detail",
100
    "pybind11/include/pybind11/eigen",
101
    "pybind11/include/pybind11/stl",
102
103
104
    "pybind11/share",
    "pybind11/share/cmake",
    "pybind11/share/cmake/pybind11",
105
    "pybind11/share/pkgconfig",
106
107
108
109
110
    "pyproject.toml",
    "setup.cfg",
    "setup.py",
    "LICENSE",
    "MANIFEST.in",
111
    "README.rst",
112
113
114
115
116
117
118
119
120
121
122
123
124
    "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",
}


125
126
127
128
129
130
131
132
133
134
135
136
def read_tz_file(tar: tarfile.TarFile, name: str) -> bytes:
    start = tar.getnames()[0] + "/"
    inner_file = tar.extractfile(tar.getmember(f"{start}{name}"))
    assert inner_file
    with contextlib.closing(inner_file) as f:
        return f.read()


def normalize_line_endings(value: bytes) -> bytes:
    return value.replace(os.linesep.encode("utf-8"), b"\n")


137
138
139
def test_build_sdist(monkeypatch, tmpdir):
    monkeypatch.chdir(MAIN_DIR)

140
141
    subprocess.run(
        [sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}"], check=True
142
143
    )

144
    (sdist,) = tmpdir.visit("*.tar.gz")
145

146
    with tarfile.open(str(sdist), "r:gz") as tar:
147
148
        start = tar.getnames()[0] + "/"
        version = start[9:-1]
149
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
150

151
152
153
154
155
156
157
158
159
160
161
        setup_py = read_tz_file(tar, "setup.py")
        pyproject_toml = read_tz_file(tar, "pyproject.toml")
        pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
        cmake_cfg = read_tz_file(
            tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
        )

    assert (
        'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
        in cmake_cfg.decode("utf-8")
    )
162

163
    files = {f"pybind11/{n}" for n in all_files}
164
    files |= sdist_files
165
    files |= {f"pybind11{n}" for n in local_sdist_files}
166
167
168
169
170
171
    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 = (
172
            string.Template(f.read().decode("utf-8"))
173
            .substitute(version=version, extra_cmd="")
174
            .encode("utf-8")
175
        )
176
    assert setup_py == contents
177
178
179

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

182
183
184
185
    simple_version = ".".join(version.split(".")[:3])
    pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
    assert normalize_line_endings(pkgconfig) == pkgconfig_expected

186
187
188
189

def test_build_global_dist(monkeypatch, tmpdir):
    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
190
191
    subprocess.run(
        [sys.executable, "-m", "build", "--sdist", "--outdir", str(tmpdir)], check=True
192
    )
193
194

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

196
    with tarfile.open(str(sdist), "r:gz") as tar:
197
198
        start = tar.getnames()[0] + "/"
        version = start[16:-1]
199
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
200

201
202
203
204
205
206
        setup_py = read_tz_file(tar, "setup.py")
        pyproject_toml = read_tz_file(tar, "pyproject.toml")
        pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
        cmake_cfg = read_tz_file(
            tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
        )
207

208
209
210
211
    assert (
        'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
        in cmake_cfg.decode("utf-8")
    )
212

213
    files = {f"pybind11/{n}" for n in all_files}
214
    files |= sdist_files
215
    files |= {f"pybind11_global{n}" for n in local_sdist_files}
216
217
218
219
220
221
    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="")
222
            .encode("utf-8")
223
224
225
226
227
228
229
        )
        assert setup_py == contents

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

230
231
232
233
    simple_version = ".".join(version.split(".")[:3])
    pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
    assert normalize_line_endings(pkgconfig) == pkgconfig_expected

234
235
236
237

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

238
239
    subprocess.run(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
240
241
242
243
    )

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

244
    files = {f"pybind11/{n}" for n in all_files}
245
246
247
248
249
250
251
252
253
254
255
256
    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()

257
    trimmed = {n for n in names if "dist-info" not in n}
258
    trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
259
260
261
262
263
264
265
    assert files == trimmed


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

266
267
    subprocess.run(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
268
269
270
271
    )

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

272
273
    files = {f"data/data/{n}" for n in src_files}
    files |= {f"data/headers/{n[8:]}" for n in headers}
274
275
276
277
278
279
280
281
282
283
284
285
    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]
286
    trimmed = {n[len(beginning) + 1 :] for n in names}
287
288

    assert files == trimmed