1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-01 19:02:18 +01:00

Don't compile unnecessary platform files (e.g. ESP8266 files on ESP32) (#9354)

This commit is contained in:
J. Nick Koston
2025-07-07 16:04:41 -05:00
committed by GitHub
parent b122112d58
commit 440de12e3f
25 changed files with 657 additions and 12 deletions

View File

@@ -0,0 +1,135 @@
"""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, get_logger_level
from esphome.const import (
CONF_LEVEL,
CONF_LOGGER,
KEY_CORE,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
PlatformFramework,
)
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: list[str] = 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() -> None:
"""Test that filter_source_files_from_platform correctly filters files for HOST 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 Host platform
mock_core_data: dict[str, dict[str, str]] = {
KEY_CORE: {
KEY_TARGET_PLATFORM: "host",
KEY_TARGET_FRAMEWORK: "host", # Framework.NATIVE is "host"
}
}
with patch("esphome.config_helpers.CORE.data", mock_core_data):
excluded: list[str] = filter_func()
# Host should exclude ESP32 and ESP8266 files
assert "logger_esp32.cpp" in excluded
assert "logger_esp8266.cpp" in excluded
# But not host or common files
assert "logger_host.cpp" not in excluded
assert "logger_common.cpp" not in excluded
def test_filter_source_files_from_platform_handles_missing_data() -> None:
"""Test that filter_source_files_from_platform returns empty list when platform/framework data is missing."""
# Define test file mappings
files_map: dict[str, set[PlatformFramework]] = {
"logger_esp32.cpp": {PlatformFramework.ESP32_ARDUINO},
"logger_host.cpp": {PlatformFramework.HOST_NATIVE},
}
# Create the filter function
filter_func: Callable[[], list[str]] = filter_source_files_from_platform(files_map)
# Test case: Missing platform/framework data
mock_core_data: dict[str, dict[str, str]] = {KEY_CORE: {}}
with patch("esphome.config_helpers.CORE.data", mock_core_data):
excluded: list[str] = filter_func()
# Should return empty list when platform/framework not set
assert excluded == []
def test_get_logger_level() -> None:
"""Test get_logger_level helper function."""
# Test no logger config - should return default DEBUG
mock_config = {}
with patch("esphome.config_helpers.CORE.config", mock_config):
assert get_logger_level() == "DEBUG"
# Test with logger set to INFO
mock_config = {CONF_LOGGER: {CONF_LEVEL: "INFO"}}
with patch("esphome.config_helpers.CORE.config", mock_config):
assert get_logger_level() == "INFO"
# Test with VERY_VERBOSE
mock_config = {CONF_LOGGER: {CONF_LEVEL: "VERY_VERBOSE"}}
with patch("esphome.config_helpers.CORE.config", mock_config):
assert get_logger_level() == "VERY_VERBOSE"
# Test with logger missing level (uses default DEBUG)
mock_config = {CONF_LOGGER: {}}
with patch("esphome.config_helpers.CORE.config", mock_config):
assert get_logger_level() == "DEBUG"

View File

@@ -0,0 +1,63 @@
"""Unit tests for esphome.loader module."""
from unittest.mock import MagicMock, patch
from esphome.loader import ComponentManifest
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()
mock_module.FILTER_SOURCE_FILES = lambda: [
"platform_esp32.cpp",
"platform_esp8266.cpp",
]
mock_module.__package__ = "esphome.components.test_component"
# Create ComponentManifest instance
manifest = ComponentManifest(mock_module)
# Mock the files in the package
def create_mock_file(filename: str) -> MagicMock:
mock_file = MagicMock()
mock_file.name = filename
mock_file.is_file.return_value = True
return mock_file
mock_files = [
create_mock_file("test.cpp"),
create_mock_file("test.h"),
create_mock_file("platform_esp32.cpp"),
create_mock_file("platform_esp8266.cpp"),
create_mock_file("common.cpp"),
create_mock_file("README.md"), # Should be excluded by extension
]
# Mock importlib.resources
with patch("importlib.resources.files") as mock_files_func:
mock_package_files = MagicMock()
mock_package_files.iterdir.return_value = mock_files
mock_package_files.joinpath = lambda name: MagicMock(is_file=lambda: True)
mock_files_func.return_value = mock_package_files
# Get resources
resources = manifest.resources
# Convert to list of filenames for easier testing
resource_names = [r.resource for r in resources]
# Check that platform files are excluded
assert "platform_esp32.cpp" not in resource_names
assert "platform_esp8266.cpp" not in resource_names
# Check that other source files are included
assert "test.cpp" in resource_names
assert "test.h" in resource_names
assert "common.cpp" in resource_names
# Check that non-source files are excluded
assert "README.md" not in resource_names
# Verify the correct number of resources
assert len(resources) == 3 # test.cpp, test.h, common.cpp