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


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28