Fail TTS tests if default TTS cache dir exists (#92023)

Fail tests if default tts cache dir exists
This commit is contained in:
Erik Montnemery 2023-04-26 15:28:48 +02:00 committed by GitHub
parent 904ce226fb
commit 32ffedd365
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,