mirror of
https://github.com/esphome/esphome.git
synced 2026-08-29 17:16:45 +00:00
Name the env boolean spelling tables like device-builder does
parse_enable_env's inline tuples become TRUTHY_ENV_STRINGS and FALSY_ENV_STRINGS frozensets mirroring cv.boolean's spellings (enable and disable included) plus the 1/0 env convention, matching device-builder's TRUTHY_BOOL_STRINGS pattern.
This commit is contained in:
@@ -11,6 +11,10 @@ from esphome.framework_helpers import strip_win_long_path_prefix, tool_version_r
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# esphome cv.boolean's spelling tables plus the 1/0 env convention
|
||||
TRUTHY_ENV_STRINGS = frozenset({"1", "true", "yes", "on", "enable"})
|
||||
FALSY_ENV_STRINGS = frozenset({"0", "false", "no", "off", "disable"})
|
||||
|
||||
|
||||
def _ccache_runs(ccache: str) -> bool:
|
||||
"""Return True when the ``ccache`` found on PATH actually runs."""
|
||||
@@ -31,9 +35,9 @@ def parse_enable_env(name: str) -> bool | None:
|
||||
if raw is None:
|
||||
return None
|
||||
lowered = raw.strip().lower()
|
||||
if lowered in ("1", "true", "yes", "on"):
|
||||
if lowered in TRUTHY_ENV_STRINGS:
|
||||
return True
|
||||
if lowered in ("0", "false", "no", "off"):
|
||||
if lowered in FALSY_ENV_STRINGS:
|
||||
return False
|
||||
_LOGGER.warning("Ignoring unrecognized %s=%r; use 1 or 0", name, raw)
|
||||
return None
|
||||
|
||||
@@ -97,3 +97,23 @@ def test_resolve_unrecognized_value_warns_and_probes(
|
||||
assert ccache.resolve_ccache_path() is None
|
||||
mock_probe.assert_called_once()
|
||||
assert "unrecognized ESPHOME_CCACHE_ENABLE" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("raw", "expected"),
|
||||
[
|
||||
("1", True),
|
||||
("enable", True),
|
||||
("ON", True),
|
||||
("0", False),
|
||||
("disable", False),
|
||||
("Off", False),
|
||||
("maybe", None),
|
||||
],
|
||||
)
|
||||
def test_parse_enable_env_spelling_tables(
|
||||
monkeypatch: pytest.MonkeyPatch, raw: str, expected: bool | None
|
||||
) -> None:
|
||||
"""cv.boolean's spelling tables plus the 1/0 env convention."""
|
||||
monkeypatch.setenv("ESPHOME_CCACHE_ENABLE", raw)
|
||||
assert ccache.parse_enable_env("ESPHOME_CCACHE_ENABLE") is expected
|
||||
|
||||
Reference in New Issue
Block a user