Adjust code, add test

This commit is contained in:
Erik 2025-02-18 10:08:47 +01:00
parent 264ebd3beb
commit 1952440ecc
2 changed files with 18 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import gc
import itertools import itertools
import logging import logging
import os import os
import pathlib
import reprlib import reprlib
from shutil import rmtree from shutil import rmtree
import sqlite3 import sqlite3
@ -49,7 +50,7 @@ from . import patch_recorder
# Setup patching of dt_util time functions before any other Home Assistant imports # Setup patching of dt_util time functions before any other Home Assistant imports
from . import patch_time # noqa: F401, isort:skip 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.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
from homeassistant.auth.models import Credentials from homeassistant.auth.models import Credentials
from homeassistant.auth.providers import homeassistant from homeassistant.auth.providers import homeassistant
@ -1260,15 +1261,17 @@ def evict_faked_translations(translations_once) -> Generator[_patch]:
) as mock_component_strings: ) as mock_component_strings:
yield yield
cache: _TranslationsCacheData = translations_once.kwargs["return_value"] cache: _TranslationsCacheData = translations_once.kwargs["return_value"]
component_paths = components.__path__
for call in mock_component_strings.mock_calls: for call in mock_component_strings.mock_calls:
integrations: dict[str, loader.Integration] = call.args[3] integrations: dict[str, loader.Integration] = call.args[3]
for domain, integration in integrations.items(): for domain, integration in integrations.items():
if str(integration.file_path).endswith( if any(
f"homeassistant/components/{domain}" pathlib.Path(f"{component_path}/{domain}") == integration.file_path
for component_path in component_paths
): ):
continue continue
cache.loaded["en"].discard(domain) cache.loaded["en"].discard(domain)
cache.cache["en"].pop(domain, None)
@pytest.fixture @pytest.fixture

View File

@ -1,6 +1,7 @@
"""Test test fixture configuration.""" """Test test fixture configuration."""
from http import HTTPStatus from http import HTTPStatus
import pathlib
import socket import socket
from aiohttp import web from aiohttp import web
@ -11,6 +12,7 @@ from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant, async_get_hass from homeassistant.core import HomeAssistant, async_get_hass
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from .common import MockModule, mock_integration
from .typing import ClientSessionGenerator from .typing import ClientSessionGenerator
@ -70,3 +72,12 @@ async def test_aiohttp_client_frozen_router_view(
assert response.status == HTTPStatus.OK assert response.status == HTTPStatus.OK
result = await response.json() result = await response.json()
assert result["test"] is True assert result["test"] is True
async def test_evict_faked_translations_assumptions(hass: HomeAssistant) -> None:
"""Test assumptions made when detecting translations for mocked integrations.
If this test fails, the evict_faked_translations may need to be updated.
"""
integration = mock_integration(hass, MockModule("test"), built_in=True)
assert integration.file_path == pathlib.Path("")