From 7d87dbe641bccd458a16966e52378800676f2e7b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 16 Sep 2025 11:51:44 -0500 Subject: [PATCH] fixes --- tests/component_tests/conftest.py | 21 ++++++++++++ tests/component_tests/packages/test_init.py | 36 ++++++++------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/tests/component_tests/conftest.py b/tests/component_tests/conftest.py index 2045b03502..1756e4de14 100644 --- a/tests/component_tests/conftest.py +++ b/tests/component_tests/conftest.py @@ -6,6 +6,7 @@ from collections.abc import Callable, Generator from pathlib import Path import sys from typing import Any +from unittest import mock import pytest @@ -135,3 +136,23 @@ def generate_main() -> Generator[Callable[[str | Path], str]]: return CORE.cpp_main_section yield generator + + +@pytest.fixture +def mock_clone_or_update() -> Generator[Any]: + """Mock git.clone_or_update for testing.""" + with mock.patch("esphome.git.clone_or_update") as mock_func: + # Default return value + mock_func.return_value = (Path("/tmp/test"), None) + yield mock_func + + +@pytest.fixture +def mock_load_yaml() -> Generator[Any]: + """Mock yaml_util.load_yaml for testing.""" + from esphome.util import OrderedDict + + with mock.patch("esphome.yaml_util.load_yaml") as mock_func: + # Default return value + mock_func.return_value = OrderedDict({"sensor": []}) + yield mock_func diff --git a/tests/component_tests/packages/test_init.py b/tests/component_tests/packages/test_init.py index fbf12829ef..25493c2770 100644 --- a/tests/component_tests/packages/test_init.py +++ b/tests/component_tests/packages/test_init.py @@ -2,24 +2,20 @@ from pathlib import Path from typing import Any -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock from esphome.components.packages import do_packages_pass from esphome.const import CONF_FILES, CONF_PACKAGES, CONF_REFRESH, CONF_URL -from esphome.util import OrderedDict -@patch("esphome.git.clone_or_update") -@patch("esphome.yaml_util.load_yaml") -@patch("pathlib.Path.is_file") def test_packages_skip_update_true( - mock_is_file: MagicMock, mock_load_yaml: MagicMock, mock_clone_or_update: MagicMock + mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock ) -> None: """Test that packages don't update when skip_update=True.""" # Setup mocks - mock_clone_or_update.return_value = (Path("/tmp/test"), None) - mock_is_file.return_value = True - mock_load_yaml.return_value = OrderedDict({"sensor": []}) + with MagicMock() as mock_is_file: + mock_is_file.return_value = True + Path.is_file = mock_is_file config: dict[str, Any] = { CONF_PACKAGES: { @@ -40,17 +36,14 @@ def test_packages_skip_update_true( assert call_args.kwargs["refresh"] is None -@patch("esphome.git.clone_or_update") -@patch("esphome.yaml_util.load_yaml") -@patch("pathlib.Path.is_file") def test_packages_skip_update_false( - mock_is_file: MagicMock, mock_load_yaml: MagicMock, mock_clone_or_update: MagicMock + mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock ) -> None: """Test that packages update when skip_update=False.""" # Setup mocks - mock_clone_or_update.return_value = (Path("/tmp/test"), None) - mock_is_file.return_value = True - mock_load_yaml.return_value = OrderedDict({"sensor": []}) + with MagicMock() as mock_is_file: + mock_is_file.return_value = True + Path.is_file = mock_is_file config: dict[str, Any] = { CONF_PACKAGES: { @@ -71,17 +64,14 @@ def test_packages_skip_update_false( assert call_args.kwargs["refresh"] == "1d" -@patch("esphome.git.clone_or_update") -@patch("esphome.yaml_util.load_yaml") -@patch("pathlib.Path.is_file") def test_packages_default_no_skip( - mock_is_file: MagicMock, mock_load_yaml: MagicMock, mock_clone_or_update: MagicMock + mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock ) -> None: """Test that packages update by default when skip_update not specified.""" # Setup mocks - mock_clone_or_update.return_value = (Path("/tmp/test"), None) - mock_is_file.return_value = True - mock_load_yaml.return_value = OrderedDict({"sensor": []}) + with MagicMock() as mock_is_file: + mock_is_file.return_value = True + Path.is_file = mock_is_file config: dict[str, Any] = { CONF_PACKAGES: {