Skip to content

Commit a2504cc

Browse files
committed
Make fail a disallow_hard_code
Add `test_disallow_hardcoded` to cover these type of settings Signed-off-by: Cristian Le <[email protected]>
1 parent 9d3ab1d commit a2504cc

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/scikit_build_core/settings/skbuild_model.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,12 @@ class ScikitBuildSettings:
416416
The build directory. Defaults to a temporary directory, but can be set.
417417
"""
418418

419-
fail: bool = False
419+
fail: Optional[bool] = dataclasses.field(
420+
default=None,
421+
metadata=SettingsFieldMetadata(
422+
disallow_hard_code=True,
423+
),
424+
)
420425
"""
421426
Immediately fail the build. This is only useful in overrides.
422427
"""

tests/test_settings_overrides.py

+48
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)