test_files.py 8.22 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
140
def test_build_sdist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)

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

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

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

152
153
154
155
156
157
158
159
160
161
162
        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")
    )
163

164
    files = {f"pybind11/{n}" for n in all_files}
165
    files |= sdist_files
166
    files |= {f"pybind11{n}" for n in local_sdist_files}
167
168
    files.add("pybind11.egg-info/entry_points.txt")
    files.add("pybind11.egg-info/requires.txt")
169
    files.add("pybind11/share/cmake/pybind11/__init__.py")
170
171
172
173
    assert simpler == files

    with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
        contents = (
174
            string.Template(f.read().decode("utf-8"))
175
            .substitute(version=version, extra_cmd="")
176
            .encode("utf-8")
177
        )
178
    assert setup_py == contents
179
180
181

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

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

188
189
190
191
192

def test_build_global_dist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
193
194
    subprocess.run(
        [sys.executable, "-m", "build", "--sdist", "--outdir", str(tmpdir)], check=True
195
    )
196
197

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

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

204
205
206
207
208
209
        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"
        )
210

211
212
213
214
    assert (
        'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
        in cmake_cfg.decode("utf-8")
    )
215

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

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

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

237
238
239
240

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

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

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

247
    files = {f"pybind11/{n}" for n in all_files}
248
249
250
251
252
253
254
255
    files |= {
        "dist-info/LICENSE",
        "dist-info/METADATA",
        "dist-info/RECORD",
        "dist-info/WHEEL",
        "dist-info/entry_points.txt",
        "dist-info/top_level.txt",
    }
256
    files.add("pybind11/share/cmake/pybind11/__init__.py")
257
258
259
260

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

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


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

270
271
    subprocess.run(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
272
273
274
275
    )

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

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

    assert files == trimmed