Remove workaround for flaky translation tests (#131628)

This commit is contained in:
epenet 2024-11-27 15:37:36 +01:00 committed by GitHub
parent a7db35c76c
commit d6f4a79b46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 5 deletions

View File

@ -1815,3 +1815,20 @@ async def snapshot_platform(
state = hass.states.get(entity_entry.entity_id)
assert state, f"State not found for {entity_entry.entity_id}"
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)

View File

@ -827,9 +827,6 @@ async def check_translations(
f"Unused ignore translations: {', '.join(unused_ignore)}. "
"Please remove them from the ignore_translations fixture."
)
for key, description in translation_errors.items():
if key.startswith("component.cloud.issues."):
# cloud tests are flaky
continue
for description in translation_errors.values():
if description not in {"used", "unused"}:
pytest.fail(description)

View File

@ -45,6 +45,7 @@ from tests.common import (
mock_integration,
mock_platform,
mock_restore_cache,
reset_translation_cache,
)
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@ -1988,3 +1989,6 @@ 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
reset_translation_cache(hass, ["cloud"])

View File

@ -1192,7 +1192,12 @@ 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 session.
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)
"""
cache = _TranslationsCacheData({}, {})
patcher = patch(
"homeassistant.helpers.translation._TranslationsCacheData",