From d004e73d99bd5325dccd792624b530cb125cd004 Mon Sep 17 00:00:00 2001 From: James Mackay Date: Fri, 9 Oct 2026 07:46:53 +0100 Subject: [PATCH] =?UTF-8?q?fix(tests):=20drop=20the=20speculative=20config?= =?UTF-8?q?-entries=20install=20guard=20=E2=80=94=20the=20scoped-stub=20te?= =?UTF-8?q?ardowns=20already=20isolate=20per=20file;=20full=20suite=20gree?= =?UTF-8?q?n=20both=20variants=20(completes=20t30=20G6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/entity_stublib.py | 6 ------ tests/test_init.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/tests/entity_stublib.py b/tests/entity_stublib.py index 7c795ce..2cee30d 100644 --- a/tests/entity_stublib.py +++ b/tests/entity_stublib.py @@ -113,12 +113,6 @@ def install() -> None: """Register the stub modules (idempotent).""" if "homeassistant.components.media_player" in sys.modules: return - # honor per-file flow stub versions already in place (e.g. the - # config-flow/pairing/protocol files) — the hub's base would strip the - # base plumbing the flow tests already installed (t30 leak). - ce = sys.modules.get("homeassistant.config_entries") - if ce is not None and hasattr(ce, "OptionsFlowWithReload"): - return vol = types.ModuleType("voluptuous") vol.Required = lambda key, default=None: key diff --git a/tests/test_init.py b/tests/test_init.py index 50c83b3..c0386ea 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -13,17 +13,45 @@ from types import SimpleNamespace import entity_stublib import pytest +# Scoped-stub hygiene (mirrors tests/test_config_flow_pairing.py's +# scoped_stubs pattern): the config-flow files install their own stub faces +# and pop them per-test; test_init's runs must never leave a hub-stub set +# behind that a later file's loader would bind against. +_INIT_STUB_NAMES = ( + "voluptuous", + "homeassistant", + "homeassistant.config_entries", + "homeassistant.core", + "homeassistant.helpers", + "homeassistant.helpers.entity_registry", + "homeassistant.helpers.device_registry", + "homeassistant.helpers.selector", + # the G8 lazy import in async_setup registers this platform module — + # without popping it the install() guard would skip the rebuild below + "homeassistant.components.media_player", + "nsh.helpers", + "nsh.config_flow", + "nsh.init", + "nsh.media_player", +) + @pytest.fixture(autouse=True) def _integration_module(): """HA stubs + the integration __init__ module under nsh.init.""" + snapshot = {name: sys.modules.get(name) for name in _INIT_STUB_NAMES} entity_stublib.install() if "nsh.init" not in sys.modules: import conftest conftest._load_module("nsh.init", conftest._SHIELD_DIR / "__init__.py") yield sys.modules["nsh.init"] - sys.modules.pop("nsh.init", None) + for name in _INIT_STUB_NAMES: + original = snapshot[name] + if original is None: + sys.modules.pop(name, None) + else: + sys.modules[name] = original class _SeedEntriesStub: