156d55ffe7510d2ff6b2b8e20225af7b9b05e6b0
[m6w6/libmemcached] / testing / CMake / CatchAddTests.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 set(prefix "${TEST_PREFIX}")
5 set(suffix "${TEST_SUFFIX}")
6 set(spec ${TEST_SPEC})
7 set(extra_args ${TEST_EXTRA_ARGS})
8 set(properties ${TEST_PROPERTIES})
9 set(script)
10 set(suite)
11 set(tests)
12
13 function(add_command NAME)
14 set(_args "")
15 foreach(_arg ${ARGN})
16 if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
17 set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
18 else()
19 set(_args "${_args} ${_arg}")
20 endif()
21 endforeach()
22 set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
23 endfunction()
24
25 # Run test executable to get list of available tests
26 if(NOT EXISTS "${TEST_EXECUTABLE}")
27 message(FATAL_ERROR
28 "Specified test executable '${TEST_EXECUTABLE}' does not exist"
29 )
30 endif()
31 execute_process(
32 COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
33 OUTPUT_VARIABLE output
34 RESULT_VARIABLE result
35 )
36 # Catch --list-test-names-only reports the number of tests, so 0 is... surprising
37 if(${result} EQUAL 0)
38 message(WARNING
39 "Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
40 )
41 elseif(${result} LESS 0)
42 message(FATAL_ERROR
43 "Error running test executable '${TEST_EXECUTABLE}':\n"
44 " Result: ${result}\n"
45 " Output: ${output}\n"
46 )
47 endif()
48
49 string(REPLACE "\n" ";" output "${output}")
50
51 # Parse output
52 foreach(line ${output})
53 set(test ${line})
54 # Escape characters in test case names that would be parsed by Catch2
55 set(test_name ${test})
56 foreach(char , [ ])
57 string(REPLACE ${char} "\\${char}" test_name ${test_name})
58 endforeach(char)
59 # ...and add to script
60 add_command(add_test
61 "${prefix}${test}${suffix}"
62 ${TEST_EXECUTOR}
63 "${TEST_EXECUTABLE}"
64 "${test_name}"
65 ${extra_args}
66 )
67 add_command(set_tests_properties
68 "${prefix}${test}${suffix}"
69 PROPERTIES
70 WORKING_DIRECTORY "${TEST_WORKING_DIR}"
71 ${properties}
72 )
73 list(APPEND tests "${prefix}${test}${suffix}")
74 endforeach()
75
76 # Create a list of all discovered tests, which users may use to e.g. set
77 # properties on the tests
78 add_command(set ${TEST_LIST} ${tests})
79
80 # Write CTest script
81 file(WRITE "${CTEST_FILE}" "${script}")