From fb3fcf2d987caf68f40b75ad033ce7aee9e8d626 Mon Sep 17 00:00:00 2001 From: Bryan Tan Date: Sun, 10 Dec 2023 17:57:24 -0800 Subject: [PATCH] Only build tests if testing is enabled This commit ensures that tests are only compiled by default if BUILD_TESTING is enabled when generating the CMake configuration. There should be no change in behavior, as BUILD_TESTING defaults to ON. --- CMakeLists.txt | 21 ++++----------------- test/CMakeLists.txt | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 test/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b13c51..541d3a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.0.2) project("sparta") +include(CTest) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) include(Commons) @@ -54,20 +55,6 @@ export(TARGETS sparta FILE sparta_target.cmake) ################################################### # test ################################################### -file(GLOB test - "test/*.cpp" - ) - -include(CTest) -# ${test} contains all paths to the test cpps -foreach(testfile ${test}) - # ${testfile} is in the format of test/SomeTest.cpp - string(REPLACE ".cpp" "_test" no_ext_name ${testfile}) - # ${no_ext_name} is in the format of test/SomeTest_test - get_filename_component(test_bin ${no_ext_name} NAME) - # ${test_bin} is in the format of SomeTest_test - add_executable(${test_bin} ${testfile}) - target_link_libraries(${test_bin} PRIVATE sparta gmock_main) - add_test(NAME ${testfile} COMMAND ${test_bin}) -endforeach() - +if (BUILD_TESTING) + add_subdirectory(test) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..de4d58c --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,14 @@ +file(GLOB test + "*.cpp" + ) + +foreach(testfile ${test}) + # ${testfile} is in the format of test/SomeTest.cpp + string(REPLACE ".cpp" "_test" no_ext_name ${testfile}) + # ${no_ext_name} is in the format of test/SomeTest_test + get_filename_component(test_bin ${no_ext_name} NAME) + # ${test_bin} is in the format of SomeTest_test + add_executable(${test_bin} ${testfile}) + target_link_libraries(${test_bin} PRIVATE sparta gmock_main) + add_test(NAME ${testfile} COMMAND ${test_bin}) +endforeach()