-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathconftest.py
397 lines (322 loc) · 11.2 KB
/
conftest.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from __future__ import annotations
import contextlib
import dataclasses
import importlib.util
import os
import shutil
import subprocess
import sys
import sysconfig
from pathlib import Path
from typing import Any
import virtualenv as _virtualenv
if sys.version_info < (3, 8):
import importlib_metadata as metadata
from typing_extensions import Literal, overload
else:
from importlib import metadata
from typing import Literal, overload
if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib
import pytest
from packaging.requirements import Requirement
DIR = Path(__file__).parent.resolve()
BASE = DIR.parent
@pytest.fixture(scope="session")
def pep518_wheelhouse(tmp_path_factory: pytest.TempPathFactory) -> Path:
wheelhouse = tmp_path_factory.mktemp("wheelhouse")
subprocess.run(
[
sys.executable,
"-m",
"pip",
"wheel",
"--wheel-dir",
str(wheelhouse),
f"{BASE}",
],
check=True,
)
packages = [
"build",
"cython",
"hatchling",
"pip",
"pybind11",
"setuptools",
"virtualenv",
"wheel",
]
if importlib.util.find_spec("cmake") is not None:
packages.append("cmake")
if importlib.util.find_spec("ninja") is not None:
packages.append("ninja")
subprocess.run(
[
sys.executable,
"-m",
"pip",
"download",
"-q",
"-d",
str(wheelhouse),
*packages,
],
check=True,
)
return wheelhouse
class VEnv:
def __init__(self, env_dir: Path, *, wheelhouse: Path | None = None) -> None:
cmd = [str(env_dir), "--no-setuptools", "--no-wheel", "--activators", ""]
result = _virtualenv.cli_run(cmd, setup_logging=False)
self.wheelhouse = wheelhouse
self.executable = Path(result.creator.exe)
self.env_dir = env_dir.resolve()
self.platlib = Path(
self.execute("import sysconfig; print(sysconfig.get_path('platlib'))")
)
self.purelib = Path(
self.execute("import sysconfig; print(sysconfig.get_path('purelib'))")
)
if sys.version_info >= (3, 13):
self.run("pip", "install", "-U", "pip>=24.1")
@overload
def run(self, *args: str, capture: Literal[True]) -> str: ...
@overload
def run(self, *args: str, capture: Literal[False] = ...) -> None: ...
def run(self, *args: str, capture: bool = False) -> str | None:
__tracebackhide__ = True
env = os.environ.copy()
paths = {str(self.executable.parent)}
env["PATH"] = os.pathsep.join([*paths, env["PATH"]])
env["VIRTUAL_ENV"] = str(self.env_dir)
env["PIP_DISABLE_PIP_VERSION_CHECK"] = "ON"
if self.wheelhouse is not None:
env["PIP_NO_INDEX"] = "ON"
env["PIP_FIND_LINKS"] = str(self.wheelhouse)
str_args = [os.fspath(a) for a in args]
# Windows does not make a python shortcut in venv
if str_args[0] in {"python", "python3"}:
str_args[0] = str(self.executable)
if capture:
result = subprocess.run(
str_args,
check=False,
capture_output=True,
text=True,
env=env,
)
if result.returncode != 0:
print(result.stdout, file=sys.stdout)
print(result.stderr, file=sys.stderr)
print("FAILED RUN:", *str_args, file=sys.stderr)
raise SystemExit(result.returncode)
return result.stdout.strip()
result_bytes = subprocess.run(
str_args,
check=False,
env=env,
)
if result_bytes.returncode != 0:
print("FAILED RUN:", *str_args, file=sys.stderr)
raise SystemExit(result_bytes.returncode)
return None
def execute(self, command: str) -> str:
return self.run(str(self.executable), "-c", command, capture=True)
def module(self, *args: str) -> None:
return self.run(str(self.executable), "-m", *args)
def install(self, *args: str, isolated: bool = True) -> None:
isolated_flags = "" if isolated else ["--no-build-isolation"]
self.module("pip", "install", *isolated_flags, *args)
@pytest.fixture
def isolated(tmp_path: Path, pep518_wheelhouse: Path) -> VEnv:
path = tmp_path / "venv"
return VEnv(path, wheelhouse=pep518_wheelhouse)
@pytest.fixture
def virtualenv(tmp_path: Path) -> VEnv:
path = tmp_path / "venv"
return VEnv(path)
@dataclasses.dataclass(frozen=True)
class PackageInfo:
name: str
sdist_hash38: str | None = None
sdist_hash39: str | None = None
sdist_dated_hash39: str | None = None
sdist_dated_hash38: str | None = None
@property
def sdist_hash(self) -> str | None:
return self.sdist_hash38 if sys.version_info < (3, 9) else self.sdist_hash39
@property
def sdist_dated_hash(self) -> str | None:
return (
self.sdist_dated_hash38
if sys.version_info < (3, 9)
else self.sdist_dated_hash39
)
@property
def source_date_epoch(self) -> str:
return "12345"
def process_package(
package: PackageInfo, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
package_dir = tmp_path / "pkg"
shutil.copytree(DIR / "packages" / package.name, package_dir)
monkeypatch.chdir(package_dir)
# Just in case this gets littered into the source tree, clear it out
if Path("dist").is_dir():
shutil.rmtree("dist")
@pytest.fixture
def package_simple_pyproject_ext(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo(
"simple_pyproject_ext",
"5544d96810ce60ac40baf28cf8caf2e1e7e1fa7439b283d3fb52cdc1f87f12ac",
"aaa15c185dc3fbc301dc2fca937cc935442c38e55bc400fbefd424bd6ce92adb",
"ee3a564a37c42df3abdcee3862175baceeb6f6eff0b29931681b424ec5d96067",
"4c1d402621e7f00fce4ce5afdb73a9ba4cc25cd4bb57619113432841f779dd68",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_simple_pyproject_script_with_flags(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo(
"simple_pyproject_script_with_flags",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_simple_pyproject_source_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo(
"simple_pyproject_source_dir",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_simple_setuptools_ext(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo("simple_setuptools_ext")
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_mixed_setuptools(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo("mixed_setuptools")
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_filepath_pure(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo("filepath_pure")
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_dynamic_metadata(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo("dynamic_metadata")
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_hatchling(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> PackageInfo:
package = PackageInfo("hatchling")
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_simplest_c(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> PackageInfo:
package = PackageInfo(
"simplest_c",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def navigate_editable(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> PackageInfo:
package = PackageInfo(
"navigate_editable",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def broken_fallback(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> PackageInfo:
package = PackageInfo(
"broken_fallback",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_sdist_config(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo(
"sdist_config",
)
process_package(package, tmp_path, monkeypatch)
return package
@pytest.fixture
def package_simple_purelib_package(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> PackageInfo:
package = PackageInfo(
"simple_purelib_package",
)
process_package(package, tmp_path, monkeypatch)
return package
def which_mock(name: str) -> str | None:
if name in {"ninja", "ninja-build", "cmake3", "samu", "gmake", "make"}:
return None
if name == "cmake":
return "cmake/path"
return None
@pytest.fixture
def protect_get_requires(fp, monkeypatch):
"""
Protect get_requires from actually calling anything variable during tests.
"""
# This needs to be passed due to packaging.tags 22 extra checks if macos 10.16 is reported
fp.pass_command([sys.executable, fp.any()])
monkeypatch.setattr(shutil, "which", which_mock)
monkeypatch.delenv("CMAKE_GENERATOR", raising=False)
orig_find_spec = importlib.util.find_spec
def find_spec(name: str, package: str | None = None) -> Any:
if name in {"cmake", "ninja"}:
return None
return orig_find_spec(name, package)
monkeypatch.setattr(importlib.util, "find_spec", find_spec)
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
for item in items:
# Ensure all tests using virtualenv are marked as such
if "virtualenv" in getattr(item, "fixturenames", ()):
item.add_marker(pytest.mark.virtualenv)
if "isolated" in getattr(item, "fixturenames", ()):
item.add_marker(pytest.mark.virtualenv)
item.add_marker(pytest.mark.isolated)
item.add_marker(pytest.mark.network)
def pytest_report_header() -> str:
with BASE.joinpath("pyproject.toml").open("rb") as f:
pyproject = tomllib.load(f)
project = pyproject.get("project", {})
pkgs = project.get("dependencies", [])
pkgs += [p for ps in project.get("optional-dependencies", {}).values() for p in ps]
if "name" in project:
pkgs.append(project["name"])
interesting_packages = {Requirement(p).name for p in pkgs}
interesting_packages.add("pip")
valid = []
for package in sorted(interesting_packages):
with contextlib.suppress(ModuleNotFoundError):
valid.append(f"{package}=={metadata.version(package)}")
reqs = " ".join(valid)
lines = [
f"installed packages of interest: {reqs}",
f"sysconfig platform: {sysconfig.get_platform()}",
]
return "\n".join(lines)