Fix temp files of mqtt CI tests not cleaned up properly (#138741)

* Fix temp files of mqtt CI tests not cleaned up properly

* Do not cleanup tempfiles, patch gettempdir only
This commit is contained in:
Jan Bouwhuis 2025-02-18 08:38:43 +01:00 committed by GitHub
parent 0dc1151a25
commit 33df208296
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 6 deletions

View File

@ -2,6 +2,7 @@
import asyncio import asyncio
from collections.abc import AsyncGenerator, Generator from collections.abc import AsyncGenerator, Generator
from pathlib import Path
from random import getrandbits from random import getrandbits
from typing import Any from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
@ -39,13 +40,22 @@ def temp_dir_prefix() -> str:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_temp_dir(temp_dir_prefix: str) -> Generator[str]: async def mock_temp_dir(
hass: HomeAssistant, tmp_path: Path, temp_dir_prefix: str
) -> AsyncGenerator[str]:
"""Mock the certificate temp directory.""" """Mock the certificate temp directory."""
with patch( mqtt_temp_dir = f"home-assistant-mqtt-{temp_dir_prefix}-{getrandbits(10):03x}"
with (
patch(
"homeassistant.components.mqtt.util.tempfile.gettempdir",
return_value=tmp_path,
),
patch(
# Patch temp dir name to avoid tests fail running in parallel # Patch temp dir name to avoid tests fail running in parallel
"homeassistant.components.mqtt.util.TEMP_DIR_NAME", "homeassistant.components.mqtt.util.TEMP_DIR_NAME",
f"home-assistant-mqtt-{temp_dir_prefix}-{getrandbits(10):03x}", mqtt_temp_dir,
) as mocked_temp_dir: ) as mocked_temp_dir,
):
yield mocked_temp_dir yield mocked_temp_dir

View File

@ -1034,6 +1034,7 @@ async def test_reloadable(
await help_test_reloadable(hass, mqtt_client_mock, domain, config) await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.usefixtures("mock_temp_dir")
@pytest.mark.parametrize( @pytest.mark.parametrize(
("hass_config", "payload1", "state1", "payload2", "state2"), ("hass_config", "payload1", "state1", "payload2", "state2"),
[ [

View File

@ -1409,6 +1409,7 @@ async def test_reloadable(
await help_test_reloadable(hass, mqtt_client_mock, domain, config) await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.usefixtures("mock_temp_dir")
@pytest.mark.parametrize( @pytest.mark.parametrize(
"hass_config", "hass_config",
[ [