Skip to content

Commit

Permalink
Rework golden tests and replace bash with CMake scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa committed Nov 24, 2024
1 parent 303bb4b commit b37658f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 98 deletions.
19 changes: 11 additions & 8 deletions Tests/GoldenTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
include(${CMAKE_SOURCE_DIR}/cmake/golden-tests.cmake)
include("${CMAKE_SOURCE_DIR}/cmake/add-golden-test.cmake")

# Initialize "global" variables used by macros
set(output_files "")
add_golden_test(HelloWorld.test.cpp)
target_link_libraries(Sts1CobcSwTests_HelloWorld PRIVATE rodos::rodos)

set(Sts1CobcSw "${CMAKE_SOURCE_DIR}/Sts1CobcSw")
add_golden_test(HelloDummy.test.cpp)
target_link_libraries(Sts1CobcSwTests_HelloDummy PRIVATE rodos::rodos etl::etl Sts1CobcSw_Dummy)

add_golden_test(TESTFILE "HelloWorld.test.cpp" LIB rodos::rodos)
# --- All golden tests ---

add_golden_test(TESTFILE "HelloDummy.test.cpp" LIB rodos::rodos etl::etl Sts1CobcSw_Dummy)

add_custom_target(AllGoldenTests DEPENDS ${output_files})
get_property(
all_golden_test_targets DIRECTORY ${CMAKE_CURRENT_LIST_DIR} PROPERTY BUILDSYSTEM_TARGETS
)
add_custom_target(AllGoldenTests) # Must be defined after getting all hardware test targets
add_dependencies(AllGoldenTests ${all_golden_test_targets})
4 changes: 0 additions & 4 deletions Tests/GoldenTests/HelloDummy.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
#include <cstdint>


std::uint32_t printfMask = 0;


namespace sts1cobcsw
{
class HelloDummy : public RODOS::StaticThread<>
{
void run() override
{
printfMask = 1;
auto const dummy = Dummy();

RODOS::PRINTF("Hello, %s!\n", dummy.name.data());
Expand Down
4 changes: 0 additions & 4 deletions Tests/GoldenTests/HelloWorld.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
#include <cstdint>


std::uint32_t printfMask = 0;


namespace sts1cobcsw
{
class HelloWorld : public RODOS::StaticThread<>
{
void run() override
{
printfMask = 1;
RODOS::PRINTF("Hello, World!\n");
RODOS::hwResetAndReboot();
}
Expand Down
12 changes: 0 additions & 12 deletions Tests/GoldenTests/Scripts/TestRunner.sh

This file was deleted.

15 changes: 0 additions & 15 deletions Tests/GoldenTests/Scripts/ThreadTestRunner.sh

This file was deleted.

23 changes: 23 additions & 0 deletions cmake/add-golden-test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function(add_golden_test test_file)
if(NOT ("${test_file}" MATCHES "\.test\.cpp$"))
message(FATAL_ERROR "Name of test file `${test_file}` does not end with .test.cpp")
endif()

get_filename_component(test_file_name ${test_file} NAME_WE)
set(target ${PROJECT_NAME}_${test_file_name})

add_executable(${target} EXCLUDE_FROM_ALL)
target_sources(${target} PRIVATE "${test_file}")
target_include_directories(${target} ${warning_guard} PUBLIC "${CMAKE_SOURCE_DIR}")
set_target_properties(${target} PROPERTIES OUTPUT_NAME ${test_file_name})

add_test(
NAME ${test_file_name}
COMMAND
"${CMAKE_COMMAND}" -D "TEST_EXECUTABLE=$<TARGET_FILE:${target}>" -D
"EXPECTED_OUTPUT_FILE=${CMAKE_SOURCE_DIR}/Tests/GoldenTests/ExpectedOutputs/${test_file_name}.txt"
-P "${CMAKE_SOURCE_DIR}/cmake/run-golden-test.cmake"
)
# Set a timeout in case we have deadlocks, infinite loops, etc.
set_tests_properties(${test_file_name} PROPERTIES TIMEOUT 8)
endfunction()
55 changes: 0 additions & 55 deletions cmake/golden-tests.cmake

This file was deleted.

25 changes: 25 additions & 0 deletions cmake/run-golden-test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.22)

if(NOT DEFINED TEST_EXECUTABLE)
message(FATAL_ERROR "TEST_EXECUTABLE is not defined")
endif()
if(NOT DEFINED EXPECTED_OUTPUT_FILE)
message(FATAL_ERROR "EXPECTED_OUTPUT_FILE is not defined")
endif()

message("Running golden test with")
message(" test executable: ${TEST_EXECUTABLE}")
message(" expected output file: ${EXPECTED_OUTPUT_FILE}")
execute_process(COMMAND "${TEST_EXECUTABLE}" OUTPUT_VARIABLE output)

set(rodos_header_regex ".*--------------- Application running ------------\n")
string(REGEX REPLACE "${rodos_header_regex}" "" output_without_rodos_header ${output})

file(READ "${EXPECTED_OUTPUT_FILE}" expected_output)

if(output_without_rodos_header STREQUAL expected_output)
message("Test passed ✔️")
else()
# TODO: Upgrade CMake to version 3.29 for cmake_language(EXIT 1)
message(FATAL_ERROR "Test failed ❌")
endif()

0 comments on commit b37658f

Please sign in to comment.