mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	fixes
This commit is contained in:
		@@ -14,100 +14,118 @@ from esphome.const import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@patch("esphome.git.clone_or_update")
 | 
			
		||||
@patch("esphome.loader.install_meta_finder")
 | 
			
		||||
def test_external_components_skip_update_true(
 | 
			
		||||
    mock_install_meta: MagicMock, mock_clone_or_update: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that external components don't update when skip_update=True."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    test_path = Path("/tmp/test/components")
 | 
			
		||||
    test_path.mkdir(parents=True, exist_ok=True)
 | 
			
		||||
    mock_clone_or_update.return_value = (test_path.parent, None)
 | 
			
		||||
    # Create a components directory structure
 | 
			
		||||
    components_dir = tmp_path / "components"
 | 
			
		||||
    components_dir.mkdir()
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
            {
 | 
			
		||||
                CONF_SOURCE: {
 | 
			
		||||
                    "type": TYPE_GIT,
 | 
			
		||||
                    CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                },
 | 
			
		||||
                CONF_REFRESH: "1d",
 | 
			
		||||
                "components": "all",
 | 
			
		||||
            }
 | 
			
		||||
        ]
 | 
			
		||||
    }
 | 
			
		||||
    # Create a test component
 | 
			
		||||
    test_component_dir = components_dir / "test_component"
 | 
			
		||||
    test_component_dir.mkdir()
 | 
			
		||||
    (test_component_dir / "__init__.py").write_text("# Test component")
 | 
			
		||||
 | 
			
		||||
    # Call with skip_update=True
 | 
			
		||||
    do_external_components_pass(config, skip_update=True)
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Verify clone_or_update was called with refresh=None
 | 
			
		||||
    mock_clone_or_update.assert_called_once()
 | 
			
		||||
    call_args = mock_clone_or_update.call_args
 | 
			
		||||
    assert call_args.kwargs["refresh"] is None
 | 
			
		||||
    with patch("esphome.loader.install_meta_finder"):
 | 
			
		||||
        config: dict[str, Any] = {
 | 
			
		||||
            CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
                {
 | 
			
		||||
                    CONF_SOURCE: {
 | 
			
		||||
                        "type": TYPE_GIT,
 | 
			
		||||
                        CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                    },
 | 
			
		||||
                    CONF_REFRESH: "1d",
 | 
			
		||||
                    "components": "all",
 | 
			
		||||
                }
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        # Call with skip_update=True
 | 
			
		||||
        do_external_components_pass(config, skip_update=True)
 | 
			
		||||
 | 
			
		||||
        # Verify clone_or_update was called with refresh=None
 | 
			
		||||
        mock_clone_or_update.assert_called_once()
 | 
			
		||||
        call_args = mock_clone_or_update.call_args
 | 
			
		||||
        assert call_args.kwargs["refresh"] is None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@patch("esphome.git.clone_or_update")
 | 
			
		||||
@patch("esphome.loader.install_meta_finder")
 | 
			
		||||
def test_external_components_skip_update_false(
 | 
			
		||||
    mock_install_meta: MagicMock, mock_clone_or_update: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that external components update when skip_update=False."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    test_path = Path("/tmp/test/components")
 | 
			
		||||
    test_path.mkdir(parents=True, exist_ok=True)
 | 
			
		||||
    mock_clone_or_update.return_value = (test_path.parent, None)
 | 
			
		||||
    # Create a components directory structure
 | 
			
		||||
    components_dir = tmp_path / "components"
 | 
			
		||||
    components_dir.mkdir()
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
            {
 | 
			
		||||
                CONF_SOURCE: {
 | 
			
		||||
                    "type": TYPE_GIT,
 | 
			
		||||
                    CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                },
 | 
			
		||||
                CONF_REFRESH: "1d",
 | 
			
		||||
                "components": "all",
 | 
			
		||||
            }
 | 
			
		||||
        ]
 | 
			
		||||
    }
 | 
			
		||||
    # Create a test component
 | 
			
		||||
    test_component_dir = components_dir / "test_component"
 | 
			
		||||
    test_component_dir.mkdir()
 | 
			
		||||
    (test_component_dir / "__init__.py").write_text("# Test component")
 | 
			
		||||
 | 
			
		||||
    # Call with skip_update=False
 | 
			
		||||
    do_external_components_pass(config, skip_update=False)
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Verify clone_or_update was called with actual refresh value
 | 
			
		||||
    mock_clone_or_update.assert_called_once()
 | 
			
		||||
    call_args = mock_clone_or_update.call_args
 | 
			
		||||
    assert call_args.kwargs["refresh"] == "1d"
 | 
			
		||||
    with patch("esphome.loader.install_meta_finder"):
 | 
			
		||||
        config: dict[str, Any] = {
 | 
			
		||||
            CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
                {
 | 
			
		||||
                    CONF_SOURCE: {
 | 
			
		||||
                        "type": TYPE_GIT,
 | 
			
		||||
                        CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                    },
 | 
			
		||||
                    CONF_REFRESH: "1d",
 | 
			
		||||
                    "components": "all",
 | 
			
		||||
                }
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        # Call with skip_update=False
 | 
			
		||||
        do_external_components_pass(config, skip_update=False)
 | 
			
		||||
 | 
			
		||||
        # Verify clone_or_update was called with actual refresh value
 | 
			
		||||
        mock_clone_or_update.assert_called_once()
 | 
			
		||||
        call_args = mock_clone_or_update.call_args
 | 
			
		||||
        assert call_args.kwargs["refresh"] == "1d"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@patch("esphome.git.clone_or_update")
 | 
			
		||||
@patch("esphome.loader.install_meta_finder")
 | 
			
		||||
def test_external_components_default_no_skip(
 | 
			
		||||
    mock_install_meta: MagicMock, mock_clone_or_update: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that external components update by default when skip_update not specified."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    test_path = Path("/tmp/test/components")
 | 
			
		||||
    test_path.mkdir(parents=True, exist_ok=True)
 | 
			
		||||
    mock_clone_or_update.return_value = (test_path.parent, None)
 | 
			
		||||
    # Create a components directory structure
 | 
			
		||||
    components_dir = tmp_path / "components"
 | 
			
		||||
    components_dir.mkdir()
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
            {
 | 
			
		||||
                CONF_SOURCE: {
 | 
			
		||||
                    "type": TYPE_GIT,
 | 
			
		||||
                    CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                },
 | 
			
		||||
                CONF_REFRESH: "1d",
 | 
			
		||||
                "components": "all",
 | 
			
		||||
            }
 | 
			
		||||
        ]
 | 
			
		||||
    }
 | 
			
		||||
    # Create a test component
 | 
			
		||||
    test_component_dir = components_dir / "test_component"
 | 
			
		||||
    test_component_dir.mkdir()
 | 
			
		||||
    (test_component_dir / "__init__.py").write_text("# Test component")
 | 
			
		||||
 | 
			
		||||
    # Call without skip_update parameter
 | 
			
		||||
    do_external_components_pass(config)
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Verify clone_or_update was called with actual refresh value
 | 
			
		||||
    mock_clone_or_update.assert_called_once()
 | 
			
		||||
    call_args = mock_clone_or_update.call_args
 | 
			
		||||
    assert call_args.kwargs["refresh"] == "1d"
 | 
			
		||||
    with patch("esphome.loader.install_meta_finder"):
 | 
			
		||||
        config: dict[str, Any] = {
 | 
			
		||||
            CONF_EXTERNAL_COMPONENTS: [
 | 
			
		||||
                {
 | 
			
		||||
                    CONF_SOURCE: {
 | 
			
		||||
                        "type": TYPE_GIT,
 | 
			
		||||
                        CONF_URL: "https://github.com/test/components",
 | 
			
		||||
                    },
 | 
			
		||||
                    CONF_REFRESH: "1d",
 | 
			
		||||
                    "components": "all",
 | 
			
		||||
                }
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        # Call without skip_update parameter
 | 
			
		||||
        do_external_components_pass(config)
 | 
			
		||||
 | 
			
		||||
        # Verify clone_or_update was called with actual refresh value
 | 
			
		||||
        mock_clone_or_update.assert_called_once()
 | 
			
		||||
        call_args = mock_clone_or_update.call_args
 | 
			
		||||
        assert call_args.kwargs["refresh"] == "1d"
 | 
			
		||||
 
 | 
			
		||||
@@ -6,16 +6,22 @@ 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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_packages_skip_update_true(
 | 
			
		||||
    mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that packages don't update when skip_update=True."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    with MagicMock() as mock_is_file:
 | 
			
		||||
        mock_is_file.return_value = True
 | 
			
		||||
        Path.is_file = mock_is_file
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Create the test yaml file
 | 
			
		||||
    test_file = tmp_path / "test.yaml"
 | 
			
		||||
    test_file.write_text("sensor: []")
 | 
			
		||||
 | 
			
		||||
    # Set mock_load_yaml to return some valid config
 | 
			
		||||
    mock_load_yaml.return_value = OrderedDict({"sensor": []})
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_PACKAGES: {
 | 
			
		||||
@@ -37,13 +43,18 @@ def test_packages_skip_update_true(
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_packages_skip_update_false(
 | 
			
		||||
    mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that packages update when skip_update=False."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    with MagicMock() as mock_is_file:
 | 
			
		||||
        mock_is_file.return_value = True
 | 
			
		||||
        Path.is_file = mock_is_file
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Create the test yaml file
 | 
			
		||||
    test_file = tmp_path / "test.yaml"
 | 
			
		||||
    test_file.write_text("sensor: []")
 | 
			
		||||
 | 
			
		||||
    # Set mock_load_yaml to return some valid config
 | 
			
		||||
    mock_load_yaml.return_value = OrderedDict({"sensor": []})
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_PACKAGES: {
 | 
			
		||||
@@ -65,13 +76,18 @@ def test_packages_skip_update_false(
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_packages_default_no_skip(
 | 
			
		||||
    mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
    tmp_path: Path, mock_clone_or_update: MagicMock, mock_load_yaml: MagicMock
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that packages update by default when skip_update not specified."""
 | 
			
		||||
    # Setup mocks
 | 
			
		||||
    with MagicMock() as mock_is_file:
 | 
			
		||||
        mock_is_file.return_value = True
 | 
			
		||||
        Path.is_file = mock_is_file
 | 
			
		||||
    # Set up mock to return our tmp_path
 | 
			
		||||
    mock_clone_or_update.return_value = (tmp_path, None)
 | 
			
		||||
 | 
			
		||||
    # Create the test yaml file
 | 
			
		||||
    test_file = tmp_path / "test.yaml"
 | 
			
		||||
    test_file.write_text("sensor: []")
 | 
			
		||||
 | 
			
		||||
    # Set mock_load_yaml to return some valid config
 | 
			
		||||
    mock_load_yaml.return_value = OrderedDict({"sensor": []})
 | 
			
		||||
 | 
			
		||||
    config: dict[str, Any] = {
 | 
			
		||||
        CONF_PACKAGES: {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user