some tests

This commit is contained in:
J. Nick Koston 2025-07-06 13:51:17 -05:00
parent 28886a896b
commit 8d8db11dd9
No known key found for this signature in database
2 changed files with 58 additions and 23 deletions

View File

@ -1,5 +1,6 @@
"""Unit tests for esphome.config_helpers module."""
from collections.abc import Callable
from unittest.mock import patch
from esphome.config_helpers import filter_source_files_from_platform
@ -11,8 +12,47 @@ from esphome.const import (
)
def test_filter_source_files_from_platform():
"""Test that filter_source_files_from_platform correctly filters files based on platform."""
def test_filter_source_files_from_platform_esp32() -> None:
"""Test that filter_source_files_from_platform correctly filters files for ESP32 platform."""
# Define test file mappings
files_map: dict[str, set[PlatformFramework]] = {
"logger_esp32.cpp": {
PlatformFramework.ESP32_ARDUINO,
PlatformFramework.ESP32_IDF,
},
"logger_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
"logger_host.cpp": {PlatformFramework.HOST_NATIVE},
"logger_common.cpp": {
PlatformFramework.ESP32_ARDUINO,
PlatformFramework.ESP32_IDF,
PlatformFramework.ESP8266_ARDUINO,
PlatformFramework.HOST_NATIVE,
},
}
# Create the filter function
filter_func: Callable[[], list[str]] = filter_source_files_from_platform(files_map)
# Test ESP32 with Arduino framework
mock_core_data: dict[str, dict[str, str]] = {
KEY_CORE: {
KEY_TARGET_PLATFORM: "esp32",
KEY_TARGET_FRAMEWORK: "arduino",
}
}
with patch("esphome.config_helpers.CORE.data", mock_core_data):
excluded = filter_func()
# ESP32 Arduino should exclude ESP8266 and HOST files
assert "logger_esp8266.cpp" in excluded
assert "logger_host.cpp" in excluded
# But not ESP32 or common files
assert "logger_esp32.cpp" not in excluded
assert "logger_common.cpp" not in excluded
def test_filter_source_files_from_platform_host():
"""Test that filter_source_files_from_platform correctly filters files for HOST platform."""
# Define test file mappings
files_map = {
"logger_esp32.cpp": {
@ -32,24 +72,7 @@ def test_filter_source_files_from_platform():
# Create the filter function
filter_func = filter_source_files_from_platform(files_map)
# Test case 1: ESP32 with Arduino framework
mock_core_data = {
KEY_CORE: {
KEY_TARGET_PLATFORM: "esp32",
KEY_TARGET_FRAMEWORK: "arduino",
}
}
with patch("esphome.config_helpers.CORE.data", mock_core_data):
excluded = filter_func()
# ESP32 Arduino should exclude ESP8266 and HOST files
assert "logger_esp8266.cpp" in excluded
assert "logger_host.cpp" in excluded
# But not ESP32 or common files
assert "logger_esp32.cpp" not in excluded
assert "logger_common.cpp" not in excluded
# Test case 2: Host platform
# Test Host platform
mock_core_data = {
KEY_CORE: {
KEY_TARGET_PLATFORM: "host",
@ -66,7 +89,19 @@ def test_filter_source_files_from_platform():
assert "logger_host.cpp" not in excluded
assert "logger_common.cpp" not in excluded
# Test case 3: Missing platform/framework data
def test_filter_source_files_from_platform_handles_missing_data():
"""Test that filter_source_files_from_platform returns empty list when platform/framework data is missing."""
# Define test file mappings
files_map = {
"logger_esp32.cpp": {PlatformFramework.ESP32_ARDUINO},
"logger_host.cpp": {PlatformFramework.HOST_NATIVE},
}
# Create the filter function
filter_func = filter_source_files_from_platform(files_map)
# Test case: Missing platform/framework data
mock_core_data = {KEY_CORE: {}}
with patch("esphome.config_helpers.CORE.data", mock_core_data):

View File

@ -5,7 +5,7 @@ from unittest.mock import MagicMock, patch
from esphome.loader import ComponentManifest
def test_component_manifest_resources_with_filter_source_files():
def test_component_manifest_resources_with_filter_source_files() -> None:
"""Test that ComponentManifest.resources correctly filters out excluded files."""
# Create a mock module with FILTER_SOURCE_FILES function
mock_module = MagicMock()
@ -19,7 +19,7 @@ def test_component_manifest_resources_with_filter_source_files():
manifest = ComponentManifest(mock_module)
# Mock the files in the package
def create_mock_file(filename):
def create_mock_file(filename: str) -> MagicMock:
mock_file = MagicMock()
mock_file.name = filename
mock_file.is_file.return_value = True