test_files.py 7.95 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
stl_headers = {
    "include/pybind11/stl/filesystem.h",
}

62
63
64
65
66
67
68
69
70
71
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",
}

72
73
74
75
pkgconfig_files = {
    "share/pkgconfig/pybind11.pc",
}

76
77
78
79
80
py_files = {
    "__init__.py",
    "__main__.py",
    "_version.py",
    "commands.py",
81
    "py.typed",
82
83
84
    "setup_helpers.py",
}

85
headers = main_headers | detail_headers | stl_headers
86
src_files = headers | cmake_files | pkgconfig_files
87
88
89
90
91
92
93
94
all_files = src_files | py_files


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


119
120
121
122
123
124
125
126
127
128
129
130
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")


131
132
133
134
def test_build_sdist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)

135
136
    subprocess.run(
        [sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}"], check=True
137
138
    )

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

141
    with tarfile.open(str(sdist), "r:gz") as tar:
142
143
        start = tar.getnames()[0] + "/"
        version = start[9:-1]
144
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
145

146
147
148
149
150
151
152
153
154
155
156
        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")
    )
157

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

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

177
178
179
180
    simple_version = ".".join(version.split(".")[:3])
    pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
    assert normalize_line_endings(pkgconfig) == pkgconfig_expected

181
182
183
184
185

def test_build_global_dist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
186
187
    subprocess.run(
        [sys.executable, "-m", "build", "--sdist", "--outdir", str(tmpdir)], check=True
188
    )
189
190

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

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

197
198
199
200
201
202
        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"
        )
203

204
205
206
207
    assert (
        'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
        in cmake_cfg.decode("utf-8")
    )
208

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

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

226
227
228
229
    simple_version = ".".join(version.split(".")[:3])
    pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
    assert normalize_line_endings(pkgconfig) == pkgconfig_expected

230
231
232
233

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

234
235
    subprocess.run(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
236
237
238
239
    )

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

240
    files = {f"pybind11/{n}" for n in all_files}
241
242
243
244
245
246
247
248
249
250
251
252
    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()

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


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

262
263
    subprocess.run(
        [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
264
265
266
267
    )

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

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

    assert files == trimmed