Improve TTS cache dir mocking (#93468)

This commit is contained in:
Erik Montnemery 2023-05-24 21:00:35 +02:00 committed by GitHub
parent 6057aeee2f
commit 68379dd55a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 128 additions and 125 deletions

View File

@ -23,15 +23,16 @@ from tests.common import (
mock_integration,
mock_platform,
)
from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import
init_cache_dir_side_effect,
mock_get_cache_files,
mock_init_cache_dir,
)
_TRANSCRIPT = "test transcript"
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
class BaseProvider:
"""Mock STT provider."""
@ -190,9 +191,6 @@ async def init_supporting_components(
mock_stt_provider_entity: MockSttProviderEntity,
mock_tts_provider: MockTTSProvider,
config_flow_fixture,
init_cache_dir_side_effect, # noqa: F811
mock_get_cache_files, # noqa: F811
mock_init_cache_dir, # noqa: F811
):
"""Initialize relevant components with empty configs."""

View File

@ -8,12 +8,11 @@ from homeassistant.components.cloud import const, prefs
from . import mock_cloud, mock_cloud_prefs
# Prevent TTS cache from being created
from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import
init_cache_dir_side_effect,
mock_get_cache_files,
mock_init_cache_dir,
)
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
@pytest.fixture(autouse=True)

View File

@ -4,6 +4,14 @@ from unittest.mock import 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]:

View File

@ -1,6 +1,4 @@
"""The tests for the Google speech platform."""
import os
import shutil
from unittest.mock import patch
from gtts import gTTSError
@ -18,7 +16,17 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
from tests.common import async_mock_service
from tests.components.tts.conftest import mutagen_mock # noqa: F401
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
async def get_media_source_url(hass, media_content_id):
@ -30,15 +38,6 @@ async def get_media_source_url(hass, media_content_id):
return resolved.url
@pytest.fixture(autouse=True)
def cleanup_cache(hass):
"""Clean up TTS cache."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
@pytest.fixture
async def calls(hass):
"""Mock media player calls."""

View File

@ -1,6 +1,4 @@
"""The tests for the MaryTTS speech platform."""
import os
import shutil
from unittest.mock import patch
import pytest
@ -27,12 +25,9 @@ async def get_media_source_url(hass, media_content_id):
@pytest.fixture(autouse=True)
def cleanup_cache(hass):
"""Prevent TTS writing."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
async def test_setup_component(hass: HomeAssistant) -> None:

View File

@ -1,6 +1,4 @@
"""Tests for Microsoft Text-to-Speech."""
import os
import shutil
from unittest.mock import patch
from pycsspeechtts import pycsspeechtts
@ -32,12 +30,9 @@ async def get_media_source_url(hass: HomeAssistant, media_content_id):
@pytest.fixture(autouse=True)
def cleanup_cache(hass: HomeAssistant):
"""Clean up TTS cache."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
@pytest.fixture

View File

@ -38,8 +38,8 @@ def pytest_runtest_makereport(item, call):
setattr(item, f"rep_{rep.when}", rep)
@pytest.fixture(autouse=True)
def mock_get_cache_files():
@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={}
@ -47,35 +47,37 @@ def mock_get_cache_files():
yield mock_cache_files
@pytest.fixture(autouse=True)
def mock_init_cache_dir(
init_cache_dir_side_effect: Any,
@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_cache_dir_side_effect,
side_effect=init_tts_cache_dir_side_effect,
) as mock_cache_dir:
yield mock_cache_dir
@pytest.fixture
def init_cache_dir_side_effect() -> Any:
@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(autouse=True)
def empty_cache_dir(tmp_path, mock_init_cache_dir, mock_get_cache_files, request):
@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_init_cache_dir.return_value = str(tmp_path)
mock_tts_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
mock_tts_get_cache_files.side_effect = _get_cache_files
yield tmp_path
if request.node.rep_call.passed:
if not hasattr(request.node, "rep_call") or request.node.rep_call.passed:
return
# Print contents of dir if failed
@ -87,8 +89,14 @@ def empty_cache_dir(tmp_path, mock_init_cache_dir, mock_get_cache_files, request
pytest.fail("Test failed, see log for details")
@pytest.fixture(autouse=True)
def mutagen_mock():
@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",
@ -97,6 +105,11 @@ def mutagen_mock():
yield mock_write_tags
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
"""Mock writing tags."""
@pytest.fixture(autouse=True)
async def internal_url_mock(hass: HomeAssistant) -> None:
"""Mock internal URL of the instance."""

View File

@ -148,12 +148,12 @@ async def test_setup_component(hass: HomeAssistant, setup: str) -> None:
assert f"{tts.DOMAIN}.test" in hass.config.components
@pytest.mark.parametrize("init_cache_dir_side_effect", [OSError(2, "No access")])
@pytest.mark.parametrize("init_tts_cache_dir_side_effect", [OSError(2, "No access")])
@pytest.mark.parametrize(
"setup", ["mock_setup", "mock_config_entry_setup"], indirect=True
)
async def test_setup_component_no_access_cache_folder(
hass: HomeAssistant, mock_init_cache_dir: MagicMock, setup: str
hass: HomeAssistant, mock_tts_init_cache_dir: MagicMock, setup: str
) -> None:
"""Set up a TTS platform with defaults."""
assert not hass.services.has_service(tts.DOMAIN, "test_say")
@ -187,7 +187,7 @@ async def test_setup_component_no_access_cache_folder(
)
async def test_service(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -212,7 +212,7 @@ async def test_service(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_{expected_url_suffix}.mp3"
).is_file()
@ -248,7 +248,7 @@ async def test_service(
)
async def test_service_default_language(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -271,7 +271,7 @@ async def test_service_default_language(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ (
f"42f18378fd4393d18c8dd11d03fa9563c1e54491_de-de_-_{expected_url_suffix}.mp3"
)
@ -309,7 +309,7 @@ async def test_service_default_language(
)
async def test_service_default_special_language(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -332,7 +332,7 @@ async def test_service_default_special_language(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_{expected_url_suffix}.mp3"
).is_file()
@ -366,7 +366,7 @@ async def test_service_default_special_language(
)
async def test_service_language(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -389,7 +389,7 @@ async def test_service_language(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_de-de_-_{expected_url_suffix}.mp3"
).is_file()
@ -423,7 +423,7 @@ async def test_service_language(
)
async def test_service_wrong_language(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -441,7 +441,7 @@ async def test_service_wrong_language(
)
assert len(calls) == 0
assert not (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_lang_-_{expected_url_suffix}.mp3"
).is_file()
@ -477,7 +477,7 @@ async def test_service_wrong_language(
)
async def test_service_options(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -502,7 +502,7 @@ async def test_service_options(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ (
"42f18378fd4393d18c8dd11d03fa9563c1e54491"
f"_de-de_{opt_hash}_{expected_url_suffix}.mp3"
@ -561,7 +561,7 @@ class MockEntityWithDefaults(MockTTSEntity):
)
async def test_service_default_options(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -586,7 +586,7 @@ async def test_service_default_options(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ (
"42f18378fd4393d18c8dd11d03fa9563c1e54491"
f"_de-de_{opt_hash}_{expected_url_suffix}.mp3"
@ -629,7 +629,7 @@ async def test_service_default_options(
)
async def test_merge_default_service_options(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -657,7 +657,7 @@ async def test_merge_default_service_options(
)
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ (
"42f18378fd4393d18c8dd11d03fa9563c1e54491"
f"_de-de_{opt_hash}_{expected_url_suffix}.mp3"
@ -696,7 +696,7 @@ async def test_merge_default_service_options(
)
async def test_service_wrong_options(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -717,7 +717,7 @@ async def test_service_wrong_options(
assert len(calls) == 0
await hass.async_block_till_done()
assert not (
empty_cache_dir
mock_tts_cache_dir
/ (
"42f18378fd4393d18c8dd11d03fa9563c1e54491"
f"_de-de_{opt_hash}_{expected_url_suffix}.mp3"
@ -752,7 +752,7 @@ async def test_service_wrong_options(
)
async def test_service_clear_cache(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -772,7 +772,7 @@ async def test_service_clear_cache(
await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID])
await hass.async_block_till_done()
assert (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_{expected_url_suffix}.mp3"
).is_file()
@ -781,7 +781,7 @@ async def test_service_clear_cache(
)
assert not (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_{expected_url_suffix}.mp3"
).is_file()
@ -814,7 +814,7 @@ async def test_service_clear_cache(
async def test_service_receive_voice(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -886,7 +886,7 @@ async def test_service_receive_voice(
async def test_service_receive_voice_german(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -994,7 +994,7 @@ async def test_web_view_wrong_filename(
)
async def test_service_without_cache(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -1012,7 +1012,7 @@ async def test_service_without_cache(
await hass.async_block_till_done()
assert len(calls) == 1
assert not (
empty_cache_dir
mock_tts_cache_dir
/ f"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_{expected_url_suffix}.mp3"
).is_file()
@ -1042,7 +1042,7 @@ class MockEntityBoom(MockTTSEntity):
@pytest.mark.parametrize("mock_provider", [MockProviderBoom(DEFAULT_LANG)])
async def test_setup_legacy_cache_dir(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
mock_provider: MockProvider,
) -> None:
"""Set up a TTS platform with cache and call service without cache."""
@ -1050,7 +1050,7 @@ async def test_setup_legacy_cache_dir(
tts_data = b""
cache_file = (
empty_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3"
mock_tts_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3"
)
with open(cache_file, "wb") as voice_file:
@ -1078,14 +1078,14 @@ async def test_setup_legacy_cache_dir(
@pytest.mark.parametrize("mock_tts_entity", [MockEntityBoom(DEFAULT_LANG)])
async def test_setup_cache_dir(
hass: HomeAssistant,
empty_cache_dir,
mock_tts_cache_dir,
mock_tts_entity: MockTTSEntity,
) -> None:
"""Set up a TTS platform with cache and call service without cache."""
calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
tts_data = b""
cache_file = empty_cache_dir / (
cache_file = mock_tts_cache_dir / (
"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_tts.test.mp3"
)
@ -1182,13 +1182,13 @@ async def test_service_get_tts_error(
async def test_load_cache_legacy_retrieve_without_mem_cache(
hass: HomeAssistant,
mock_provider: MockProvider,
empty_cache_dir,
mock_tts_cache_dir,
hass_client: ClientSessionGenerator,
) -> None:
"""Set up component and load cache and get without mem cache."""
tts_data = b""
cache_file = (
empty_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_test.mp3"
mock_tts_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_test.mp3"
)
with open(cache_file, "wb") as voice_file:
@ -1208,12 +1208,12 @@ async def test_load_cache_legacy_retrieve_without_mem_cache(
async def test_load_cache_retrieve_without_mem_cache(
hass: HomeAssistant,
mock_tts_entity: MockTTSEntity,
empty_cache_dir,
mock_tts_cache_dir,
hass_client: ClientSessionGenerator,
) -> None:
"""Set up component and load cache and get without mem cache."""
tts_data = b""
cache_file = empty_cache_dir / (
cache_file = mock_tts_cache_dir / (
"42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_tts.test.mp3"
)

View File

@ -169,7 +169,7 @@ async def test_service_base_url_set(hass: HomeAssistant, mock_tts) -> None:
async def test_service_without_cache_config(
hass: HomeAssistant, empty_cache_dir, mock_tts
hass: HomeAssistant, mock_tts_cache_dir, mock_tts
) -> None:
"""Set up a TTS platform without cache."""
calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
@ -191,5 +191,5 @@ async def test_service_without_cache_config(
assert len(calls) == 1
await hass.async_block_till_done()
assert not (
empty_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3"
mock_tts_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3"
).is_file()

View File

@ -1,8 +1,6 @@
"""The tests for the VoiceRSS speech platform."""
import asyncio
from http import HTTPStatus
import os
import shutil
import pytest
@ -17,7 +15,6 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, async_mock_service
from tests.components.tts.conftest import mutagen_mock # noqa: F401
from tests.test_util.aiohttp import AiohttpClientMocker
URL = "https://api.voicerss.org/"
@ -30,6 +27,17 @@ FORM_DATA = {
}
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
async def get_media_source_url(hass, media_content_id):
"""Get the media source url."""
if media_source.DOMAIN not in hass.config.components:
@ -39,15 +47,6 @@ async def get_media_source_url(hass, media_content_id):
return resolved.url
@pytest.fixture(autouse=True)
def cleanup_cache(hass):
"""Prevent TTS writing."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
async def test_setup_component(hass: HomeAssistant) -> None:
"""Test setup component."""
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}

View File

@ -15,11 +15,11 @@ from homeassistant.helpers.entity_component import DATA_INSTANCES
from . import MockAsyncTcpClient
from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import
init_cache_dir_side_effect,
mock_get_cache_files,
mock_init_cache_dir,
)
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
async def test_support(hass: HomeAssistant, init_wyoming_tts) -> None:

View File

@ -1,8 +1,6 @@
"""The tests for the Yandex SpeechKit speech platform."""
import asyncio
from http import HTTPStatus
import os
import shutil
import pytest
@ -16,14 +14,22 @@ from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, async_mock_service
from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import
mutagen_mock,
)
from tests.test_util.aiohttp import AiohttpClientMocker
URL = "https://tts.voicetech.yandex.net/generate?"
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
async def get_media_source_url(hass, media_content_id):
"""Get the media source url."""
if media_source.DOMAIN not in hass.config.components:
@ -33,15 +39,6 @@ async def get_media_source_url(hass, media_content_id):
return resolved.url
@pytest.fixture(autouse=True)
def cleanup_cache(hass):
"""Prevent TTS writing."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
async def test_setup_component(hass: HomeAssistant) -> None:
"""Test setup component."""
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}