diff --git a/tests/components/tts/conftest.py b/tests/components/tts/conftest.py index 43488808693..dd018e4673f 100644 --- a/tests/components/tts/conftest.py +++ b/tests/components/tts/conftest.py @@ -3,12 +3,13 @@ From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures """ from collections.abc import Generator +import os from typing import Any from unittest.mock import MagicMock, patch import pytest -from homeassistant.components.tts import _get_cache_files +from homeassistant.components.tts import DEFAULT_CACHE_DIR, _get_cache_files from homeassistant.config import async_process_ha_core_config from homeassistant.config_entries import ConfigFlow from homeassistant.core import HomeAssistant @@ -49,9 +50,14 @@ def mock_get_cache_files(): @pytest.fixture(autouse=True) def mock_init_cache_dir( + hass: HomeAssistant, init_cache_dir_side_effect: Any, ) -> Generator[MagicMock, None, None]: - """Mock the TTS cache dir in memory.""" + """Prevent the TTS cache from being created and fail the test if it exists.""" + cache_dir = hass.config.path(DEFAULT_CACHE_DIR) + if os.path.isdir(cache_dir): + pytest.fail(f"Default TTS cache dir '{cache_dir}' already exists") + with patch( "homeassistant.components.tts._init_tts_cache_dir", side_effect=init_cache_dir_side_effect,