]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
Improve Catch2 support by adding helper functions. 63529/head
authorJesse F. Williamson <jfw@ibm.com>
Tue, 27 May 2025 17:44:43 +0000 (10:44 -0700)
committerJesse F. Williamson <jfw@ibm.com>
Thu, 29 May 2025 15:39:21 +0000 (08:39 -0700)
Makes it less tedious to add a new Catch2 test with CMake.

Signed-off-by: Jesse F. Williamson <jfw@ibm.com>
cmake/modules/AddCephTest.cmake
src/test/rgw/CMakeLists.txt

index ab4dc63ca32a233fb158cb1e5bc0f6bdfb2f6209..67e60504d6dd8caacd784be179bef46b2ec4373f 100644 (file)
@@ -136,3 +136,51 @@ function(add_tox_test name)
     PYTHONPATH=${CMAKE_SOURCE_DIR}/src/pybind)
   list(APPEND tox_test run-tox-${name})
 endfunction()
+
+# Helper for adding new tests built with Catch2:
+function(add_catch2_test test_name)
+if(NOT WITH_CATCH2)
+  return()
+endif()
+
+set(options NO_CATCH2_MAIN)
+set(oneValueArgs)
+set(multiValueArgs EXTRA_LIBS EXTRA_INCS)
+
+cmake_parse_arguments(PARSE_ARGV 0 catch2_opt
+  "${options}" "${oneValueArgs}" "${multiValueArgs}")
+
+ add_executable(unittest_${test_name}
+ test_${test_name}.cc)
+
+SET(tl_libs
+  librados
+  ceph-common
+  ${EXTRALIBS})
+
+SET(tl_incs
+  SYSTEM PRIVATE ${CMAKE_SOURCE_DIR})
+
+if(DEFINED catch2_opt_EXTRA_LIBS)
+  LIST(APPEND tl_libs ${catch2_opt_EXTRA_LIBS})
+endif()
+
+if(DEFINED catch2_opt_EXTRA_INCS)
+  LIST(APPEND tl_incs ${catch2_opt_EXTRA_INCS})
+endif()
+
+if(${catch2_opt_NO_CATCH2_MAIN})
+  LIST(APPEND tl_libs Catch2)
+else()
+  LIST(APPEND tl_libs Catch2WithMain)
+endif()
+
+target_link_libraries(unittest_${test_name} 
+  ${tl_libs})
+
+target_include_directories(unittest_${test_name}
+  ${tl_incs})
+
+add_ceph_unittest(unittest_${test_name})
+
+endfunction()
index 56050317314065a6f5d8f6d799c8ac42b6793386..a8e5896316a6abb49a9f0af03b0b1a9aef576137 100644 (file)
@@ -388,21 +388,19 @@ target_link_libraries(ceph_test_datalog
   ${rgw_libs})
 install(TARGETS ceph_test_datalog DESTINATION ${CMAKE_INSTALL_BINDIR})
 
-if(WITH_CATCH2)
- add_executable(unittest_rgw_hex
-  test_rgw_hex.cc)
+#
+# Helper for adding RGW tests built with Catch2:
+# Note: to define a test that does not use Catch2::main(), use:
+#   add_catch2_test_rgw(my_test_name NO_CATCH2_MAIN)
+# ...your test should be in a file called "test_<my_test_name>.cc (ex. "test_foo.cc")
+# and the output will be called "unittest_my_test_name".
+#
+function(add_catch2_test_rgw test_name)
+add_catch2_test(${test_name}
+ ${ARGV1}
+ EXTRA_LIBS rgw_common ${rgw_libs}
+ EXTRA_INCS "SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src/rgw")
+endfunction()
+
+add_catch2_test_rgw(rgw_hex)
 
- target_include_directories(unittest_rgw_hex
-  SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/src/rgw")
-
- target_link_libraries(unittest_rgw_hex 
-  rgw_common
-  librados
-  ceph-common
-  ${rgw_libs}
-  ${EXTRALIBS}
-  Catch2WithMain)
-
- add_ceph_unittest(unittest_rgw_hex)
-
-endif(WITH_CATCH2)