test_files.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # -*- coding: utf-8 -*-
  2. import contextlib
  3. import os
  4. import string
  5. import subprocess
  6. import sys
  7. import tarfile
  8. import zipfile
  9. # These tests must be run explicitly
  10. # They require CMake 3.15+ (--install)
  11. DIR = os.path.abspath(os.path.dirname(__file__))
  12. MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
  13. main_headers = {
  14. "include/pybind11/attr.h",
  15. "include/pybind11/buffer_info.h",
  16. "include/pybind11/cast.h",
  17. "include/pybind11/chrono.h",
  18. "include/pybind11/common.h",
  19. "include/pybind11/complex.h",
  20. "include/pybind11/eigen.h",
  21. "include/pybind11/embed.h",
  22. "include/pybind11/eval.h",
  23. "include/pybind11/functional.h",
  24. "include/pybind11/iostream.h",
  25. "include/pybind11/numpy.h",
  26. "include/pybind11/operators.h",
  27. "include/pybind11/options.h",
  28. "include/pybind11/pybind11.h",
  29. "include/pybind11/pytypes.h",
  30. "include/pybind11/stl.h",
  31. "include/pybind11/stl_bind.h",
  32. }
  33. detail_headers = {
  34. "include/pybind11/detail/class.h",
  35. "include/pybind11/detail/common.h",
  36. "include/pybind11/detail/descr.h",
  37. "include/pybind11/detail/init.h",
  38. "include/pybind11/detail/internals.h",
  39. "include/pybind11/detail/typeid.h",
  40. }
  41. cmake_files = {
  42. "share/cmake/pybind11/FindPythonLibsNew.cmake",
  43. "share/cmake/pybind11/pybind11Common.cmake",
  44. "share/cmake/pybind11/pybind11Config.cmake",
  45. "share/cmake/pybind11/pybind11ConfigVersion.cmake",
  46. "share/cmake/pybind11/pybind11NewTools.cmake",
  47. "share/cmake/pybind11/pybind11Targets.cmake",
  48. "share/cmake/pybind11/pybind11Tools.cmake",
  49. }
  50. py_files = {
  51. "__init__.py",
  52. "__main__.py",
  53. "_version.py",
  54. "_version.pyi",
  55. "commands.py",
  56. "py.typed",
  57. "setup_helpers.py",
  58. "setup_helpers.pyi",
  59. }
  60. headers = main_headers | detail_headers
  61. src_files = headers | cmake_files
  62. all_files = src_files | py_files
  63. sdist_files = {
  64. "pybind11",
  65. "pybind11/include",
  66. "pybind11/include/pybind11",
  67. "pybind11/include/pybind11/detail",
  68. "pybind11/share",
  69. "pybind11/share/cmake",
  70. "pybind11/share/cmake/pybind11",
  71. "pyproject.toml",
  72. "setup.cfg",
  73. "setup.py",
  74. "LICENSE",
  75. "MANIFEST.in",
  76. "README.rst",
  77. "PKG-INFO",
  78. }
  79. local_sdist_files = {
  80. ".egg-info",
  81. ".egg-info/PKG-INFO",
  82. ".egg-info/SOURCES.txt",
  83. ".egg-info/dependency_links.txt",
  84. ".egg-info/not-zip-safe",
  85. ".egg-info/top_level.txt",
  86. }
  87. def test_build_sdist(monkeypatch, tmpdir):
  88. monkeypatch.chdir(MAIN_DIR)
  89. out = subprocess.check_output(
  90. [
  91. sys.executable,
  92. "setup.py",
  93. "sdist",
  94. "--formats=tar",
  95. "--dist-dir",
  96. str(tmpdir),
  97. ]
  98. )
  99. if hasattr(out, "decode"):
  100. out = out.decode()
  101. (sdist,) = tmpdir.visit("*.tar")
  102. with tarfile.open(str(sdist)) as tar:
  103. start = tar.getnames()[0] + "/"
  104. version = start[9:-1]
  105. simpler = set(n.split("/", 1)[-1] for n in tar.getnames()[1:])
  106. with contextlib.closing(
  107. tar.extractfile(tar.getmember(start + "setup.py"))
  108. ) as f:
  109. setup_py = f.read()
  110. with contextlib.closing(
  111. tar.extractfile(tar.getmember(start + "pyproject.toml"))
  112. ) as f:
  113. pyproject_toml = f.read()
  114. files = set("pybind11/{}".format(n) for n in all_files)
  115. files |= sdist_files
  116. files |= set("pybind11{}".format(n) for n in local_sdist_files)
  117. files.add("pybind11.egg-info/entry_points.txt")
  118. files.add("pybind11.egg-info/requires.txt")
  119. assert simpler == files
  120. with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
  121. contents = (
  122. string.Template(f.read().decode())
  123. .substitute(version=version, extra_cmd="")
  124. .encode()
  125. )
  126. assert setup_py == contents
  127. with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
  128. contents = f.read()
  129. assert pyproject_toml == contents
  130. def test_build_global_dist(monkeypatch, tmpdir):
  131. monkeypatch.chdir(MAIN_DIR)
  132. monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
  133. out = subprocess.check_output(
  134. [
  135. sys.executable,
  136. "setup.py",
  137. "sdist",
  138. "--formats=tar",
  139. "--dist-dir",
  140. str(tmpdir),
  141. ]
  142. )
  143. if hasattr(out, "decode"):
  144. out = out.decode()
  145. (sdist,) = tmpdir.visit("*.tar")
  146. with tarfile.open(str(sdist)) as tar:
  147. start = tar.getnames()[0] + "/"
  148. version = start[16:-1]
  149. simpler = set(n.split("/", 1)[-1] for n in tar.getnames()[1:])
  150. with contextlib.closing(
  151. tar.extractfile(tar.getmember(start + "setup.py"))
  152. ) as f:
  153. setup_py = f.read()
  154. with contextlib.closing(
  155. tar.extractfile(tar.getmember(start + "pyproject.toml"))
  156. ) as f:
  157. pyproject_toml = f.read()
  158. files = set("pybind11/{}".format(n) for n in all_files)
  159. files |= sdist_files
  160. files |= set("pybind11_global{}".format(n) for n in local_sdist_files)
  161. assert simpler == files
  162. with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f:
  163. contents = (
  164. string.Template(f.read().decode())
  165. .substitute(version=version, extra_cmd="")
  166. .encode()
  167. )
  168. assert setup_py == contents
  169. with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
  170. contents = f.read()
  171. assert pyproject_toml == contents
  172. def tests_build_wheel(monkeypatch, tmpdir):
  173. monkeypatch.chdir(MAIN_DIR)
  174. subprocess.check_output(
  175. [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
  176. )
  177. (wheel,) = tmpdir.visit("*.whl")
  178. files = set("pybind11/{}".format(n) for n in all_files)
  179. files |= {
  180. "dist-info/LICENSE",
  181. "dist-info/METADATA",
  182. "dist-info/RECORD",
  183. "dist-info/WHEEL",
  184. "dist-info/entry_points.txt",
  185. "dist-info/top_level.txt",
  186. }
  187. with zipfile.ZipFile(str(wheel)) as z:
  188. names = z.namelist()
  189. trimmed = set(n for n in names if "dist-info" not in n)
  190. trimmed |= set(
  191. "dist-info/{}".format(n.split("/", 1)[-1]) for n in names if "dist-info" in n
  192. )
  193. assert files == trimmed
  194. def tests_build_global_wheel(monkeypatch, tmpdir):
  195. monkeypatch.chdir(MAIN_DIR)
  196. monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
  197. subprocess.check_output(
  198. [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
  199. )
  200. (wheel,) = tmpdir.visit("*.whl")
  201. files = set("data/data/{}".format(n) for n in src_files)
  202. files |= set("data/headers/{}".format(n[8:]) for n in headers)
  203. files |= {
  204. "dist-info/LICENSE",
  205. "dist-info/METADATA",
  206. "dist-info/WHEEL",
  207. "dist-info/top_level.txt",
  208. "dist-info/RECORD",
  209. }
  210. with zipfile.ZipFile(str(wheel)) as z:
  211. names = z.namelist()
  212. beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
  213. trimmed = set(n[len(beginning) + 1 :] for n in names)
  214. assert files == trimmed