refactor: mock serial platform backend in test_serial

test_serial mocked log_write.h, which serial.c never calls.
Split celrs_serial into celrs_serial (platform-agnostic logic)
and celrs_serial_platform (real Win/POSIX backend), matching
the celrs_logger/celrs_log_write split.

test_serial now mocks celrs/platform/serial_internal.h and
links only celrs_serial, so the list-ports tests verify the
max_ports clamping and pass-through logic without hitting the
real registry or /dev.
This commit is contained in:
2026-06-14 20:18:17 +02:00
parent a0868cd3b7
commit 2761bcb16c
4 changed files with 22 additions and 16 deletions
+10 -3
View File
@@ -2,14 +2,21 @@ add_library(celrs_crsf STATIC crsf.c)
target_include_directories(celrs_crsf PUBLIC "${CMAKE_SOURCE_DIR}")
target_compile_features(celrs_crsf PRIVATE c_std_23)
# Platform-agnostic serial logic — calls cel_serial_platform_*();
# symbol resolved by celrs_serial_platform (or a mock in tests)
add_library(celrs_serial STATIC serial.c)
target_include_directories(celrs_serial PUBLIC "${CMAKE_SOURCE_DIR}")
target_compile_features(celrs_serial PRIVATE c_std_23)
# Real platform backend — linked into production binaries only
add_library(celrs_serial_platform STATIC)
target_include_directories(celrs_serial_platform PUBLIC "${CMAKE_SOURCE_DIR}")
target_compile_features(celrs_serial_platform PRIVATE c_std_23)
if (IS_WINDOWS)
target_sources(celrs_serial PRIVATE platform/serial_win.c)
target_link_libraries(celrs_serial PRIVATE advapi32)
target_sources(celrs_serial_platform PRIVATE platform/serial_win.c)
target_link_libraries(celrs_serial_platform PRIVATE advapi32)
elseif(IS_LINUX OR IS_MACOS)
target_sources(celrs_serial PRIVATE platform/serial_posix.c)
target_sources(celrs_serial_platform PRIVATE platform/serial_posix.c)
endif()
# Level-filtering logger — calls log_write(); symbol resolved by the final binary
+2 -2
View File
@@ -38,12 +38,12 @@ target_compile_features(test_crsf PRIVATE c_std_23)
add_test(NAME test_crsf COMMAND test_crsf)
list(APPEND TEST_TARGETS test_crsf)
# Serial tests — mocks log_write.h for any logging calls
# Serial tests — mocks the platform backend (serial_internal.h)
add_executable(test_serial test_serial.c)
target_include_directories(test_serial PRIVATE "${CMAKE_SOURCE_DIR}")
target_link_libraries(test_serial PRIVATE celrs_serial Unity::Unity CMock::CMock)
target_compile_features(test_serial PRIVATE c_std_23)
cmock_generate_mock(test_serial "${CMAKE_SOURCE_DIR}/celrs/log_write.h")
cmock_generate_mock(test_serial "${CMAKE_SOURCE_DIR}/celrs/platform/serial_internal.h")
add_test(NAME test_serial COMMAND test_serial)
list(APPEND TEST_TARGETS test_serial)
+9 -10
View File
@@ -2,10 +2,10 @@
#include "unity.h"
#include "celrs/serial.h"
#include "Mocklog_write.h"
#include "Mockserial_internal.h"
void setUp(void) { Mocklog_write_Init(); }
void tearDown(void) { Mocklog_write_Verify(); Mocklog_write_Destroy(); }
void setUp(void) { Mockserial_internal_Init(); }
void tearDown(void) { Mockserial_internal_Verify(); Mockserial_internal_Destroy(); }
void test_open_valid_path(void) {
cel_serial_port* port = cel_serial_open("COM3", 400000);
@@ -64,19 +64,18 @@ void test_list_ports_null_out(void) {
TEST_ASSERT_EQUAL_INT(-1, cel_serial_list_ports(NULL, 16));
}
void test_list_ports_runs_without_crash(void) {
void test_list_ports_passes_max_ports_through(void) {
char** ports = NULL;
cel_serial_platform_list_ports_ExpectAndReturn(&ports, 16, 2);
int count = cel_serial_list_ports(&ports, 16);
/* count >= 0 is valid (may be 0 if no ports available) */
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, count);
cel_serial_free_ports(ports, count);
TEST_ASSERT_EQUAL_INT(2, count);
}
void test_list_ports_zero_max_uses_default(void) {
char** ports = NULL;
cel_serial_platform_list_ports_ExpectAndReturn(&ports, 64, 0);
int count = cel_serial_list_ports(&ports, 0);
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, count);
cel_serial_free_ports(ports, count);
TEST_ASSERT_EQUAL_INT(0, count);
}
void test_free_ports_null(void) {
@@ -99,7 +98,7 @@ int main(void) {
RUN_TEST(test_flush_no_crash);
RUN_TEST(test_flush_null);
RUN_TEST(test_list_ports_null_out);
RUN_TEST(test_list_ports_runs_without_crash);
RUN_TEST(test_list_ports_passes_max_ports_through);
RUN_TEST(test_list_ports_zero_max_uses_default);
RUN_TEST(test_free_ports_null);
RUN_TEST(test_free_ports_zero_count);
+1 -1
View File
@@ -1,4 +1,4 @@
add_executable(telemetry telemetry.c)
target_include_directories(telemetry PRIVATE "${CMAKE_SOURCE_DIR}")
target_compile_features(telemetry PRIVATE c_std_23)
target_link_libraries(telemetry PRIVATE celrs_crsf celrs_serial celrs_logger celrs_log_write)
target_link_libraries(telemetry PRIVATE celrs_crsf celrs_serial celrs_serial_platform celrs_logger celrs_log_write)