From 33df20829634661431567c3b1ff2415ee9ef1dc6 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 18 Feb 2025 08:38:43 +0100 Subject: [PATCH] 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 --- tests/components/mqtt/conftest.py | 22 +++++++++++++++------ tests/components/mqtt/test_binary_sensor.py | 1 + tests/components/mqtt/test_sensor.py | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/components/mqtt/conftest.py b/tests/components/mqtt/conftest.py index 87bbcecebe5..efe5d0f1a4e 100644 --- a/tests/components/mqtt/conftest.py +++ b/tests/components/mqtt/conftest.py @@ -2,6 +2,7 @@ import asyncio from collections.abc import AsyncGenerator, Generator +from pathlib import Path from random import getrandbits from typing import Any from unittest.mock import AsyncMock, patch @@ -39,13 +40,22 @@ def temp_dir_prefix() -> str: @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.""" - with patch( - # Patch temp dir name to avoid tests fail running in parallel - "homeassistant.components.mqtt.util.TEMP_DIR_NAME", - f"home-assistant-mqtt-{temp_dir_prefix}-{getrandbits(10):03x}", - ) as mocked_temp_dir: + 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 + "homeassistant.components.mqtt.util.TEMP_DIR_NAME", + mqtt_temp_dir, + ) as mocked_temp_dir, + ): yield mocked_temp_dir diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 34be237fb72..8809f2201f2 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -1034,6 +1034,7 @@ async def test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config) +@pytest.mark.usefixtures("mock_temp_dir") @pytest.mark.parametrize( ("hass_config", "payload1", "state1", "payload2", "state2"), [ diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 6b3bbd6334c..9226b03a7d2 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -1409,6 +1409,7 @@ async def test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config) +@pytest.mark.usefixtures("mock_temp_dir") @pytest.mark.parametrize( "hass_config", [