test_files.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import contextlib
4 import os
5 import string
6 import subprocess
7 import sys
8 import tarfile
9 import zipfile
10 
11 # These tests must be run explicitly
12 # They require CMake 3.15+ (--install)
13 
14 DIR = os.path.abspath(os.path.dirname(__file__))
15 MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
16 
17 PKGCONFIG = """\
18 prefix=${{pcfiledir}}/../../
19 includedir=${{prefix}}/include
20 
21 Name: pybind11
22 Description: Seamless operability between C++11 and Python
23 Version: {VERSION}
24 Cflags: -I${{includedir}}
25 """
26 
27 
28 main_headers = {
29  "include/pybind11/attr.h",
30  "include/pybind11/buffer_info.h",
31  "include/pybind11/cast.h",
32  "include/pybind11/chrono.h",
33  "include/pybind11/common.h",
34  "include/pybind11/complex.h",
35  "include/pybind11/eigen.h",
36  "include/pybind11/embed.h",
37  "include/pybind11/eval.h",
38  "include/pybind11/functional.h",
39  "include/pybind11/gil.h",
40  "include/pybind11/gil_safe_call_once.h",
41  "include/pybind11/iostream.h",
42  "include/pybind11/numpy.h",
43  "include/pybind11/operators.h",
44  "include/pybind11/options.h",
45  "include/pybind11/pybind11.h",
46  "include/pybind11/pytypes.h",
47  "include/pybind11/stl.h",
48  "include/pybind11/stl_bind.h",
49  "include/pybind11/type_caster_pyobject_ptr.h",
50  "include/pybind11/typing.h",
51 }
52 
53 detail_headers = {
54  "include/pybind11/detail/class.h",
55  "include/pybind11/detail/common.h",
56  "include/pybind11/detail/descr.h",
57  "include/pybind11/detail/init.h",
58  "include/pybind11/detail/internals.h",
59  "include/pybind11/detail/type_caster_base.h",
60  "include/pybind11/detail/typeid.h",
61 }
62 
63 eigen_headers = {
64  "include/pybind11/eigen/common.h",
65  "include/pybind11/eigen/matrix.h",
66  "include/pybind11/eigen/tensor.h",
67 }
68 
69 stl_headers = {
70  "include/pybind11/stl/filesystem.h",
71 }
72 
73 cmake_files = {
74  "share/cmake/pybind11/FindPythonLibsNew.cmake",
75  "share/cmake/pybind11/pybind11Common.cmake",
76  "share/cmake/pybind11/pybind11Config.cmake",
77  "share/cmake/pybind11/pybind11ConfigVersion.cmake",
78  "share/cmake/pybind11/pybind11GuessPythonExtSuffix.cmake",
79  "share/cmake/pybind11/pybind11NewTools.cmake",
80  "share/cmake/pybind11/pybind11Targets.cmake",
81  "share/cmake/pybind11/pybind11Tools.cmake",
82 }
83 
84 pkgconfig_files = {
85  "share/pkgconfig/pybind11.pc",
86 }
87 
88 py_files = {
89  "__init__.py",
90  "__main__.py",
91  "_version.py",
92  "commands.py",
93  "py.typed",
94  "setup_helpers.py",
95 }
96 
97 headers = main_headers | detail_headers | eigen_headers | stl_headers
98 src_files = headers | cmake_files | pkgconfig_files
99 all_files = src_files | py_files
100 
101 
102 sdist_files = {
103  "pybind11",
104  "pybind11/include",
105  "pybind11/include/pybind11",
106  "pybind11/include/pybind11/detail",
107  "pybind11/include/pybind11/eigen",
108  "pybind11/include/pybind11/stl",
109  "pybind11/share",
110  "pybind11/share/cmake",
111  "pybind11/share/cmake/pybind11",
112  "pybind11/share/pkgconfig",
113  "pyproject.toml",
114  "setup.cfg",
115  "setup.py",
116  "LICENSE",
117  "MANIFEST.in",
118  "README.rst",
119  "PKG-INFO",
120  "SECURITY.md",
121 }
122 
123 local_sdist_files = {
124  ".egg-info",
125  ".egg-info/PKG-INFO",
126  ".egg-info/SOURCES.txt",
127  ".egg-info/dependency_links.txt",
128  ".egg-info/not-zip-safe",
129  ".egg-info/top_level.txt",
130 }
131 
132 
133 def read_tz_file(tar: tarfile.TarFile, name: str) -> bytes:
134  start = tar.getnames()[0] + "/"
135  inner_file = tar.extractfile(tar.getmember(f"{start}{name}"))
136  assert inner_file
137  with contextlib.closing(inner_file) as f:
138  return f.read()
139 
140 
141 def normalize_line_endings(value: bytes) -> bytes:
142  return value.replace(os.linesep.encode("utf-8"), b"\n")
143 
144 
145 def test_build_sdist(monkeypatch, tmpdir):
146  monkeypatch.chdir(MAIN_DIR)
147 
148  subprocess.run(
149  [sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}"], check=True
150  )
151 
152  (sdist,) = tmpdir.visit("*.tar.gz")
153 
154  with tarfile.open(str(sdist), "r:gz") as tar:
155  start = tar.getnames()[0] + "/"
156  version = start[9:-1]
157  simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
158 
159  setup_py = read_tz_file(tar, "setup.py")
160  pyproject_toml = read_tz_file(tar, "pyproject.toml")
161  pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
162  cmake_cfg = read_tz_file(
163  tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
164  )
165 
166  assert (
167  'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
168  in cmake_cfg.decode("utf-8")
169  )
170 
171  files = {f"pybind11/{n}" for n in all_files}
172  files |= sdist_files
173  files |= {f"pybind11{n}" for n in local_sdist_files}
174  files.add("pybind11.egg-info/entry_points.txt")
175  files.add("pybind11.egg-info/requires.txt")
176  assert simpler == files
177 
178  with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
179  contents = (
180  string.Template(f.read().decode("utf-8"))
181  .substitute(version=version, extra_cmd="")
182  .encode("utf-8")
183  )
184  assert setup_py == contents
185 
186  with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
187  contents = f.read()
188  assert pyproject_toml == contents
189 
190  simple_version = ".".join(version.split(".")[:3])
191  pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
192  assert normalize_line_endings(pkgconfig) == pkgconfig_expected
193 
194 
195 def test_build_global_dist(monkeypatch, tmpdir):
196  monkeypatch.chdir(MAIN_DIR)
197  monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
198  subprocess.run(
199  [sys.executable, "-m", "build", "--sdist", "--outdir", str(tmpdir)], check=True
200  )
201 
202  (sdist,) = tmpdir.visit("*.tar.gz")
203 
204  with tarfile.open(str(sdist), "r:gz") as tar:
205  start = tar.getnames()[0] + "/"
206  version = start[16:-1]
207  simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
208 
209  setup_py = read_tz_file(tar, "setup.py")
210  pyproject_toml = read_tz_file(tar, "pyproject.toml")
211  pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
212  cmake_cfg = read_tz_file(
213  tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
214  )
215 
216  assert (
217  'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
218  in cmake_cfg.decode("utf-8")
219  )
220 
221  files = {f"pybind11/{n}" for n in all_files}
222  files |= sdist_files
223  files |= {f"pybind11_global{n}" for n in local_sdist_files}
224  assert simpler == files
225 
226  with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f:
227  contents = (
228  string.Template(f.read().decode())
229  .substitute(version=version, extra_cmd="")
230  .encode("utf-8")
231  )
232  assert setup_py == contents
233 
234  with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
235  contents = f.read()
236  assert pyproject_toml == contents
237 
238  simple_version = ".".join(version.split(".")[:3])
239  pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
240  assert normalize_line_endings(pkgconfig) == pkgconfig_expected
241 
242 
243 def tests_build_wheel(monkeypatch, tmpdir):
244  monkeypatch.chdir(MAIN_DIR)
245 
246  subprocess.run(
247  [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
248  )
249 
250  (wheel,) = tmpdir.visit("*.whl")
251 
252  files = {f"pybind11/{n}" for n in all_files}
253  files |= {
254  "dist-info/LICENSE",
255  "dist-info/METADATA",
256  "dist-info/RECORD",
257  "dist-info/WHEEL",
258  "dist-info/entry_points.txt",
259  "dist-info/top_level.txt",
260  }
261 
262  with zipfile.ZipFile(str(wheel)) as z:
263  names = z.namelist()
264 
265  trimmed = {n for n in names if "dist-info" not in n}
266  trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
267  assert files == trimmed
268 
269 
270 def tests_build_global_wheel(monkeypatch, tmpdir):
271  monkeypatch.chdir(MAIN_DIR)
272  monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
273 
274  subprocess.run(
275  [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
276  )
277 
278  (wheel,) = tmpdir.visit("*.whl")
279 
280  files = {f"data/data/{n}" for n in src_files}
281  files |= {f"data/headers/{n[8:]}" for n in headers}
282  files |= {
283  "dist-info/LICENSE",
284  "dist-info/METADATA",
285  "dist-info/WHEEL",
286  "dist-info/top_level.txt",
287  "dist-info/RECORD",
288  }
289 
290  with zipfile.ZipFile(str(wheel)) as z:
291  names = z.namelist()
292 
293  beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
294  trimmed = {n[len(beginning) + 1 :] for n in names}
295 
296  assert files == trimmed
test_files.test_build_sdist
def test_build_sdist(monkeypatch, tmpdir)
Definition: test_files.py:145
encode
int encode(Index i, Index j)
Definition: indexed_view.cpp:45
test_files.tests_build_global_wheel
def tests_build_global_wheel(monkeypatch, tmpdir)
Definition: test_files.py:270
test_files.test_build_global_dist
def test_build_global_dist(monkeypatch, tmpdir)
Definition: test_files.py:195
test_files.tests_build_wheel
def tests_build_wheel(monkeypatch, tmpdir)
Definition: test_files.py:243
decode
IndexPair decode(Index ij)
Definition: indexed_view.cpp:49
str
Definition: pytypes.h:1558
test_files.normalize_line_endings
bytes normalize_line_endings(bytes value)
Definition: test_files.py:141
gtsam::split
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Definition: graph-inl.h:245
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
test_files.read_tz_file
bytes read_tz_file(tarfile.TarFile tar, str name)
Definition: test_files.py:133


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:15:58