Skip to content

Commit

Permalink
Allow building unit tests against installed lib
Browse files Browse the repository at this point in the history
  • Loading branch information
orzechow committed Nov 12, 2024
1 parent c9863f8 commit 3103b17
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
cmake_minimum_required(VERSION 3.22)


######################
## Project settings ##
######################

# We support building this as top-level project, e.g. in order to test the lib installation
project(util_caching_tests
LANGUAGES CXX
)


###############
## C++ setup ##
###############

# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Require C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)

# Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Allow clangd and others to properly understand this C++ project
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Testing only available if this is the main project
# Note this needs to be done in the main CMakeLists
# since it calls enable_testing, which must be in the
# main CMakeLists.
include(CTest)
endif()


###################
## Find packages ##
###################

find_package(GTest)

# Find installed lib and its dependencies, if this is build as top-level project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
find_package(util_caching REQUIRED)
endif()


###########
## Build ##
###########

if(GTEST_FOUND)
file(GLOB_RECURSE _tests CONFIGURE_DEPENDS "*.cpp" "*.cc")
list(FILTER _tests EXCLUDE REGEX "${CMAKE_CURRENT_BINARY_DIR}")

foreach(_test ${_tests})
get_filename_component(_test_name ${_test} NAME_WE)
# make sure we add only one -test to the target
string(REGEX REPLACE "-test" "" TEST_TARGET_NAME ${_test_name})
set(TEST_TARGET_NAME ${PROJECT_NAME}-gtest-${TEST_TARGET_NAME})
set(TEST_TARGET_NAME util_caching_tests-gtest-${TEST_TARGET_NAME})

message(STATUS
"Adding gtest unittest \"${TEST_TARGET_NAME}\" with working dir ${PROJECT_SOURCE_DIR}/${TEST_FOLDER} \n _test: ${_test}"
Expand All @@ -26,7 +72,7 @@ if(GTEST_FOUND)

target_link_libraries(${TEST_TARGET_NAME} PUBLIC
${GTEST_BOTH_LIBRARIES} pthread
${PROJECT_NAME}
util_caching
)

add_test(NAME ${TEST_TARGET_NAME}
Expand Down

0 comments on commit 3103b17

Please sign in to comment.