Skip to content

Commit 2ef008e

Browse files
LecrisUTjcfr
authored andcommitted
Make fail a disallow_hard_code
Add `test_disallow_hardcoded` to cover these type of settings Signed-off-by: Cristian Le <git@lecris.dev>
1 parent 3472dc5 commit 2ef008e

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ minimum-version = "0.11" # current version
287287
build-dir = ""
288288

289289
# Immediately fail the build. This is only useful in overrides.
290-
fail = false
290+
fail = ""
291291

292292
```
293293

docs/reference/configs.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ print(mk_skbuild_docs())
7878
```{eval-rst}
7979
.. confval:: fail
8080
:type: ``bool``
81-
:default: false
8281
8382
Immediately fail the build. This is only useful in overrides.
8483
```

src/scikit_build_core/resources/scikit-build.schema.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@
491491
},
492492
"fail": {
493493
"type": "boolean",
494-
"default": false,
495494
"description": "Immediately fail the build. This is only useful in overrides."
496495
},
497496
"overrides": {

src/scikit_build_core/settings/skbuild_model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,12 @@ class ScikitBuildSettings:
507507
This can be set to reuse the build directory from previous runs.
508508
"""
509509

510-
fail: bool = False
510+
fail: Optional[bool] = dataclasses.field(
511+
default=None,
512+
metadata=SettingsFieldMetadata(
513+
disallow_hard_code=True,
514+
),
515+
)
511516
"""
512517
Immediately fail the build. This is only useful in overrides.
513518
"""

tests/test_settings_overrides.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
import sysconfig
45
import typing
56
from pathlib import Path
@@ -22,6 +23,53 @@ class VersionInfo(typing.NamedTuple):
2223
releaselevel: str = "final"
2324

2425

26+
def test_disallow_hardcoded(
27+
tmp_path: Path,
28+
caplog: pytest.LogCaptureFixture,
29+
capsys: pytest.CaptureFixture[str],
30+
):
31+
caplog.set_level(logging.WARNING)
32+
pyproject_toml = tmp_path / "pyproject.toml"
33+
template = dedent(
34+
"""\
35+
[tool.scikit-build]
36+
strict-config = {strict_config}
37+
fail = false
38+
"""
39+
)
40+
41+
# First check without strict-config to make sure all fields are disallowed
42+
strict_config = "false"
43+
pyproject_toml.write_text(
44+
template.format(strict_config=strict_config),
45+
encoding="utf-8",
46+
)
47+
48+
settings_reader = SettingsReader.from_file(pyproject_toml)
49+
settings_reader.validate_may_exit()
50+
assert caplog.records
51+
for idx, key in enumerate(["fail"]):
52+
assert (
53+
f"{key} is not allowed to be hard-coded in the pyproject.toml file"
54+
in str(caplog.records[idx].msg)
55+
)
56+
57+
# Next check that this exits if string-config is set
58+
strict_config = "true"
59+
pyproject_toml.write_text(
60+
template.format(strict_config=strict_config),
61+
encoding="utf-8",
62+
)
63+
# Flush the capsys just in case
64+
capsys.readouterr()
65+
settings_reader = SettingsReader.from_file(pyproject_toml)
66+
with pytest.raises(SystemExit) as exc:
67+
settings_reader.validate_may_exit()
68+
assert exc.value.code == 7
69+
out, _ = capsys.readouterr()
70+
assert "is not allowed to be hard-coded in the pyproject.toml file" in out
71+
72+
2573
@pytest.mark.parametrize("python_version", ["3.9", "3.10"])
2674
def test_skbuild_overrides_pyver(
2775
python_version: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch

0 commit comments

Comments
 (0)