2020-04-05 10:15:29 +00:00
|
|
|
"""Conftest for TTS tests.
|
|
|
|
|
|
|
|
From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
|
|
|
|
"""
|
2023-04-25 20:21:18 +00:00
|
|
|
from collections.abc import Generator
|
2023-04-21 02:55:46 +00:00
|
|
|
from typing import Any
|
|
|
|
from unittest.mock import MagicMock, patch
|
2020-04-05 10:15:29 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-04-26 19:18:17 +00:00
|
|
|
from homeassistant.components.tts import _get_cache_files
|
2023-04-17 17:01:50 +00:00
|
|
|
from homeassistant.config import async_process_ha_core_config
|
2023-04-21 02:55:46 +00:00
|
|
|
from homeassistant.config_entries import ConfigFlow
|
2023-04-17 17:01:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-04-21 02:55:46 +00:00
|
|
|
from .common import (
|
|
|
|
DEFAULT_LANG,
|
|
|
|
TEST_DOMAIN,
|
|
|
|
MockProvider,
|
|
|
|
MockTTS,
|
|
|
|
MockTTSEntity,
|
|
|
|
mock_config_entry_setup,
|
|
|
|
mock_setup,
|
|
|
|
)
|
2023-04-17 17:01:50 +00:00
|
|
|
|
2023-04-21 02:55:46 +00:00
|
|
|
from tests.common import MockModule, mock_config_flow, mock_integration, mock_platform
|
2022-02-14 16:54:12 +00:00
|
|
|
|
2020-04-05 10:15:29 +00:00
|
|
|
|
|
|
|
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
"""Add test report to node."""
|
|
|
|
# execute all other hooks to obtain the report object
|
|
|
|
outcome = yield
|
|
|
|
rep = outcome.get_result()
|
|
|
|
|
|
|
|
# set a report attribute for each phase of a call, which can
|
|
|
|
# be "setup", "call", "teardown"
|
2020-04-07 21:14:28 +00:00
|
|
|
setattr(item, f"rep_{rep.when}", rep)
|
2022-02-14 16:54:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_get_cache_files():
|
|
|
|
"""Mock the list TTS cache function."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.tts._get_cache_files", return_value={}
|
|
|
|
) as mock_cache_files:
|
|
|
|
yield mock_cache_files
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2023-04-21 02:55:46 +00:00
|
|
|
def mock_init_cache_dir(
|
|
|
|
init_cache_dir_side_effect: Any,
|
|
|
|
) -> Generator[MagicMock, None, None]:
|
2023-04-26 19:18:17 +00:00
|
|
|
"""Mock the TTS cache dir in memory."""
|
2022-02-14 16:54:12 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.tts._init_tts_cache_dir",
|
2023-04-21 02:55:46 +00:00
|
|
|
side_effect=init_cache_dir_side_effect,
|
2022-02-14 16:54:12 +00:00
|
|
|
) as mock_cache_dir:
|
|
|
|
yield mock_cache_dir
|
|
|
|
|
|
|
|
|
2023-04-21 02:55:46 +00:00
|
|
|
@pytest.fixture
|
2023-04-25 20:21:18 +00:00
|
|
|
def init_cache_dir_side_effect() -> Any:
|
2023-04-21 02:55:46 +00:00
|
|
|
"""Return the cache dir."""
|
2023-04-25 20:21:18 +00:00
|
|
|
return None
|
2023-04-21 02:55:46 +00:00
|
|
|
|
|
|
|
|
2023-04-25 20:21:18 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2022-02-14 16:54:12 +00:00
|
|
|
def empty_cache_dir(tmp_path, mock_init_cache_dir, mock_get_cache_files, request):
|
|
|
|
"""Mock the TTS cache dir with empty dir."""
|
|
|
|
mock_init_cache_dir.return_value = str(tmp_path)
|
|
|
|
|
|
|
|
# Restore original get cache files behavior, we're working with a real dir.
|
|
|
|
mock_get_cache_files.side_effect = _get_cache_files
|
|
|
|
|
|
|
|
yield tmp_path
|
|
|
|
|
|
|
|
if request.node.rep_call.passed:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Print contents of dir if failed
|
2023-01-24 13:24:21 +00:00
|
|
|
print("Content of dir for", request.node.nodeid) # noqa: T201
|
2022-02-14 16:54:12 +00:00
|
|
|
for fil in tmp_path.iterdir():
|
2023-01-24 13:24:21 +00:00
|
|
|
print(fil.relative_to(tmp_path)) # noqa: T201
|
2022-02-14 16:54:12 +00:00
|
|
|
|
|
|
|
# To show the log.
|
2023-01-27 10:10:29 +00:00
|
|
|
pytest.fail("Test failed, see log for details")
|
2022-02-14 16:54:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mutagen_mock():
|
|
|
|
"""Mock writing tags."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.tts.SpeechManager.write_tags",
|
|
|
|
side_effect=lambda *args: args[1],
|
|
|
|
) as mock_write_tags:
|
|
|
|
yield mock_write_tags
|
2023-04-17 17:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def internal_url_mock(hass: HomeAssistant) -> None:
|
|
|
|
"""Mock internal URL of the instance."""
|
|
|
|
await async_process_ha_core_config(
|
|
|
|
hass,
|
|
|
|
{"internal_url": "http://example.local:8123"},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-04-21 02:55:46 +00:00
|
|
|
async def mock_tts(hass: HomeAssistant, mock_provider) -> None:
|
2023-04-17 17:01:50 +00:00
|
|
|
"""Mock TTS."""
|
|
|
|
mock_integration(hass, MockModule(domain="test"))
|
2023-04-21 02:55:46 +00:00
|
|
|
mock_platform(hass, "test.tts", MockTTS(mock_provider))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_provider() -> MockProvider:
|
|
|
|
"""Test TTS provider."""
|
|
|
|
return MockProvider(DEFAULT_LANG)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_tts_entity() -> MockTTSEntity:
|
|
|
|
"""Test TTS entity."""
|
|
|
|
return MockTTSEntity(DEFAULT_LANG)
|
|
|
|
|
|
|
|
|
|
|
|
class TTSFlow(ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def config_flow_fixture(hass: HomeAssistant) -> Generator[None, None, None]:
|
|
|
|
"""Mock config flow."""
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
|
|
|
|
with mock_config_flow(TEST_DOMAIN, TTSFlow):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="setup")
|
|
|
|
async def setup_fixture(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
request: pytest.FixtureRequest,
|
|
|
|
mock_provider: MockProvider,
|
|
|
|
mock_tts_entity: MockTTSEntity,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the test environment."""
|
|
|
|
if request.param == "mock_setup":
|
|
|
|
await mock_setup(hass, mock_provider)
|
|
|
|
elif request.param == "mock_config_entry_setup":
|
|
|
|
await mock_config_entry_setup(hass, mock_tts_entity)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Invalid setup fixture")
|