diff --git a/tests/common.py b/tests/common.py index 4d767f0611c..df674d1824c 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1867,23 +1867,6 @@ async def snapshot_platform( assert state == snapshot(name=f"{entity_entry.entity_id}-state") -def reset_translation_cache(hass: HomeAssistant, components: list[str]) -> None: - """Reset translation cache for specified components. - - Use this if you are mocking a core component (for example via - mock_integration), to ensure that the mocked translations are not - persisted in the shared session cache. - """ - translations_cache = translation._async_get_translations_cache(hass) - for loaded_components in translations_cache.cache_data.loaded.values(): - for component_to_unload in components: - loaded_components.discard(component_to_unload) - for loaded_categories in translations_cache.cache_data.cache.values(): - for loaded_components in loaded_categories.values(): - for component_to_unload in components: - loaded_components.pop(component_to_unload, None) - - @lru_cache def get_quality_scale(integration: str) -> dict[str, QualityScaleStatus]: """Load quality scale for integration.""" diff --git a/tests/components/stt/test_init.py b/tests/components/stt/test_init.py index e36ece52f57..cada4b0c533 100644 --- a/tests/components/stt/test_init.py +++ b/tests/components/stt/test_init.py @@ -34,7 +34,6 @@ from tests.common import ( mock_integration, mock_platform, mock_restore_cache, - reset_translation_cache, ) from tests.typing import ClientSessionGenerator, WebSocketGenerator @@ -519,9 +518,6 @@ async def test_default_engine_prefer_cloud_entity( assert provider_engine.name == "test" assert async_default_engine(hass) == "stt.cloud_stt_entity" - # Reset the `cloud` translations cache to avoid flaky translation checks - reset_translation_cache(hass, ["cloud"]) - async def test_get_engine_legacy( hass: HomeAssistant, tmp_path: Path, mock_provider: MockSTTProvider diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index d115546c9bc..4d0767cddf3 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -44,7 +44,6 @@ from tests.common import ( mock_integration, mock_platform, mock_restore_cache, - reset_translation_cache, ) from tests.typing import ClientSessionGenerator, WebSocketGenerator @@ -1987,6 +1986,3 @@ async def test_default_engine_prefer_cloud_entity( provider_engine = tts.async_resolve_engine(hass, "test") assert provider_engine == "test" assert tts.async_default_engine(hass) == "tts.cloud_tts_entity" - - # Reset the `cloud` translations cache to avoid flaky translation checks - reset_translation_cache(hass, ["cloud"]) diff --git a/tests/conftest.py b/tests/conftest.py index 7d9fa7eda2e..b14523e065c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -85,6 +85,7 @@ from homeassistant.helpers import ( issue_registry as ir, label_registry as lr, recorder as recorder_helper, + translation as translation_helper, ) from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.translation import _TranslationsCacheData @@ -1232,11 +1233,10 @@ def mock_get_source_ip() -> Generator[_patch]: @pytest.fixture(autouse=True, scope="session") def translations_once() -> Generator[_patch]: - """Only load translations once per session. + """Only load translations once per module. - Warning: having this as a session fixture can cause issues with tests that - create mock integrations, overriding the real integration translations - with empty ones. Translations should be reset after such tests (see #131628) + Note: To avoid issues with tests that mock integrations, translations for + mocked integrations are cleaned up by the evict_faked_translations fixture. """ cache = _TranslationsCacheData({}, {}) patcher = patch( @@ -1250,6 +1250,27 @@ def translations_once() -> Generator[_patch]: patcher.stop() +@pytest.fixture(autouse=True, scope="module") +def evict_faked_translations(translations_once) -> Generator[_patch]: + """Clear translations for mocked integrations from the cache after each module.""" + real_component_strings = translation_helper._async_get_component_strings + with patch( + "homeassistant.helpers.translation._async_get_component_strings", + wraps=real_component_strings, + ) as mock_component_strings: + yield + cache: _TranslationsCacheData = translations_once.kwargs["return_value"] + for call in mock_component_strings.mock_calls: + integrations: dict[str, loader.Integration] = call.args[3] + for domain, integration in integrations.items(): + if str(integration.file_path).endswith( + f"homeassistant/components/{domain}" + ): + continue + cache.loaded["en"].discard(domain) + cache.cache["en"].pop(domain, None) + + @pytest.fixture def disable_translations_once( translations_once: _patch,