From 20e6d49857f2206ff04d738775081edb25ecf1e5 Mon Sep 17 00:00:00 2001 From: Ron <45816308+rjaegers@users.noreply.github.com> Date: Thu, 4 Jul 2024 11:12:46 +0200 Subject: [PATCH] refactor: remove duplication in cpp testsuite (#482) * refactor: remove duplication in cpp testsuite * chore: remove unused files --- .../cpp/test/coverage/CMakeLists.txt | 19 ----- .devcontainer/cpp/test/coverage/test.cpp | 20 ----- .devcontainer/cpp/test/testsuite.bats | 78 ++++++++----------- 3 files changed, 33 insertions(+), 84 deletions(-) delete mode 100644 .devcontainer/cpp/test/coverage/CMakeLists.txt delete mode 100644 .devcontainer/cpp/test/coverage/test.cpp diff --git a/.devcontainer/cpp/test/coverage/CMakeLists.txt b/.devcontainer/cpp/test/coverage/CMakeLists.txt deleted file mode 100644 index 6ec9a7df..00000000 --- a/.devcontainer/cpp/test/coverage/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -option(ENABLE_COVERAGE_TEST Off) - -if (ENABLE_COVERAGE_TEST) - include(FetchContent) - - FetchContent_Declare(googletest - GIT_REPOSITORY https://github.com/google/googletest - GIT_TAG v1.14.0 - ) - - FetchContent_MakeAvailable(googletest) - - add_executable(test-coverage EXCLUDE_FROM_ALL test.cpp) - target_compile_options(test-coverage PRIVATE - -g -O0 --coverage -fprofile-arcs -ftest-coverage -fno-inline - ) - target_link_libraries(test-coverage PRIVATE gmock_main gcov) - add_test(NAME test-coverage COMMAND test-coverage) -endif() diff --git a/.devcontainer/cpp/test/coverage/test.cpp b/.devcontainer/cpp/test/coverage/test.cpp deleted file mode 100644 index 3aefac02..00000000 --- a/.devcontainer/cpp/test/coverage/test.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include - -int Factorial(int n) -{ - int result = 1; - for (int i = 1; i <= n; i++) - result *= i; - - return result; -} - -TEST(FactorialTest, Negative) -{ - EXPECT_EQ(1, Factorial(-1)); -} - -TEST(FactorialTest, Zero) -{ - EXPECT_EQ(1, Factorial(0)); -} diff --git a/.devcontainer/cpp/test/testsuite.bats b/.devcontainer/cpp/test/testsuite.bats index d3689e7c..8747db53 100644 --- a/.devcontainer/cpp/test/testsuite.bats +++ b/.devcontainer/cpp/test/testsuite.bats @@ -71,41 +71,11 @@ teardown() { } @test "using ccache as a compiler launcher should result in cached build using gcc compiler" { - ccache --clear --zero-stats - cmake --preset gcc -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - cmake --build --preset gcc - - run ccache -s - assert_output --partial "Hits: 0" - assert_output --partial "Misses: 1" - - rm -rf build - ccache --zero-stats - cmake --preset gcc -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - cmake --build --preset gcc - - run ccache -s - assert_output --partial "Hits: 1" - assert_output --partial "Misses: 0" + configure_and_build_with_ccache gcc } @test "using ccache as a compiler launcher should result in cached build using clang-cl compiler" { - ccache --clear --zero-stats - cmake --preset clang-cl -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - cmake --build --preset clang-cl - - run ccache -s - assert_output --partial "Hits: 0" - assert_output --partial "Misses: 1" - - rm -rf build - ccache --zero-stats - cmake --preset clang-cl -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - cmake --build --preset clang-cl - - run ccache -s - assert_output --partial "Hits: 1" - assert_output --partial "Misses: 0" + configure_and_build_with_ccache clang-cl } @test "running clang-tidy as part of the build should result in warning diagnostics" { @@ -193,27 +163,45 @@ teardown() { } @test "sanitizers should detect undefined or suspicious behavior in code compiled with gcc" { - cmake --preset gcc - cmake --build --preset gcc-sanitizers + build_and_run_with_sanitizers gcc +} - run build/gcc/sanitizers/test-asan - assert_failure - assert_output --partial "AddressSanitizer: stack-buffer-overflow" +@test "sanitizers should detect undefined or suspicious behavior in code compiled with clang" { + build_and_run_with_sanitizers clang +} - run build/gcc/sanitizers/test-ubsan - assert_failure - assert_output --partial "runtime error: load of null pointer" +function configure_and_build_with_ccache() { + local PRESET=${1:?} + + ccache --clear --zero-stats + cmake --preset ${PRESET} -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache + cmake --build --preset ${PRESET} + + run ccache -s + assert_output --partial "Hits: 0" + assert_output --partial "Misses: 1" + + rm -rf build + ccache --zero-stats + cmake --preset ${PRESET} -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache + cmake --build --preset ${PRESET} + + run ccache -s + assert_output --partial "Hits: 1" + assert_output --partial "Misses: 0" } -@test "sanitizers should detect undefined or suspicious behavior in code compiled with clang" { - cmake --preset clang - cmake --build --preset clang-sanitizers +function build_and_run_with_sanitizers() { + local PRESET=${1:?} + + cmake --preset ${PRESET} + cmake --build --preset ${PRESET}-sanitizers - run build/clang/sanitizers/test-asan + run build/${PRESET}/sanitizers/test-asan assert_failure assert_output --partial "AddressSanitizer: stack-buffer-overflow" - run build/clang/sanitizers/test-ubsan + run build/${PRESET}/sanitizers/test-ubsan assert_failure assert_output --partial "runtime error: load of null pointer" }