mirror of
https://github.com/esphome/esphome.git
synced 2025-09-22 21:22:22 +01:00
fixes
This commit is contained in:
@@ -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
|
||||
|
@@ -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: {
|
||||
|
Reference in New Issue
Block a user