From a7093d36872f6581de1a42320f7ac28a8cd78ad0 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:57:34 +0200 Subject: [PATCH] Fix flaky filesize tests (#91200) * Fix flaky filesize tests * Adjust constant usage * Once more * Use joinpath --- tests/components/filesize/__init__.py | 4 -- tests/components/filesize/conftest.py | 21 +++------ tests/components/filesize/test_config_flow.py | 44 ++++++++++--------- tests/components/filesize/test_init.py | 18 ++++---- tests/components/filesize/test_sensor.py | 22 +++++----- 5 files changed, 49 insertions(+), 60 deletions(-) diff --git a/tests/components/filesize/__init__.py b/tests/components/filesize/__init__.py index 3e09a745387..b8956d4fb80 100644 --- a/tests/components/filesize/__init__.py +++ b/tests/components/filesize/__init__.py @@ -1,13 +1,9 @@ """Tests for the filesize component.""" -import os from homeassistant.core import HomeAssistant -TEST_DIR = os.path.join(os.path.dirname(__file__)) TEST_FILE_NAME = "mock_file_test_filesize.txt" TEST_FILE_NAME2 = "mock_file_test_filesize2.txt" -TEST_FILE = os.path.join(TEST_DIR, TEST_FILE_NAME) -TEST_FILE2 = os.path.join(TEST_DIR, TEST_FILE_NAME2) async def async_create_file(hass: HomeAssistant, path: str) -> None: diff --git a/tests/components/filesize/conftest.py b/tests/components/filesize/conftest.py index 6584ebc95df..ac36ab687f4 100644 --- a/tests/components/filesize/conftest.py +++ b/tests/components/filesize/conftest.py @@ -2,7 +2,7 @@ from __future__ import annotations from collections.abc import Generator -import os +from pathlib import Path from unittest.mock import patch import pytest @@ -10,19 +10,20 @@ import pytest from homeassistant.components.filesize.const import DOMAIN from homeassistant.const import CONF_FILE_PATH -from . import TEST_FILE, TEST_FILE2, TEST_FILE_NAME +from . import TEST_FILE_NAME from tests.common import MockConfigEntry @pytest.fixture -def mock_config_entry() -> MockConfigEntry: +def mock_config_entry(tmp_path: Path) -> MockConfigEntry: """Return the default mocked config entry.""" + test_file = str(tmp_path.joinpath(TEST_FILE_NAME)) return MockConfigEntry( title=TEST_FILE_NAME, domain=DOMAIN, - data={CONF_FILE_PATH: TEST_FILE}, - unique_id=TEST_FILE, + data={CONF_FILE_PATH: test_file}, + unique_id=test_file, ) @@ -33,13 +34,3 @@ def mock_setup_entry() -> Generator[None, None, None]: "homeassistant.components.filesize.async_setup_entry", return_value=True ): yield - - -@pytest.fixture(autouse=True) -def remove_file() -> None: - """Remove test file.""" - yield - if os.path.isfile(TEST_FILE): - os.remove(TEST_FILE) - if os.path.isfile(TEST_FILE2): - os.remove(TEST_FILE2) diff --git a/tests/components/filesize/test_config_flow.py b/tests/components/filesize/test_config_flow.py index dd99aa2a723..27dec438168 100644 --- a/tests/components/filesize/test_config_flow.py +++ b/tests/components/filesize/test_config_flow.py @@ -1,4 +1,5 @@ """Tests for the Filesize config flow.""" +from pathlib import Path from unittest.mock import patch import pytest @@ -9,54 +10,55 @@ from homeassistant.const import CONF_FILE_PATH from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import TEST_DIR, TEST_FILE, TEST_FILE_NAME, async_create_file +from . import TEST_FILE_NAME, async_create_file from tests.common import MockConfigEntry pytestmark = pytest.mark.usefixtures("mock_setup_entry") -async def test_full_user_flow(hass: HomeAssistant) -> None: +async def test_full_user_flow(hass: HomeAssistant, tmp_path: Path) -> None: """Test the full user configuration flow.""" - await async_create_file(hass, TEST_FILE) - hass.config.allowlist_external_dirs = {TEST_DIR} + test_file = str(tmp_path.joinpath(TEST_FILE_NAME)) + await async_create_file(hass, test_file) + hass.config.allowlist_external_dirs = {tmp_path} result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) assert result.get("type") == FlowResultType.FORM - assert result.get("step_id") == SOURCE_USER + assert result.get("step_id") == "user" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - user_input={CONF_FILE_PATH: TEST_FILE}, + user_input={CONF_FILE_PATH: test_file}, ) assert result2.get("type") == FlowResultType.CREATE_ENTRY assert result2.get("title") == TEST_FILE_NAME - assert result2.get("data") == {CONF_FILE_PATH: TEST_FILE} + assert result2.get("data") == {CONF_FILE_PATH: test_file} async def test_unique_path( - hass: HomeAssistant, - mock_config_entry: MockConfigEntry, + hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path ) -> None: """Test we abort if already setup.""" - await async_create_file(hass, TEST_FILE) - hass.config.allowlist_external_dirs = {TEST_DIR} + test_file = str(tmp_path.joinpath(TEST_FILE_NAME)) + await async_create_file(hass, test_file) + hass.config.allowlist_external_dirs = {tmp_path} mock_config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={CONF_FILE_PATH: TEST_FILE} + DOMAIN, context={"source": SOURCE_USER}, data={CONF_FILE_PATH: test_file} ) assert result.get("type") == FlowResultType.ABORT assert result.get("reason") == "already_configured" -async def test_flow_fails_on_validation(hass: HomeAssistant) -> None: +async def test_flow_fails_on_validation(hass: HomeAssistant, tmp_path: Path) -> None: """Test config flow errors.""" - + test_file = str(tmp_path.joinpath(TEST_FILE_NAME)) hass.config.allowlist_external_dirs = {} result = await hass.config_entries.flow.async_init( @@ -64,18 +66,18 @@ async def test_flow_fails_on_validation(hass: HomeAssistant) -> None: ) assert result["type"] == FlowResultType.FORM - assert result["step_id"] == SOURCE_USER + assert result["step_id"] == "user" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ - CONF_FILE_PATH: TEST_FILE, + CONF_FILE_PATH: test_file, }, ) assert result2["errors"] == {"base": "not_valid"} - await async_create_file(hass, TEST_FILE) + await async_create_file(hass, test_file) with patch( "homeassistant.components.filesize.config_flow.pathlib.Path", @@ -83,25 +85,25 @@ async def test_flow_fails_on_validation(hass: HomeAssistant) -> None: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ - CONF_FILE_PATH: TEST_FILE, + CONF_FILE_PATH: test_file, }, ) assert result2["errors"] == {"base": "not_allowed"} - hass.config.allowlist_external_dirs = {TEST_DIR} + hass.config.allowlist_external_dirs = {tmp_path} with patch( "homeassistant.components.filesize.config_flow.pathlib.Path", ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ - CONF_FILE_PATH: TEST_FILE, + CONF_FILE_PATH: test_file, }, ) assert result2["type"] == FlowResultType.CREATE_ENTRY assert result2["title"] == TEST_FILE_NAME assert result2["data"] == { - CONF_FILE_PATH: TEST_FILE, + CONF_FILE_PATH: test_file, } diff --git a/tests/components/filesize/test_init.py b/tests/components/filesize/test_init.py index 7c0526b8194..c580bb7da77 100644 --- a/tests/components/filesize/test_init.py +++ b/tests/components/filesize/test_init.py @@ -1,5 +1,5 @@ """Tests for the Filesize integration.""" -import py +from pathlib import Path from homeassistant.components.filesize.const import DOMAIN from homeassistant.config_entries import ConfigEntryState @@ -12,12 +12,12 @@ from tests.common import MockConfigEntry async def test_load_unload_config_entry( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmpdir: py.path.local + hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path ) -> None: """Test the Filesize configuration entry loading/unloading.""" - testfile = f"{tmpdir}/file.txt" + testfile = str(tmp_path.joinpath("file.txt")) await async_create_file(hass, testfile) - hass.config.allowlist_external_dirs = {tmpdir} + hass.config.allowlist_external_dirs = {tmp_path} mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( mock_config_entry, unique_id=testfile, data={CONF_FILE_PATH: testfile} @@ -35,12 +35,12 @@ async def test_load_unload_config_entry( async def test_cannot_access_file( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmpdir: py.path.local + hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path ) -> None: """Test that an file not exist is caught.""" mock_config_entry.add_to_hass(hass) - testfile = f"{tmpdir}/file_not_exist.txt" - hass.config.allowlist_external_dirs = {tmpdir} + testfile = str(tmp_path.joinpath("file_not_exist.txt")) + hass.config.allowlist_external_dirs = {tmp_path} hass.config_entries.async_update_entry( mock_config_entry, unique_id=testfile, data={CONF_FILE_PATH: testfile} ) @@ -52,10 +52,10 @@ async def test_cannot_access_file( async def test_not_valid_path_to_file( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmpdir: py.path.local + hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path ) -> None: """Test that an invalid path is caught.""" - testfile = f"{tmpdir}/file.txt" + testfile = str(tmp_path.joinpath("file.txt")) await async_create_file(hass, testfile) mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( diff --git a/tests/components/filesize/test_sensor.py b/tests/components/filesize/test_sensor.py index bef0eceb653..20354df13bd 100644 --- a/tests/components/filesize/test_sensor.py +++ b/tests/components/filesize/test_sensor.py @@ -1,24 +1,24 @@ """The tests for the filesize sensor.""" import os - -import py +from pathlib import Path from homeassistant.const import CONF_FILE_PATH, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_component import async_update_entity -from . import TEST_FILE, TEST_FILE_NAME, async_create_file +from . import TEST_FILE_NAME, async_create_file from tests.common import MockConfigEntry async def test_invalid_path( - hass: HomeAssistant, mock_config_entry: MockConfigEntry + hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path ) -> None: """Test that an invalid path is caught.""" + test_file = str(tmp_path.joinpath(TEST_FILE_NAME)) mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( - mock_config_entry, unique_id=TEST_FILE, data={CONF_FILE_PATH: TEST_FILE} + mock_config_entry, unique_id=test_file, data={CONF_FILE_PATH: test_file} ) state = hass.states.get("sensor." + TEST_FILE_NAME) @@ -26,12 +26,12 @@ async def test_invalid_path( async def test_valid_path( - hass: HomeAssistant, tmpdir: py.path.local, mock_config_entry: MockConfigEntry + hass: HomeAssistant, tmp_path: Path, mock_config_entry: MockConfigEntry ) -> None: """Test for a valid path.""" - testfile = f"{tmpdir}/file.txt" + testfile = str(tmp_path.joinpath("file.txt")) await async_create_file(hass, testfile) - hass.config.allowlist_external_dirs = {tmpdir} + hass.config.allowlist_external_dirs = {tmp_path} mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( mock_config_entry, unique_id=testfile, data={CONF_FILE_PATH: testfile} @@ -48,12 +48,12 @@ async def test_valid_path( async def test_state_unavailable( - hass: HomeAssistant, tmpdir: py.path.local, mock_config_entry: MockConfigEntry + hass: HomeAssistant, tmp_path: Path, mock_config_entry: MockConfigEntry ) -> None: """Verify we handle state unavailable.""" - testfile = f"{tmpdir}/file.txt" + testfile = str(tmp_path.joinpath("file.txt")) await async_create_file(hass, testfile) - hass.config.allowlist_external_dirs = {tmpdir} + hass.config.allowlist_external_dirs = {tmp_path} mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( mock_config_entry, unique_id=testfile, data={CONF_FILE_PATH: testfile}