From 3fc0c9a3257d06d373960dec3a906ed70773a807 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 25 May 2023 11:59:20 +0200 Subject: [PATCH] Improve TTS test fixtures (#93517) --- tests/components/conftest.py | 59 ++++++++++++++++++++++++----- tests/components/tts/common.py | 60 ++++++++++++++++++++++++++++++ tests/components/tts/conftest.py | 64 -------------------------------- 3 files changed, 110 insertions(+), 73 deletions(-) diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 576b3b3b0bc..ff58909126a 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -1,17 +1,10 @@ """Fixtures for component testing.""" from collections.abc import Generator -from unittest.mock import patch +from typing import Any +from unittest.mock import MagicMock, patch import pytest -from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import - init_tts_cache_dir_side_effect_fixture, - mock_tts_cache_dir_fixture, - mock_tts_get_cache_files_fixture, - mock_tts_init_cache_dir_fixture, - tts_mutagen_mock_fixture, -) - @pytest.fixture(scope="session", autouse=True) def patch_zeroconf_multiple_catcher() -> Generator[None, None, None]: @@ -40,3 +33,51 @@ def entity_registry_enabled_by_default() -> Generator[None, None, None]: return_value=True, ): yield + + +@pytest.fixture(name="mock_tts_get_cache_files") +def mock_tts_get_cache_files_fixture(): + """Mock the list TTS cache function.""" + from tests.components.tts.common import mock_tts_get_cache_files_fixture_helper + + yield from mock_tts_get_cache_files_fixture_helper() + + +@pytest.fixture(name="mock_tts_init_cache_dir") +def mock_tts_init_cache_dir_fixture( + init_tts_cache_dir_side_effect: Any, +) -> Generator[MagicMock, None, None]: + """Mock the TTS cache dir in memory.""" + from tests.components.tts.common import mock_tts_init_cache_dir_fixture_helper + + yield from mock_tts_init_cache_dir_fixture_helper(init_tts_cache_dir_side_effect) + + +@pytest.fixture(name="init_tts_cache_dir_side_effect") +def init_tts_cache_dir_side_effect_fixture() -> Any: + """Return the cache dir.""" + from tests.components.tts.common import ( + init_tts_cache_dir_side_effect_fixture_helper, + ) + + return init_tts_cache_dir_side_effect_fixture_helper() + + +@pytest.fixture(name="mock_tts_cache_dir") +def mock_tts_cache_dir_fixture( + tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request +): + """Mock the TTS cache dir with empty dir.""" + from tests.components.tts.common import mock_tts_cache_dir_fixture_helper + + yield from mock_tts_cache_dir_fixture_helper( + tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request + ) + + +@pytest.fixture(name="tts_mutagen_mock") +def tts_mutagen_mock_fixture(): + """Mock writing tags.""" + from tests.components.tts.common import tts_mutagen_mock_fixture_helper + + yield from tts_mutagen_mock_fixture_helper() diff --git a/tests/components/tts/common.py b/tests/components/tts/common.py index 6f8de136484..4e57b85ba4f 100644 --- a/tests/components/tts/common.py +++ b/tests/components/tts/common.py @@ -1,8 +1,11 @@ """Provide common tests tools for tts.""" from __future__ import annotations +from collections.abc import Generator from typing import Any +from unittest.mock import MagicMock, patch +import pytest import voluptuous as vol from homeassistant.components import media_source @@ -14,6 +17,7 @@ from homeassistant.components.tts import ( TextToSpeechEntity, TtsAudioType, Voice, + _get_cache_files, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -34,6 +38,62 @@ SUPPORT_LANGUAGES = ["de_CH", "de_DE", "en_GB", "en_US"] TEST_DOMAIN = "test" +def mock_tts_get_cache_files_fixture_helper(): + """Mock the list TTS cache function.""" + with patch( + "homeassistant.components.tts._get_cache_files", return_value={} + ) as mock_cache_files: + yield mock_cache_files + + +def mock_tts_init_cache_dir_fixture_helper( + init_tts_cache_dir_side_effect: Any, +) -> Generator[MagicMock, None, None]: + """Mock the TTS cache dir in memory.""" + with patch( + "homeassistant.components.tts._init_tts_cache_dir", + side_effect=init_tts_cache_dir_side_effect, + ) as mock_cache_dir: + yield mock_cache_dir + + +def init_tts_cache_dir_side_effect_fixture_helper() -> Any: + """Return the cache dir.""" + return None + + +def mock_tts_cache_dir_fixture_helper( + tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request +): + """Mock the TTS cache dir with empty dir.""" + mock_tts_init_cache_dir.return_value = str(tmp_path) + + # Restore original get cache files behavior, we're working with a real dir. + mock_tts_get_cache_files.side_effect = _get_cache_files + + yield tmp_path + + if not hasattr(request.node, "rep_call") or request.node.rep_call.passed: + return + + # Print contents of dir if failed + print("Content of dir for", request.node.nodeid) # noqa: T201 + for fil in tmp_path.iterdir(): + print(fil.relative_to(tmp_path)) # noqa: T201 + + # To show the log. + pytest.fail("Test failed, see log for details") + + +def tts_mutagen_mock_fixture_helper(): + """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 + + async def get_media_source_url(hass: HomeAssistant, media_content_id: str) -> str: """Get the media source url.""" if media_source.DOMAIN not in hass.config.components: diff --git a/tests/components/tts/conftest.py b/tests/components/tts/conftest.py index d7ec722bbf2..753c90e158d 100644 --- a/tests/components/tts/conftest.py +++ b/tests/components/tts/conftest.py @@ -3,12 +3,9 @@ From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures """ from collections.abc import Generator -from typing import Any -from unittest.mock import MagicMock, patch import pytest -from homeassistant.components.tts import _get_cache_files from homeassistant.config import async_process_ha_core_config from homeassistant.config_entries import ConfigFlow from homeassistant.core import HomeAssistant @@ -38,73 +35,12 @@ def pytest_runtest_makereport(item, call): setattr(item, f"rep_{rep.when}", rep) -@pytest.fixture(name="mock_tts_get_cache_files") -def mock_tts_get_cache_files_fixture(): - """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(name="mock_tts_init_cache_dir") -def mock_tts_init_cache_dir_fixture( - init_tts_cache_dir_side_effect: Any, -) -> Generator[MagicMock, None, None]: - """Mock the TTS cache dir in memory.""" - with patch( - "homeassistant.components.tts._init_tts_cache_dir", - side_effect=init_tts_cache_dir_side_effect, - ) as mock_cache_dir: - yield mock_cache_dir - - -@pytest.fixture(name="init_tts_cache_dir_side_effect") -def init_tts_cache_dir_side_effect_fixture() -> Any: - """Return the cache dir.""" - return None - - -@pytest.fixture(name="mock_tts_cache_dir") -def mock_tts_cache_dir_fixture( - tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request -): - """Mock the TTS cache dir with empty dir.""" - mock_tts_init_cache_dir.return_value = str(tmp_path) - - # Restore original get cache files behavior, we're working with a real dir. - mock_tts_get_cache_files.side_effect = _get_cache_files - - yield tmp_path - - if not hasattr(request.node, "rep_call") or request.node.rep_call.passed: - return - - # Print contents of dir if failed - print("Content of dir for", request.node.nodeid) # noqa: T201 - for fil in tmp_path.iterdir(): - print(fil.relative_to(tmp_path)) # noqa: T201 - - # To show the log. - pytest.fail("Test failed, see log for details") - - @pytest.fixture(autouse=True, name="mock_tts_cache_dir") def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir): """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir -@pytest.fixture(name="tts_mutagen_mock") -def tts_mutagen_mock_fixture(): - """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 - - @pytest.fixture(autouse=True) def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): """Mock writing tags."""