test_files.py 7.06 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
27
# -*- coding: utf-8 -*-
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",
28
    "include/pybind11/gil.h",
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    "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",
45
    "include/pybind11/detail/type_caster_base.h",
46
47
48
    "include/pybind11/detail/typeid.h",
}

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

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",
67
    "_version.pyi",
68
    "commands.py",
69
    "py.typed",
70
    "setup_helpers.py",
71
    "setup_helpers.pyi",
72
73
}

74
headers = main_headers | detail_headers | stl_headers
75
76
77
78
79
80
81
82
83
src_files = headers | cmake_files
all_files = src_files | py_files


sdist_files = {
    "pybind11",
    "pybind11/include",
    "pybind11/include/pybind11",
    "pybind11/include/pybind11/detail",
84
    "pybind11/include/pybind11/stl",
85
86
87
88
89
90
91
92
    "pybind11/share",
    "pybind11/share/cmake",
    "pybind11/share/cmake/pybind11",
    "pyproject.toml",
    "setup.cfg",
    "setup.py",
    "LICENSE",
    "MANIFEST.in",
93
    "README.rst",
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
    "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,
            "setup.py",
            "sdist",
            "--formats=tar",
            "--dist-dir",
            str(tmpdir),
        ]
    )
    if hasattr(out, "decode"):
        out = out.decode()

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

    with tarfile.open(str(sdist)) as tar:
        start = tar.getnames()[0] + "/"
        version = start[9:-1]
129
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
130
131
132
133
134
135
136
137
138
139
140

        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()

141
    files = {"pybind11/{}".format(n) for n in all_files}
142
    files |= sdist_files
143
    files |= {"pybind11{}".format(n) for n in local_sdist_files}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    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()
        )
        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 test_build_global_dist(monkeypatch, tmpdir):

    monkeypatch.chdir(MAIN_DIR)
    monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")

    out = subprocess.check_output(
        [
            sys.executable,
            "setup.py",
            "sdist",
            "--formats=tar",
            "--dist-dir",
            str(tmpdir),
        ]
    )
    if hasattr(out, "decode"):
        out = out.decode()

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

    with tarfile.open(str(sdist)) as tar:
        start = tar.getnames()[0] + "/"
        version = start[16:-1]
184
        simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
185
186
187
188
189
190
191
192
193
194
195

        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()

196
    files = {"pybind11/{}".format(n) for n in all_files}
197
    files |= sdist_files
198
    files |= {"pybind11_global{}".format(n) for n in local_sdist_files}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    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")

223
    files = {"pybind11/{}".format(n) for n in all_files}
224
225
226
227
228
229
230
231
232
233
234
235
    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()

236
237
    trimmed = {n for n in names if "dist-info" not in n}
    trimmed |= {
238
        "dist-info/{}".format(n.split("/", 1)[-1]) for n in names if "dist-info" in n
239
    }
240
241
242
243
244
245
246
247
248
249
250
251
252
    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")

253
254
    files = {"data/data/{}".format(n) for n in src_files}
    files |= {"data/headers/{}".format(n[8:]) for n in headers}
255
256
257
258
259
260
261
262
263
264
265
266
    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]
267
    trimmed = {n[len(beginning) + 1 :] for n in names}
268
269

    assert files == trimmed