diff --git a/tests/unit_tests/components/test_logger.py b/tests/unit_tests/components/test_logger.py index 7453c8845a7..dbfb6bb3be7 100644 --- a/tests/unit_tests/components/test_logger.py +++ b/tests/unit_tests/components/test_logger.py @@ -1,21 +1,16 @@ """Tests for the logger component's lazy platform tables.""" +import subprocess import sys +from esphome.components import logger + def test_uart_selection_tables_exposed_for_external_tooling() -> None: """device-builder introspects these names off the module; keep them - accessible (and discoverable) without importing the platform packages.""" - for mod in ("esphome.components.esp32", "esphome.components.libretiny"): - sys.modules.pop(mod, None) - - from esphome.components import logger - + accessible (and discoverable) and stable between accesses.""" assert "UART_SELECTION_ESP32" in dir(logger) assert "UART_SELECTION_LIBRETINY" in dir(logger) - # dir() must not import the platform packages; only table access may - assert "esphome.components.esp32" not in sys.modules - assert "esphome.components.libretiny" not in sys.modules assert logger.UART_SELECTION_ESP32["ESP32C3"] == [ "UART0", "UART1", @@ -25,3 +20,18 @@ def test_uart_selection_tables_exposed_for_external_tooling() -> None: assert "bk72xx" in logger.UART_SELECTION_LIBRETINY # Repeated access returns the same cached object assert logger.UART_SELECTION_ESP32 is logger.UART_SELECTION_ESP32 + + +def test_dir_does_not_import_platform_packages() -> None: + """Listing the names must not load the platform packages; only reading a + table may. Checked in a fresh interpreter so nothing loaded by other tests + hides a leak, and nothing here changes what later tests see.""" + check = ( + "import sys; from esphome.components import logger; dir(logger); " + "print(','.join(m for m in ('esphome.components.esp32', " + "'esphome.components.libretiny') if m in sys.modules))" + ) + result = subprocess.run( + [sys.executable, "-c", check], capture_output=True, text=True, check=True + ) + assert result.stdout.strip() == ""