From ab2e99b0d95714e16a7d1a1313d7da938b0485cb Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 24 Jul 2024 12:28:10 +0100 Subject: [PATCH] cmake: Create test suite for `ctest` --- CMakeLists.txt | 3 +++ src/CMakeLists.txt | 4 ++-- src/test/CMakeLists.txt | 38 +++++++++++++++++++++++++++++++++++++ src/univalue/CMakeLists.txt | 26 +++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c6a15e647..9529847bd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,6 +213,9 @@ try_append_cxx_flags("-fno-extended-identifiers" TARGET core_interface SKIP_LINK # -fstack-reuse=none for all gcc builds. (Only gcc understands this flag). try_append_cxx_flags("-fstack-reuse=none" TARGET core_interface) +if(BUILD_TESTS) + enable_testing() +endif() # TODO: The `CMAKE_SKIP_BUILD_RPATH` variable setting can be deleted # in the future after reordering Guix script commands to # perform binary checks after the installation step. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a80b840699..09e57e1378 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,8 +41,8 @@ set(SECP256K1_DISABLE_SHARED ON CACHE BOOL "" FORCE) set(SECP256K1_ENABLE_MODULE_ECDH OFF CACHE BOOL "" FORCE) set(SECP256K1_ENABLE_MODULE_RECOVERY ON CACHE BOOL "" FORCE) set(SECP256K1_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE) -set(SECP256K1_BUILD_TESTS OFF CACHE BOOL "" FORCE) -set(SECP256K1_BUILD_EXHAUSTIVE_TESTS OFF CACHE BOOL "" FORCE) +set(SECP256K1_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE) +set(SECP256K1_BUILD_EXHAUSTIVE_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE) set(SECP256K1_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # We want to build libsecp256k1 with the most tested RelWithDebInfo configuration. enable_language(C) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 167d8babf2..d607e0782f 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -153,3 +153,41 @@ target_link_libraries(test_bitcoin Boost::headers $ ) + +function(add_boost_test source_file) + if(NOT EXISTS ${source_file}) + return() + endif() + + file(READ "${source_file}" source_file_content) + string(REGEX + MATCH "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(([A-Za-z0-9_]+)" + test_suite_macro "${source_file_content}" + ) + string(REGEX + REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" "" + test_suite_name "${test_suite_macro}" + ) + if(test_suite_name) + add_test(NAME ${test_suite_name} + COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no + ) + set_property(TEST ${test_suite_name} PROPERTY + SKIP_REGULAR_EXPRESSION "no test cases matching filter" "Skipping" + ) + endif() +endfunction() + +function(add_all_test_targets) + get_target_property(test_source_dir test_bitcoin SOURCE_DIR) + get_target_property(test_sources test_bitcoin SOURCES) + foreach(test_source ${test_sources}) + cmake_path(IS_RELATIVE test_source result) + if(result) + cmake_path(APPEND test_source_dir ${test_source} OUTPUT_VARIABLE test_source) + endif() + add_boost_test(${test_source}) + endforeach() +endfunction() + +add_all_test_targets() diff --git a/src/univalue/CMakeLists.txt b/src/univalue/CMakeLists.txt index ef7f7362f9..96733fe077 100644 --- a/src/univalue/CMakeLists.txt +++ b/src/univalue/CMakeLists.txt @@ -13,3 +13,29 @@ target_include_directories(univalue $ ) target_link_libraries(univalue PRIVATE core_interface) + +if(BUILD_TESTS) + add_executable(unitester test/unitester.cpp) + target_compile_definitions(unitester + PRIVATE + JSON_TEST_SRC=\"${CMAKE_CURRENT_SOURCE_DIR}/test\" + ) + target_link_libraries(unitester + PRIVATE + core_interface + univalue + ) + add_test(NAME univalue_test + COMMAND unitester + ) + + add_executable(object test/object.cpp) + target_link_libraries(object + PRIVATE + core_interface + univalue + ) + add_test(NAME univalue_object_test + COMMAND object + ) +endif()