From e52703617e83328b7dd413d6ea984443254e73fe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 22 Aug 2026 12:47:05 -0500 Subject: [PATCH] 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. --- esphome/build_helpers/ccache.py | 8 ++++++-- tests/unit_tests/build_helpers/test_ccache.py | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/esphome/build_helpers/ccache.py b/esphome/build_helpers/ccache.py index 8804e5028d..dfceaabf1c 100644 --- a/esphome/build_helpers/ccache.py +++ b/esphome/build_helpers/ccache.py @@ -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 diff --git a/tests/unit_tests/build_helpers/test_ccache.py b/tests/unit_tests/build_helpers/test_ccache.py index c612f79554..619a1a3476 100644 --- a/tests/unit_tests/build_helpers/test_ccache.py +++ b/tests/unit_tests/build_helpers/test_ccache.py @@ -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