From cd19cc14c03403cac7dd79c6f93fe379f3895ad5 Mon Sep 17 00:00:00 2001 From: portersky <24420859+portersky@users.noreply.github.com> Date: Sat, 9 May 2026 21:32:58 +0200 Subject: [PATCH] feat: add Unity color and check target Enable UNITY_OUTPUT_COLOR and UNITY_INCLUDE_PRINT_FORMATTED via compile definitions on the unity target so colors propagate to all test suites automatically. Add a 'check' custom target that builds all suites and runs CTest with --output-on-failure and USES_TERMINAL, keeping ANSI colors alive through Ninja's buffering. --- AGENTS.md | 3 ++- README.md | 14 ++++++-------- deps/FindUnity.cmake | 7 +++++++ tests/CMakeLists.txt | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index ca48772..334a055 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -15,7 +15,8 @@ ```sh cmake -S . -B build -G Ninja ninja -C build # build -ninja -C build test # run tests +ninja -C build check # run tests (full Unity output, colored) +ninja -C build test # run tests (CTest summary only) ./build/main # run (Linux/macOS) ./build/main.exe # run (Windows) ``` diff --git a/README.md b/README.md index f624602..1143329 100644 --- a/README.md +++ b/README.md @@ -26,16 +26,13 @@ ninja -C build ## Test ```sh -ninja -C build test # summary output -ctest --test-dir build -V # verbose (shows each test name) +ninja -C build check # full Unity output, colored +ninja -C build test # CTest summary only ``` -Run a single suite directly for the clearest output: - -```sh -./build/tests/test_str.exe -./build/tests/test_report.exe -``` +`check` builds all suites and runs CTest with `--output-on-failure`, +so assertion-level detail appears on any failure without running +binaries by hand. ## Run @@ -84,6 +81,7 @@ target_include_directories(test_counter PRIVATE "${CMAKE_SOURCE_DIR}") target_link_libraries(test_counter PRIVATE ctdd_counter Unity::Unity) target_compile_features(test_counter PRIVATE c_std_23) add_test(NAME test_counter COMMAND test_counter) +list(APPEND TEST_TARGETS test_counter) ``` ### 4. Add a stub, confirm RED, then implement GREEN diff --git a/deps/FindUnity.cmake b/deps/FindUnity.cmake index 26907d0..b14ba5e 100644 --- a/deps/FindUnity.cmake +++ b/deps/FindUnity.cmake @@ -55,4 +55,11 @@ if (Unity_INCLUDE_DIR AND TARGET unity) ) endif() +if (TARGET unity) + target_compile_definitions(unity PUBLIC + UNITY_OUTPUT_COLOR + UNITY_INCLUDE_PRINT_FORMATTED + ) +endif() + set(UNITY_LICENSE_FILE "${unity_SOURCE_DIR}/LICENSE.txt" CACHE FILEPATH "Path to Unity license file") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d61e1d6..57709ad 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,12 +28,15 @@ function(cmock_generate_mock target header) target_include_directories("${target}" PRIVATE "${MOCK_GEN_DIR}" "${header_dir}") endfunction() +set(TEST_TARGETS "") + # str tests — pure functions, no mock needed add_executable(test_str test_str.c) target_include_directories(test_str PRIVATE "${CMAKE_SOURCE_DIR}") target_link_libraries(test_str PRIVATE ctdd_str Unity::Unity) target_compile_features(test_str PRIVATE c_std_23) add_test(NAME test_str COMMAND test_str) +list(APPEND TEST_TARGETS test_str) # report tests — mocks logger.h so log_info calls are intercepted add_executable(test_report test_report.c) @@ -42,6 +45,7 @@ target_link_libraries(test_report PRIVATE ctdd_report Unity::Unity CMock::CMock) target_compile_features(test_report PRIVATE c_std_23) cmock_generate_mock(test_report "${CMAKE_SOURCE_DIR}/ctdd/logger.h") add_test(NAME test_report COMMAND test_report) +list(APPEND TEST_TARGETS test_report) # logger tests — mocks log_write.h so output calls are intercepted add_executable(test_logger test_logger.c) @@ -50,3 +54,16 @@ target_link_libraries(test_logger PRIVATE ctdd_logger Unity::Unity CMock::CMock) target_compile_features(test_logger PRIVATE c_std_23) cmock_generate_mock(test_logger "${CMAKE_SOURCE_DIR}/ctdd/log_write.h") add_test(NAME test_logger COMMAND test_logger) +list(APPEND TEST_TARGETS test_logger) + +# 'check' builds all suites and runs CTest with full Unity output. +# USES_TERMINAL keeps ANSI colors alive through Ninja's output buffering. +add_custom_target(check + COMMAND ${CMAKE_CTEST_COMMAND} + --test-dir "${CMAKE_BINARY_DIR}" + --output-on-failure + --progress + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" + USES_TERMINAL + DEPENDS ${TEST_TARGETS} +)