Skip to content

Commit

Permalink
Rework golden tests, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa committed Nov 24, 2024
1 parent 988f02b commit 8a7f774
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 79 deletions.
18 changes: 9 additions & 9 deletions Tests/GoldenTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
include(${CMAKE_SOURCE_DIR}/cmake/golden-tests.cmake)
include("${CMAKE_SOURCE_DIR}/cmake/golden-tests.cmake")

# Initialize "global" variables used by macros
set(output_files "")
add_golden_test(TESTFILE HelloWorld.test.cpp LIBS rodos::rodos)
add_golden_test(TESTFILE HelloDummy.test.cpp LIBS rodos::rodos etl::etl Sts1CobcSw_Dummy)

set(Sts1CobcSw "${CMAKE_SOURCE_DIR}/Sts1CobcSw")
# --- All golden tests ---

add_golden_test(TESTFILE "HelloWorld.test.cpp" LIB rodos::rodos)

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
27 changes: 19 additions & 8 deletions Tests/GoldenTests/Scripts/TestRunner.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/usr/bin/env bash

echo "::: Running $1"
set -e
# Kill test executable (with SIGKILL) after 8 seconds. Necessary
# because sometimes tests might deadlock.
if timeout -s 9 8 $1 > "$1.output"
then
true
if [ $# -ne 2 ]; then
echo "Error: wrong number of arguments"
echo "Usage: $0 <test_executable> <expected_output>"
exit 1
fi
echo "Running $1"

# Remove Rodos header/intro from output
$1 > "$1.output"
pattern="--------------- Application running ------------"
# Search for the pattern and store everything after it in "$1.output.trimmed"
awk "/$pattern/ {found=1; next} found" "$1.output" > "$1.output.trimmed"

# Compare the trimmed output with the expected one
if diff -q "$1.output.trimmed" "$2" >/dev/null; then
echo "-> passed: test generated expected output"
exit 0
else
echo ":: KILLED BY TIMEOUT" >> "$1.output"
echo "-> failed: test did not generate expected output"
exit 1
fi
15 changes: 0 additions & 15 deletions Tests/GoldenTests/Scripts/ThreadTestRunner.sh

This file was deleted.

52 changes: 13 additions & 39 deletions cmake/golden-tests.cmake
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
# TODO: Check if this can be turned into a function
macro(add_golden_test)
# Parse arguments
set(options "")
set(one_value_args TESTFILE)
set(multi_value_args SOURCE LIB)
set(multi_value_args SOURCES LIBS)
cmake_parse_arguments(GT "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})

# Retrieve file name
get_filename_component(test_filename ${GT_TESTFILE} NAME_WE)
set(target_name ${PROJECT_NAME}_${test_filename})

# Sanity check
if(NOT ("${GT_TESTFILE}" MATCHES "\.test\.cpp$"))
# In case we are testing one of the real threads
if(NOT ("${GT_TESTFILE}" MATCHES ".*Sts1CobcSw/.*Thread.cpp"))
message(FATAL_ERROR "Name of test file `${GT_TESTFILE}` do not end with .test.cpp")
endif()
message(FATAL_ERROR "Name of test file `${GT_TESTFILE}` do not end with .test.cpp")
endif()

# Create executable
add_executable(${target_name} EXCLUDE_FROM_ALL ${GT_TESTFILE} ${GT_SOURCE})
add_executable(${target_name} EXCLUDE_FROM_ALL ${GT_TESTFILE} ${GT_SOURCES})
target_include_directories(${target_name} ${warning_guard} PUBLIC "${CMAKE_SOURCE_DIR}")
set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${test_filename})
target_link_libraries(${target_name} PUBLIC ${GT_LIB})
target_link_libraries(${target_name} PUBLIC ${GT_LIBS})

if("${GT_TESTFILE}" MATCHES "\.test\.cpp$")
add_custom_command(
OUTPUT "${test_filename}.output"
COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/Scripts/TestRunner.sh"
$<TARGET_FILE:${target_name}> DEPENDS ${target_name} Scripts/TestRunner.sh
)
add_test(NAME ${test_filename}_Test
COMMAND diff "${test_filename}.output"
"${CMAKE_CURRENT_SOURCE_DIR}/ExpectedOutputs/${test_filename}.txt"
)
else()
add_custom_command(
OUTPUT "${test_filename}.output"
COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/Scripts/ThreadTestRunner.sh"
$<TARGET_FILE:${target_name}>
DEPENDS ${target_name} Scripts/ThreadTestRunner.sh
)
add_test(NAME ${test_filename}_Test
COMMAND diff "${test_filename}.output.trimmed"
"${CMAKE_CURRENT_SOURCE_DIR}/ExpectedOutputs/${test_filename}.txt"
)
endif()

list(APPEND output_files "${test_filename}.output")

# Create clean target
add_custom_target(
${target_name}_Clean COMMAND ${CMAKE_COMMAND} -E remove -f "${test_filename}.output*"
add_test(
NAME ${test_filename}_Test
COMMAND
bash "${CMAKE_SOURCE_DIR}/Tests/GoldenTests/Scripts/TestRunner.sh"
$<TARGET_FILE:${target_name}>
"${CMAKE_SOURCE_DIR}/Tests/GoldenTests/ExpectedOutputs/${test_filename}.txt"
)
endmacro()

# TODO: Convert TestRunner.sh to a CMake script

0 comments on commit 8a7f774

Please sign in to comment.