Skip to content

Commit aa76aff

Browse files
authored
Merge pull request ceph#63529 from chardan/build-cmake-improve-catch2-support
make adding Catch2 tests straightforward
2 parents 703d0e7 + 9bdc4e1 commit aa76aff

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

cmake/modules/AddCephTest.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,51 @@ function(add_tox_test name)
136136
PYTHONPATH=${CMAKE_SOURCE_DIR}/src/pybind)
137137
list(APPEND tox_test run-tox-${name})
138138
endfunction()
139+
140+
# Helper for adding new tests built with Catch2:
141+
function(add_catch2_test test_name)
142+
if(NOT WITH_CATCH2)
143+
return()
144+
endif()
145+
146+
set(options NO_CATCH2_MAIN)
147+
set(oneValueArgs)
148+
set(multiValueArgs EXTRA_LIBS EXTRA_INCS)
149+
150+
cmake_parse_arguments(PARSE_ARGV 0 catch2_opt
151+
"${options}" "${oneValueArgs}" "${multiValueArgs}")
152+
153+
add_executable(unittest_${test_name}
154+
test_${test_name}.cc)
155+
156+
SET(tl_libs
157+
librados
158+
ceph-common
159+
${EXTRALIBS})
160+
161+
SET(tl_incs
162+
SYSTEM PRIVATE ${CMAKE_SOURCE_DIR})
163+
164+
if(DEFINED catch2_opt_EXTRA_LIBS)
165+
LIST(APPEND tl_libs ${catch2_opt_EXTRA_LIBS})
166+
endif()
167+
168+
if(DEFINED catch2_opt_EXTRA_INCS)
169+
LIST(APPEND tl_incs ${catch2_opt_EXTRA_INCS})
170+
endif()
171+
172+
if(${catch2_opt_NO_CATCH2_MAIN})
173+
LIST(APPEND tl_libs Catch2)
174+
else()
175+
LIST(APPEND tl_libs Catch2WithMain)
176+
endif()
177+
178+
target_link_libraries(unittest_${test_name}
179+
${tl_libs})
180+
181+
target_include_directories(unittest_${test_name}
182+
${tl_incs})
183+
184+
add_ceph_unittest(unittest_${test_name})
185+
186+
endfunction()

src/test/rgw/CMakeLists.txt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,19 @@ target_link_libraries(ceph_test_datalog
388388
${rgw_libs})
389389
install(TARGETS ceph_test_datalog DESTINATION ${CMAKE_INSTALL_BINDIR})
390390

391-
if(WITH_CATCH2)
392-
add_executable(unittest_rgw_hex
393-
test_rgw_hex.cc)
391+
#
392+
# Helper for adding RGW tests built with Catch2:
393+
# Note: to define a test that does not use Catch2::main(), use:
394+
# add_catch2_test_rgw(my_test_name NO_CATCH2_MAIN)
395+
# ...your test should be in a file called "test_<my_test_name>.cc (ex. "test_foo.cc")
396+
# and the output will be called "unittest_my_test_name".
397+
#
398+
function(add_catch2_test_rgw test_name)
399+
add_catch2_test(${test_name}
400+
${ARGV1}
401+
EXTRA_LIBS rgw_common ${rgw_libs}
402+
EXTRA_INCS "SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src/rgw")
403+
endfunction()
404+
405+
add_catch2_test_rgw(rgw_hex)
394406

395-
target_include_directories(unittest_rgw_hex
396-
SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/src/rgw")
397-
398-
target_link_libraries(unittest_rgw_hex
399-
rgw_common
400-
librados
401-
ceph-common
402-
${rgw_libs}
403-
${EXTRALIBS}
404-
Catch2WithMain)
405-
406-
add_ceph_unittest(unittest_rgw_hex)
407-
408-
endif(WITH_CATCH2)

0 commit comments

Comments
 (0)