-
Notifications
You must be signed in to change notification settings - Fork 53
/
test_settings_overrides.py
427 lines (359 loc) · 12.6 KB
/
test_settings_overrides.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from __future__ import annotations
import typing
from textwrap import dedent
import pytest
from scikit_build_core.settings.skbuild_read_settings import SettingsReader, regex_match
if typing.TYPE_CHECKING:
from pathlib import Path
@pytest.mark.parametrize("python_version", ["3.9", "3.10"])
def test_skbuild_overrides_pyver(
python_version: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setattr("sys.version_info", (*map(int, python_version.split(".")), 0))
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if = {python-version = ">=3.10"}
cmake.args = ["-DFOO=BAR"]
experimental = true
cmake.define.SPAM = "EGGS"
sdist.cmake = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if python_version == "3.10":
assert settings.cmake.args == ["-DFOO=BAR"]
assert settings.cmake.define == {"SPAM": "EGGS"}
assert settings.experimental
assert settings.sdist.cmake
else:
assert not settings.cmake.args
assert not settings.cmake.define
assert not settings.experimental
assert not settings.sdist.cmake
@pytest.mark.parametrize("implementation_name", ["cpython", "pypy"])
@pytest.mark.parametrize("platform_system", ["darwin", "linux"])
def test_skbuild_overrides_dual(
implementation_name: str,
platform_system: str,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(
"sys.implementation", type("Mock", (), {"name": implementation_name})
)
monkeypatch.setattr("sys.platform", platform_system)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if = {implementation-name = "pypy", platform-system = "darwin"}
editable.verbose = false
install.components = ["headers"]
[[tool.scikit-build.overrides]]
if.implementation-name = "cpython"
if.platform-system = "darwin"
install.components = ["bindings"]
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if implementation_name == "pypy" and platform_system == "darwin":
assert not settings.editable.verbose
assert settings.install.components == ["headers"]
elif implementation_name == "cpython" and platform_system == "darwin":
assert settings.editable.verbose
assert settings.install.components == ["bindings"]
else:
assert settings.editable.verbose
assert not settings.install.components
@pytest.mark.parametrize("implementation_name", ["cpython", "pypy"])
@pytest.mark.parametrize("platform_system", ["darwin", "linux"])
def test_skbuild_overrides_any(
implementation_name: str,
platform_system: str,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(
"sys.implementation", type("Mock", (), {"name": implementation_name})
)
monkeypatch.setattr("sys.platform", platform_system)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if.any = {implementation-name = "pypy", platform-system = "darwin"}
editable.verbose = false
install.components = ["headers"]
[[tool.scikit-build.overrides]]
if.any.implementation-name = "cpython"
if.any.platform-system = "darwin"
install.components = ["bindings"]
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if implementation_name == "cpython" or platform_system == "darwin":
assert settings.editable.verbose == (
platform_system == "linux" and implementation_name == "cpython"
)
assert settings.install.components == ["bindings"]
elif implementation_name == "pypy" or platform_system == "darwin":
assert not settings.editable.verbose
assert settings.install.components == ["headers"]
else:
assert settings.editable.verbose
assert not settings.install.components
@pytest.mark.parametrize("python_version", ["3.9", "3.10"])
@pytest.mark.parametrize("implementation_name", ["cpython", "pypy"])
@pytest.mark.parametrize("platform_system", ["darwin", "linux"])
def test_skbuild_overrides_any_mixed(
implementation_name: str,
platform_system: str,
python_version: str,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(
"sys.implementation", type("Mock", (), {"name": implementation_name})
)
monkeypatch.setattr("sys.platform", platform_system)
monkeypatch.setattr("sys.version_info", (*map(int, python_version.split(".")), 0))
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if.any = {implementation-name = "pypy", platform-system = "darwin"}
if.python-version = ">=3.10"
editable.verbose = false
install.components = ["headers"]
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if python_version == "3.10" and (
implementation_name == "pypy" or platform_system == "darwin"
):
assert not settings.editable.verbose
assert settings.install.components == ["headers"]
else:
assert settings.editable.verbose
assert not settings.install.components
@pytest.mark.parametrize("platform_node", ["thismatch", "matchthat"])
def test_skbuild_overrides_platnode(
platform_node: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setattr("platform.node", lambda: platform_node)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if.platform-node = "^match"
experimental = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if platform_node == "matchthat":
assert settings.experimental
else:
assert not settings.experimental
@pytest.mark.parametrize("platform_machine", ["x86_64", "x86_32", "other"])
def test_skbuild_overrides_regex(
platform_machine: str,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr("platform.machine", lambda: platform_machine)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[tool.scikit-build]
install.components = ["default"]
[[tool.scikit-build.overrides]]
if = {platform_machine = "x86_.*"}
install.components = ["headers"]
[[tool.scikit-build.overrides]]
if = {platform_machine = "x86_32"}
install.components = ["headers_32"]
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if platform_machine == "x86_64":
assert settings.install.components == ["headers"]
elif platform_machine == "x86_32":
assert settings.install.components == ["headers_32"]
else:
assert settings.install.components == ["default"]
def test_skbuild_overrides_no_if(
tmp_path: Path,
):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
minimum-version="0.1"
"""
),
encoding="utf-8",
)
with pytest.raises(KeyError, match="At least one 'if' override must be provided"):
SettingsReader.from_file(pyproject_toml, {})
def test_skbuild_overrides_empty_if(
tmp_path: Path,
):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if = {}
minimum-version="0.1"
"""
),
encoding="utf-8",
)
with pytest.raises(KeyError, match="At least one 'if' override must be provided"):
SettingsReader.from_file(pyproject_toml, {})
def test_skbuild_overrides_invalid_key(
tmp_path: Path,
):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if = {python_version = ">=3"}
invalid-key = "Hi"
"""
),
encoding="utf-8",
)
settings = SettingsReader.from_file(pyproject_toml, {})
with pytest.raises(SystemExit):
settings.validate_may_exit()
@pytest.mark.parametrize("regex", ["is", "this", "^this", "string$"])
def test_regex_match(regex: str):
assert regex_match("this_is_a_string", regex)
@pytest.mark.parametrize("regex", ["^string", "this$", "other"])
def test_not_regex_match(regex: str):
assert not regex_match("this_is_a_string", regex)
@pytest.mark.parametrize("envvar", ["BAR", "", None])
def test_skbuild_env(
envvar: str | None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
if envvar is None:
monkeypatch.delenv("FOO", raising=False)
else:
monkeypatch.setenv("FOO", envvar)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if.env.FOO = "BAR"
sdist.cmake = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if envvar == "BAR":
assert settings.sdist.cmake
else:
assert not settings.sdist.cmake
@pytest.mark.parametrize("envvar", ["tRUE", "3", "0", "", None])
def test_skbuild_env_bool(
envvar: str | None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
if envvar is None:
monkeypatch.delenv("FOO", raising=False)
else:
monkeypatch.setenv("FOO", envvar)
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[[tool.scikit-build.overrides]]
if.env.FOO = true
sdist.cmake = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if envvar in {"tRUE", "3"}:
assert settings.sdist.cmake
else:
assert not settings.sdist.cmake
@pytest.mark.parametrize("foo", ["true", "false"])
@pytest.mark.parametrize("bar", ["true", "false"])
@pytest.mark.parametrize("any", [True, False])
def test_skbuild_env_bool_all_any(
foo: str, bar: str, any: bool, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv("FOO", foo)
monkeypatch.setenv("BAR", bar)
any_str = ".any" if any else ""
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
f"""\
[[tool.scikit-build.overrides]]
if{any_str}.env.FOO = true
if{any_str}.env.BAR = true
sdist.cmake = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {})
settings = settings_reader.settings
if (foo == "true" and bar == "true") or any and (foo == "true" or bar == "true"):
assert settings.sdist.cmake
else:
assert not settings.sdist.cmake
@pytest.mark.parametrize("state", ["wheel", "sdist"])
def test_skbuild_overrides_state(state: str, tmp_path: Path):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
dedent(
f"""\
[[tool.scikit-build.overrides]]
if.state = "{state}"
experimental = true
"""
),
encoding="utf-8",
)
settings_reader = SettingsReader.from_file(pyproject_toml, {}, state="wheel")
settings = settings_reader.settings
if state == "wheel":
assert settings.experimental
else:
assert not settings.experimental