Skip to content

Commit 67badd9

Browse files
committed
fix: support negative envvar values correctly
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 01a79df commit 67badd9

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

docs/overrides.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ you can set any value `tool.scikit-build` supports, and it will override if the
1111
There are three types of conditions. Booleans, strings, and version numbers.
1212
Booleans take a bool; if the boolean matches the bool you give, the override
1313
matches. If the value is a string (such as an environment variable), it will
14-
match truth-like values. Strings take a regex which will try to match. Version
15-
numbers take a specifier set, like `>=1.0`.
14+
match non false-like values, and if the variable is unset or empty, that counts
15+
as false. Strings take a regex which will try to match. Version numbers take
16+
a specifier set, like `>=1.0`.
1617

1718
If multiple conditions are given, they all must be true. Use `if.any` (below)
1819
if you would rather matching on any one of multiple conditions being true.

src/scikit_build_core/settings/skbuild_overrides.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ def strtobool(value: str) -> bool:
3535
"""
3636
Converts a environment variable string into a boolean value.
3737
"""
38+
if not value:
39+
return False
3840
value = value.lower()
3941
if value.isdigit():
4042
return bool(int(value))
41-
return value in {"y", "yes", "on", "true", "t"}
43+
return value not in {"n", "no", "off", "false", "f"}
4244

4345

4446
def version_match(value: str, match: str, name: str) -> str:
@@ -216,13 +218,13 @@ def override_match(
216218

217219
if env:
218220
for key, value in env.items():
219-
if key not in current_env:
220-
failed_set.add(f"env.{key}")
221-
elif isinstance(value, bool):
222-
if strtobool(current_env[key]) == value:
221+
if isinstance(value, bool):
222+
if strtobool(current_env.get(key, "")) == value:
223223
passed_dict[f"env.{key}"] = f"env {key} is {value}"
224224
else:
225225
failed_set.add(f"env.{key}")
226+
elif key not in current_env:
227+
failed_set.add(f"env.{key}")
226228
else:
227229
current_value = current_env[key]
228230
match_msg = regex_match(current_value, value)

tests/test_settings_overrides.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,36 @@ def test_skbuild_env_bool(
414414
assert not settings.sdist.cmake
415415

416416

417+
@pytest.mark.parametrize("envvar", ["random", "FalSE", "", "0", None])
418+
def test_skbuild_env_negative_bool(
419+
envvar: str | None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
420+
):
421+
if envvar is None:
422+
monkeypatch.delenv("FOO", raising=False)
423+
else:
424+
monkeypatch.setenv("FOO", envvar)
425+
426+
pyproject_toml = tmp_path / "pyproject.toml"
427+
pyproject_toml.write_text(
428+
dedent(
429+
"""\
430+
[[tool.scikit-build.overrides]]
431+
if.env.FOO = false
432+
sdist.cmake = true
433+
"""
434+
),
435+
encoding="utf-8",
436+
)
437+
438+
settings_reader = SettingsReader.from_file(pyproject_toml)
439+
settings = settings_reader.settings
440+
441+
if envvar in {"random"}:
442+
assert not settings.sdist.cmake
443+
else:
444+
assert settings.sdist.cmake
445+
446+
417447
@pytest.mark.parametrize("foo", ["true", "false"])
418448
@pytest.mark.parametrize("bar", ["true", "false"])
419449
@pytest.mark.parametrize("any", [True, False])

0 commit comments

Comments
 (0)