Skip to content

Commit

Permalink
Only add test targets if testing is enabled
Browse files Browse the repository at this point in the history
This commit ensures that test targets are only added if BUILD_TESTING is
enabled when generating the CMake configuration. There should be no
change in behavior, as BUILD_TESTING defaults to ON.
  • Loading branch information
Technius committed Dec 14, 2023
1 parent fdbef97 commit e5d1dbc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
18 changes: 4 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -53,17 +54,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()
17 changes: 17 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

file(GLOB test "*.cpp")

foreach(testfile ${test})
# ${testfile} is in the format of SomeTest.cpp
string(REPLACE ".cpp" "_test" no_ext_name ${testfile})
# ${no_ext_name} is in the format of 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()

0 comments on commit e5d1dbc

Please sign in to comment.