mirror of
https://github.com/home-assistant/core.git
synced 2025-12-20 02:48:57 +00:00
Fix evict_faked_translations fixture (#159419)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
@@ -1270,9 +1270,11 @@ def evict_faked_translations(translations_once) -> Generator[_patch]:
|
|||||||
component_paths = components.__path__
|
component_paths = components.__path__
|
||||||
|
|
||||||
for call in mock_component_strings.mock_calls:
|
for call in mock_component_strings.mock_calls:
|
||||||
|
_components: set[str] = call.args[2]
|
||||||
integrations: dict[str, loader.Integration] = call.args[3]
|
integrations: dict[str, loader.Integration] = call.args[3]
|
||||||
for domain, integration in integrations.items():
|
for domain in _components:
|
||||||
if any(
|
# If the integration exists, don't evict from cache
|
||||||
|
if (integration := integrations.get(domain)) and any(
|
||||||
pathlib.Path(f"{component_path}/{domain}") == integration.file_path
|
pathlib.Path(f"{component_path}/{domain}") == integration.file_path
|
||||||
for component_path in component_paths
|
for component_path in component_paths
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""Test test fixture configuration."""
|
"""Test test fixture configuration."""
|
||||||
|
|
||||||
from collections.abc import Generator
|
from collections.abc import Callable, Generator
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import pathlib
|
import pathlib
|
||||||
import socket
|
import socket
|
||||||
@@ -90,17 +90,36 @@ async def test_evict_faked_translations_assumptions(hass: HomeAssistant) -> None
|
|||||||
assert integration.file_path == pathlib.Path("custom_components/test")
|
assert integration.file_path == pathlib.Path("custom_components/test")
|
||||||
|
|
||||||
|
|
||||||
async def test_evict_faked_translations(hass: HomeAssistant, translations_once) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test the evict_faked_translations fixture."""
|
"prepare_integration",
|
||||||
|
[
|
||||||
|
# Fake integration backed by a module
|
||||||
|
lambda hass: mock_integration(hass, MockModule("test"), built_in=True),
|
||||||
|
# fake integration not backed by a module
|
||||||
|
lambda hass: None,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_evict_faked_translations(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
translations_once,
|
||||||
|
prepare_integration: Callable[[HomeAssistant], None],
|
||||||
|
) -> None:
|
||||||
|
"""Test the evict_faked_translations fixture.
|
||||||
|
|
||||||
|
In this test, we load translations for a fake integration, then ensure that
|
||||||
|
after the fixture is torn down, only the real integration remains in the
|
||||||
|
translations cache.
|
||||||
|
"""
|
||||||
cache: translation._TranslationsCacheData = translations_once.kwargs["return_value"]
|
cache: translation._TranslationsCacheData = translations_once.kwargs["return_value"]
|
||||||
fake_domain = "test"
|
fake_domain = "test"
|
||||||
real_domain = "homeassistant"
|
real_domain = "homeassistant"
|
||||||
|
|
||||||
# Evict the real domain from the cache in case it's been loaded before
|
if "en" in cache.loaded:
|
||||||
cache.loaded["en"].discard(real_domain)
|
# Evict the real domain from the cache in case it's been loaded before
|
||||||
|
cache.loaded["en"].discard(real_domain)
|
||||||
|
|
||||||
assert fake_domain not in cache.loaded["en"]
|
assert fake_domain not in cache.loaded["en"]
|
||||||
assert real_domain not in cache.loaded["en"]
|
assert real_domain not in cache.loaded["en"]
|
||||||
|
|
||||||
# The evict_faked_translations fixture has module scope, so we set it up and
|
# The evict_faked_translations fixture has module scope, so we set it up and
|
||||||
# tear it down manually
|
# tear it down manually
|
||||||
@@ -110,7 +129,8 @@ async def test_evict_faked_translations(hass: HomeAssistant, translations_once)
|
|||||||
# Set up the evict_faked_translations fixture
|
# Set up the evict_faked_translations fixture
|
||||||
next(gen)
|
next(gen)
|
||||||
|
|
||||||
mock_integration(hass, MockModule(fake_domain), built_in=True)
|
# Try loading translations for mock integration
|
||||||
|
prepare_integration(hass)
|
||||||
await translation.async_load_integrations(hass, {fake_domain, real_domain})
|
await translation.async_load_integrations(hass, {fake_domain, real_domain})
|
||||||
assert fake_domain in cache.loaded["en"]
|
assert fake_domain in cache.loaded["en"]
|
||||||
assert real_domain in cache.loaded["en"]
|
assert real_domain in cache.loaded["en"]
|
||||||
|
|||||||
Reference in New Issue
Block a user