1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-22 21:22:22 +01:00
This commit is contained in:
J. Nick Koston
2025-09-16 11:51:44 -05:00
parent 8e13335ff6
commit 7d87dbe641
2 changed files with 34 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ from collections.abc import Callable, Generator
from pathlib import Path from pathlib import Path
import sys import sys
from typing import Any from typing import Any
from unittest import mock
import pytest import pytest
@@ -135,3 +136,23 @@ def generate_main() -> Generator[Callable[[str | Path], str]]:
return CORE.cpp_main_section return CORE.cpp_main_section
yield generator 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

View File

@@ -2,24 +2,20 @@
from pathlib import Path from pathlib import Path
from typing import Any 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.components.packages import do_packages_pass
from esphome.const import CONF_FILES, CONF_PACKAGES, CONF_REFRESH, CONF_URL 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( 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: ) -> None:
"""Test that packages don't update when skip_update=True.""" """Test that packages don't update when skip_update=True."""
# Setup mocks # Setup mocks
mock_clone_or_update.return_value = (Path("/tmp/test"), None) with MagicMock() as mock_is_file:
mock_is_file.return_value = True mock_is_file.return_value = True
mock_load_yaml.return_value = OrderedDict({"sensor": []}) Path.is_file = mock_is_file
config: dict[str, Any] = { config: dict[str, Any] = {
CONF_PACKAGES: { CONF_PACKAGES: {
@@ -40,17 +36,14 @@ def test_packages_skip_update_true(
assert call_args.kwargs["refresh"] is None 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( 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: ) -> None:
"""Test that packages update when skip_update=False.""" """Test that packages update when skip_update=False."""
# Setup mocks # Setup mocks
mock_clone_or_update.return_value = (Path("/tmp/test"), None) with MagicMock() as mock_is_file:
mock_is_file.return_value = True mock_is_file.return_value = True
mock_load_yaml.return_value = OrderedDict({"sensor": []}) Path.is_file = mock_is_file
config: dict[str, Any] = { config: dict[str, Any] = {
CONF_PACKAGES: { CONF_PACKAGES: {
@@ -71,17 +64,14 @@ def test_packages_skip_update_false(
assert call_args.kwargs["refresh"] == "1d" 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( 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: ) -> None:
"""Test that packages update by default when skip_update not specified.""" """Test that packages update by default when skip_update not specified."""
# Setup mocks # Setup mocks
mock_clone_or_update.return_value = (Path("/tmp/test"), None) with MagicMock() as mock_is_file:
mock_is_file.return_value = True mock_is_file.return_value = True
mock_load_yaml.return_value = OrderedDict({"sensor": []}) Path.is_file = mock_is_file
config: dict[str, Any] = { config: dict[str, Any] = {
CONF_PACKAGES: { CONF_PACKAGES: {