mirror of
https://github.com/esphome/esphome.git
synced 2026-08-24 07:06:20 +00:00
[core] Parse the common on/off spellings in boolean env vars
This commit is contained in:
+2
-2
@@ -398,9 +398,9 @@ def get_bool_env(var, default=False):
|
||||
value = os.getenv(var, default)
|
||||
if isinstance(value, str):
|
||||
value = value.lower()
|
||||
if value in ["1", "true"]:
|
||||
if value in ("1", "true", "yes", "on", "enable"):
|
||||
return True
|
||||
if value in ["0", "false"]:
|
||||
if value in ("0", "false", "no", "off", "disable"):
|
||||
return False
|
||||
return bool(value)
|
||||
|
||||
|
||||
@@ -1108,3 +1108,24 @@ def test_progressbar_enabled_on_pipe_with_dashboard(monkeypatch) -> None:
|
||||
def test_format_duration(seconds: float, expected: str) -> None:
|
||||
"""Test that durations are rendered as short human-readable strings."""
|
||||
assert helpers.format_duration(seconds) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
("yes", True),
|
||||
("on", True),
|
||||
("enable", True),
|
||||
("no", False),
|
||||
("off", False),
|
||||
("disable", False),
|
||||
],
|
||||
)
|
||||
def test_get_bool_env_common_spellings(
|
||||
monkeypatch: pytest.MonkeyPatch, value: str, expected: bool
|
||||
) -> None:
|
||||
"""The common on/off spellings parse instead of falling through to
|
||||
bool(str), which read "off" as enabled (e.g. IDF_CCACHE_ENABLE=off
|
||||
left ccache on)."""
|
||||
monkeypatch.setenv("SOME_KNOB", value)
|
||||
assert helpers.get_bool_env("SOME_KNOB") is expected
|
||||
|
||||
Reference in New Issue
Block a user