mirror of
https://github.com/esphome/esphome.git
synced 2025-09-11 15:52:20 +01:00
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""Unit tests for esphome.config_helpers module."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
from esphome.config_helpers import filter_source_files_from_platform
|
|
from esphome.const import (
|
|
KEY_CORE,
|
|
KEY_TARGET_FRAMEWORK,
|
|
KEY_TARGET_PLATFORM,
|
|
PlatformFramework,
|
|
)
|
|
|
|
|
|
def test_filter_source_files_from_platform():
|
|
"""Test that filter_source_files_from_platform correctly filters files based on platform."""
|
|
# Define test file mappings
|
|
files_map = {
|
|
"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 = 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
|
|
mock_core_data = {
|
|
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 = 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
|
|
|
|
# Test case 3: Missing platform/framework data
|
|
mock_core_data = {KEY_CORE: {}}
|
|
|
|
with patch("esphome.config_helpers.CORE.data", mock_core_data):
|
|
excluded = filter_func()
|
|
# Should return empty list when platform/framework not set
|
|
assert excluded == []
|