1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 14:43:51 +00:00

Merge branch 'wifi_sta_fixed' into integration

This commit is contained in:
J. Nick Koston
2025-10-21 13:12:12 -10:00
29 changed files with 807 additions and 108 deletions

5
tests/components/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
# Gitignore settings for ESPHome
# This is an example and may include too much for your use-case.
# You can modify this file to suit your needs.
/.esphome/
/secrets.yaml

View File

@@ -0,0 +1,32 @@
# How to write C++ ESPHome unit tests
1. Locate the folder with your component or create a new one with the same name as the component.
2. Write the tests. You can add as many `.cpp` and `.h` files as you need to organize your tests.
**IMPORTANT**: wrap all your testing code in a unique namespace to avoid linker collisions when compiling
testing binaries that combine many components. By convention, this unique namespace is `esphome::component::testing`
(where "component" is the component under test), for example: `esphome::uart::testing`.
## Running component unit tests
(from the repository root)
```bash
./script/cpp_unit_test.py component1 component2 ...
```
The above will compile and run the provided components and their tests.
To run all tests, you can invoke `cpp_unit_test.py` with the special `--all` flag:
```bash
./script/cpp_unit_test.py --all
```
To run a specific test suite, you can provide a Google Test filter:
```bash
GTEST_FILTER='UART*' ./script/cpp_unit_test.py uart modbus
```
The process will return `0` for success or nonzero for failure. In case of failure, the errors will be printed out to the console.

26
tests/components/main.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <gtest/gtest.h>
/*
This special main.cpp replaces the default one.
It will run all the Google Tests found in all compiled cpp files and then exit with the result
See README.md for more information
*/
// Auto generated code by esphome
// ========== AUTO GENERATED INCLUDE BLOCK BEGIN ===========
// ========== AUTO GENERATED INCLUDE BLOCK END ==========="
void original_setup() {
// This function won't be run.
// ========== AUTO GENERATED CODE BEGIN ===========
// =========== AUTO GENERATED CODE END ============
}
void setup() {
::testing::InitGoogleTest();
int exit_code = RUN_ALL_TESTS();
exit(exit_code);
}
void loop() {}

View File

@@ -0,0 +1,37 @@
#pragma once
#include <vector>
#include <cstdint>
#include <cstring>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "esphome/components/uart/uart_component.h"
namespace esphome::uart::testing {
using ::testing::_;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::SetArgPointee;
// Derive a mock from UARTComponent to test the wrapper implementations.
class MockUARTComponent : public UARTComponent {
public:
using UARTComponent::write_array;
using UARTComponent::write_byte;
// NOTE: std::vector is used here for test convenience. For production code,
// consider using StaticVector or FixedVector from esphome/core/helpers.h instead.
std::vector<uint8_t> written_data;
void write_array(const uint8_t *data, size_t len) override { written_data.assign(data, data + len); }
MOCK_METHOD(bool, read_array, (uint8_t * data, size_t len), (override));
MOCK_METHOD(bool, peek_byte, (uint8_t * data), (override));
MOCK_METHOD(int, available, (), (override));
MOCK_METHOD(void, flush, (), (override));
MOCK_METHOD(void, check_logger_conflict, (), (override));
};
} // namespace esphome::uart::testing

View File

@@ -0,0 +1,73 @@
#include "common.h"
namespace esphome::uart::testing {
TEST(UARTComponentTest, SetGetBaudRate) {
MockUARTComponent mock;
mock.set_baud_rate(38400);
EXPECT_EQ(mock.get_baud_rate(), 38400);
}
TEST(UARTComponentTest, SetGetStopBits) {
MockUARTComponent mock;
mock.set_stop_bits(2);
EXPECT_EQ(mock.get_stop_bits(), 2);
}
TEST(UARTComponentTest, SetGetDataBits) {
MockUARTComponent mock;
mock.set_data_bits(7);
EXPECT_EQ(mock.get_data_bits(), 7);
}
TEST(UARTComponentTest, SetGetParity) {
MockUARTComponent mock;
mock.set_parity(UARTParityOptions::UART_CONFIG_PARITY_EVEN);
EXPECT_EQ(mock.get_parity(), UARTParityOptions::UART_CONFIG_PARITY_EVEN);
}
TEST(UARTComponentTest, SetGetRxBufferSize) {
MockUARTComponent mock;
mock.set_rx_buffer_size(128);
EXPECT_EQ(mock.get_rx_buffer_size(), 128);
}
TEST(UARTComponentTest, WriteArrayVector) {
MockUARTComponent mock;
std::vector<uint8_t> data = {10, 20, 30};
mock.write_array(data);
EXPECT_EQ(mock.written_data, data);
}
TEST(UARTComponentTest, WriteByte) {
MockUARTComponent mock;
uint8_t byte = 0x79;
mock.write_byte(byte);
EXPECT_EQ(mock.written_data.size(), 1);
EXPECT_EQ(mock.written_data[0], byte);
}
TEST(UARTComponentTest, WriteStr) {
MockUARTComponent mock;
const char *str = "Hello";
std::vector<uint8_t> captured;
mock.write_str(str);
EXPECT_EQ(mock.written_data.size(), strlen(str));
EXPECT_EQ(0, strncmp(str, (const char *) mock.written_data.data(), mock.written_data.size()));
}
// Tests for wrapper methods forwarding to pure virtual read_array
TEST(UARTComponentTest, ReadByteSuccess) {
MockUARTComponent mock;
uint8_t value = 0;
EXPECT_CALL(mock, read_array(&value, 1)).WillOnce(Return(true));
EXPECT_TRUE(mock.read_byte(&value));
}
TEST(UARTComponentTest, ReadByteFailure) {
MockUARTComponent mock;
uint8_t value = 0xFF;
EXPECT_CALL(mock, read_array(&value, 1)).WillOnce(Return(false));
EXPECT_FALSE(mock.read_byte(&value));
}
} // namespace esphome::uart::testing

View File

@@ -0,0 +1,108 @@
#include "common.h"
#include "esphome/components/uart/uart.h"
namespace esphome::uart::testing {
TEST(UARTDeviceTest, ReadByteSuccess) {
MockUARTComponent mock;
UARTDevice dev(&mock);
uint8_t value = 0;
EXPECT_CALL(mock, read_array(_, 1)).WillOnce(DoAll(SetArgPointee<0>(0x5A), Return(true)));
bool result = dev.read_byte(&value);
EXPECT_TRUE(result);
EXPECT_EQ(value, 0x5A);
}
TEST(UARTDeviceTest, ReadByteFailure) {
MockUARTComponent mock;
UARTDevice dev(&mock);
uint8_t value = 0xFF;
EXPECT_CALL(mock, read_array(_, 1)).WillOnce(Return(false));
bool result = dev.read_byte(&value);
EXPECT_FALSE(result);
}
TEST(UARTDeviceTest, PeekByteSuccess) {
MockUARTComponent mock;
UARTDevice dev(&mock);
uint8_t value = 0;
EXPECT_CALL(mock, peek_byte(_)).WillOnce(DoAll(SetArgPointee<0>(0xA5), Return(true)));
bool result = dev.peek_byte(&value);
EXPECT_TRUE(result);
EXPECT_EQ(value, 0xA5);
}
TEST(UARTDeviceTest, PeekByteFailure) {
MockUARTComponent mock;
UARTDevice dev(&mock);
uint8_t value = 0;
EXPECT_CALL(mock, peek_byte(_)).WillOnce(Return(false));
bool result = dev.peek_byte(&value);
EXPECT_FALSE(result);
}
TEST(UARTDeviceTest, Available) {
MockUARTComponent mock;
UARTDevice dev(&mock);
EXPECT_CALL(mock, available()).WillOnce(Return(5));
EXPECT_EQ(dev.available(), 5);
}
TEST(UARTDeviceTest, FlushCallsParent) {
MockUARTComponent mock;
UARTDevice dev(&mock);
EXPECT_CALL(mock, flush()).Times(1);
dev.flush();
}
TEST(UARTDeviceTest, WriteByteForwardsToWriteArray) {
MockUARTComponent mock;
UARTDevice dev(&mock);
dev.write_byte(0xAB);
EXPECT_EQ(mock.written_data.size(), 1);
EXPECT_EQ(mock.written_data[0], 0xAB);
}
TEST(UARTDeviceTest, WriteArrayPointer) {
MockUARTComponent mock;
UARTDevice dev(&mock);
uint8_t data[3] = {1, 2, 3};
dev.write_array(data, 3);
EXPECT_EQ(mock.written_data.size(), 3);
EXPECT_EQ(mock.written_data, std::vector(data, data + 3));
}
TEST(UARTDeviceTest, WriteArrayVector) {
MockUARTComponent mock;
UARTDevice dev(&mock);
std::vector<uint8_t> data = {4, 5, 6};
dev.write_array(data);
EXPECT_EQ(mock.written_data, data);
}
TEST(UARTDeviceTest, WriteArrayStdArray) {
MockUARTComponent mock;
UARTDevice dev(&mock);
std::array<uint8_t, 4> data = {7, 8, 9, 10};
dev.write_array(data);
EXPECT_EQ(mock.written_data.size(), data.size());
EXPECT_EQ(mock.written_data, std::vector(data.begin(), data.end()));
}
TEST(UARTDeviceTest, WriteStrForwardsToWriteArray) {
MockUARTComponent mock;
UARTDevice dev(&mock);
const char *str = "ESPHome";
dev.write_str(str);
EXPECT_EQ(mock.written_data.size(), strlen(str));
EXPECT_EQ(0, strncmp(str, (const char *) mock.written_data.data(), mock.written_data.size()));
}
TEST(UARTDeviceTest, WriteStrEmptyString) {
MockUARTComponent mock;
UARTDevice dev(&mock);
const char *str = "";
dev.write_str(str);
EXPECT_EQ(mock.written_data.size(), 0);
}
} // namespace esphome::uart::testing

View File

@@ -12,5 +12,8 @@ esphome:
- logger.log: "Failed to connect to WiFi!"
wifi:
ssid: MySSID
password: password1
networks:
- ssid: MySSID
password: password1
- ssid: MySSID2
password: password2

View File

@@ -5,7 +5,6 @@ import importlib.util
import json
import os
from pathlib import Path
import subprocess
import sys
from unittest.mock import Mock, call, patch
@@ -56,9 +55,9 @@ def mock_should_run_python_linters() -> Generator[Mock, None, None]:
@pytest.fixture
def mock_subprocess_run() -> Generator[Mock, None, None]:
"""Mock subprocess.run for list-components.py calls."""
with patch.object(determine_jobs.subprocess, "run") as mock:
def mock_determine_cpp_unit_tests() -> Generator[Mock, None, None]:
"""Mock determine_cpp_unit_tests from helpers."""
with patch.object(determine_jobs, "determine_cpp_unit_tests") as mock:
yield mock
@@ -82,8 +81,8 @@ def test_main_all_tests_should_run(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
mock_determine_cpp_unit_tests: Mock,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -95,6 +94,7 @@ def test_main_all_tests_should_run(
mock_should_run_clang_tidy.return_value = True
mock_should_run_clang_format.return_value = True
mock_should_run_python_linters.return_value = True
mock_determine_cpp_unit_tests.return_value = (False, ["wifi", "api", "sensor"])
# Mock changed_files to return non-component files (to avoid memory impact)
# Memory impact only runs when component C++ files change
@@ -114,15 +114,15 @@ def test_main_all_tests_should_run(
),
patch.object(
determine_jobs,
"filter_component_files",
"filter_component_and_test_files",
side_effect=lambda f: f.startswith("esphome/components/"),
),
patch.object(
determine_jobs,
"get_components_with_dependencies",
side_effect=lambda files, deps: ["wifi", "api"]
if not deps
else ["wifi", "api", "sensor"],
side_effect=lambda files, deps: (
["wifi", "api"] if not deps else ["wifi", "api", "sensor"]
),
),
):
determine_jobs.main()
@@ -150,6 +150,8 @@ def test_main_all_tests_should_run(
# memory_impact should be false (no component C++ files changed)
assert "memory_impact" in output
assert output["memory_impact"]["should_run"] == "false"
assert output["cpp_unit_tests_run_all"] is False
assert output["cpp_unit_tests_components"] == ["wifi", "api", "sensor"]
def test_main_no_tests_should_run(
@@ -157,8 +159,8 @@ def test_main_no_tests_should_run(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
mock_determine_cpp_unit_tests: Mock,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -170,6 +172,7 @@ def test_main_no_tests_should_run(
mock_should_run_clang_tidy.return_value = False
mock_should_run_clang_format.return_value = False
mock_should_run_python_linters.return_value = False
mock_determine_cpp_unit_tests.return_value = (False, [])
# Mock changed_files to return no component files
mock_changed_files.return_value = []
@@ -178,7 +181,9 @@ def test_main_no_tests_should_run(
with (
patch("sys.argv", ["determine-jobs.py"]),
patch.object(determine_jobs, "get_changed_components", return_value=[]),
patch.object(determine_jobs, "filter_component_files", return_value=False),
patch.object(
determine_jobs, "filter_component_and_test_files", return_value=False
),
patch.object(
determine_jobs, "get_components_with_dependencies", return_value=[]
),
@@ -202,31 +207,8 @@ def test_main_no_tests_should_run(
# memory_impact should be present
assert "memory_impact" in output
assert output["memory_impact"]["should_run"] == "false"
def test_main_list_components_fails(
mock_should_run_integration_tests: Mock,
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test when list-components.py fails."""
mock_should_run_integration_tests.return_value = True
mock_should_run_clang_tidy.return_value = True
mock_should_run_clang_format.return_value = True
mock_should_run_python_linters.return_value = True
# Mock list-components.py failure
mock_subprocess_run.side_effect = subprocess.CalledProcessError(1, "cmd")
# Run main function with mocked argv - should raise
with (
patch("sys.argv", ["determine-jobs.py"]),
pytest.raises(subprocess.CalledProcessError),
):
determine_jobs.main()
assert output["cpp_unit_tests_run_all"] is False
assert output["cpp_unit_tests_components"] == []
def test_main_with_branch_argument(
@@ -234,8 +216,8 @@ def test_main_with_branch_argument(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
mock_determine_cpp_unit_tests: Mock,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -247,6 +229,7 @@ def test_main_with_branch_argument(
mock_should_run_clang_tidy.return_value = True
mock_should_run_clang_format.return_value = False
mock_should_run_python_linters.return_value = True
mock_determine_cpp_unit_tests.return_value = (False, ["mqtt"])
# Mock changed_files to return non-component files (to avoid memory impact)
# Memory impact only runs when component C++ files change
@@ -258,7 +241,7 @@ def test_main_with_branch_argument(
patch.object(determine_jobs, "get_changed_components", return_value=["mqtt"]),
patch.object(
determine_jobs,
"filter_component_files",
"filter_component_and_test_files",
side_effect=lambda f: f.startswith("esphome/components/"),
),
patch.object(
@@ -296,6 +279,8 @@ def test_main_with_branch_argument(
# memory_impact should be false (no component C++ files changed)
assert "memory_impact" in output
assert output["memory_impact"]["should_run"] == "false"
assert output["cpp_unit_tests_run_all"] is False
assert output["cpp_unit_tests_components"] == ["mqtt"]
def test_should_run_integration_tests(
@@ -506,7 +491,6 @@ def test_main_filters_components_without_tests(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
@@ -556,16 +540,17 @@ def test_main_filters_components_without_tests(
),
patch.object(
determine_jobs,
"filter_component_files",
"filter_component_and_test_files",
side_effect=lambda f: f.startswith("esphome/components/"),
),
patch.object(
determine_jobs,
"get_components_with_dependencies",
side_effect=lambda files, deps: ["wifi", "sensor"]
if not deps
else ["wifi", "sensor", "airthings_ble"],
side_effect=lambda files, deps: (
["wifi", "sensor"] if not deps else ["wifi", "sensor", "airthings_ble"]
),
),
patch.object(determine_jobs, "changed_files", return_value=[]),
):
# Clear the cache since we're mocking root_path
determine_jobs._component_has_tests.cache_clear()
@@ -808,7 +793,6 @@ def test_clang_tidy_mode_full_scan(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
@@ -829,7 +813,9 @@ def test_clang_tidy_mode_full_scan(
patch("sys.argv", ["determine-jobs.py"]),
patch.object(determine_jobs, "_is_clang_tidy_full_scan", return_value=True),
patch.object(determine_jobs, "get_changed_components", return_value=[]),
patch.object(determine_jobs, "filter_component_files", return_value=False),
patch.object(
determine_jobs, "filter_component_and_test_files", return_value=False
),
patch.object(
determine_jobs, "get_components_with_dependencies", return_value=[]
),
@@ -873,7 +859,6 @@ def test_clang_tidy_mode_targeted_scan(
mock_should_run_clang_tidy: Mock,
mock_should_run_clang_format: Mock,
mock_should_run_python_linters: Mock,
mock_subprocess_run: Mock,
mock_changed_files: Mock,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
@@ -912,7 +897,7 @@ def test_clang_tidy_mode_targeted_scan(
patch.object(determine_jobs, "get_changed_components", return_value=components),
patch.object(
determine_jobs,
"filter_component_files",
"filter_component_and_test_files",
side_effect=lambda f: f.startswith("esphome/components/"),
),
patch.object(