1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 14:08:21 +00:00

Clean up translations for mocked integrations inbetween tests (#138732)

* Clean up translations for mocked integrations inbetween tests

* Adjust code, add test

* Fix docstring

* Improve cleanup, add test

* Fix test
This commit is contained in:
Erik Montnemery
2025-02-19 13:49:31 +01:00
committed by GitHub
parent 1733f5d3fb
commit af0a862aab
5 changed files with 77 additions and 29 deletions

View File

@@ -11,6 +11,7 @@ import gc
import itertools
import logging
import os
import pathlib
import reprlib
from shutil import rmtree
import sqlite3
@@ -49,7 +50,7 @@ from . import patch_recorder
# Setup patching of dt_util time functions before any other Home Assistant imports
from . import patch_time # noqa: F401, isort:skip
from homeassistant import core as ha, loader, runner
from homeassistant import components, core as ha, loader, runner
from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
from homeassistant.auth.models import Credentials
from homeassistant.auth.providers import homeassistant
@@ -85,6 +86,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
@@ -1234,9 +1236,8 @@ def mock_get_source_ip() -> Generator[_patch]:
def translations_once() -> Generator[_patch]:
"""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)
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 +1251,30 @@ 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"]
component_paths = components.__path__
for call in mock_component_strings.mock_calls:
integrations: dict[str, loader.Integration] = call.args[3]
for domain, integration in integrations.items():
if any(
pathlib.Path(f"{component_path}/{domain}") == integration.file_path
for component_path in component_paths
):
continue
for loaded_for_lang in cache.loaded.values():
loaded_for_lang.discard(domain)
@pytest.fixture
def disable_translations_once(
translations_once: _patch,