mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-17 19:22:25 +01:00
3d7104c124
Copied from [Dynamic Catch test discovery in CMake](https://gist.github.com/garethsb/a01ed0dbd4977d439c16200640549935), which was inspired by [Dynamic Google Test Discovery in CMake 3.10](https://blog.kitware.com/dynamic-google-test-discovery-in-cmake-3-10/). Original source code: - Adapted by [Gareth Sylvester-Bradley](https://github.com/garethsb) from [GoogleTest ``gtest_discover_tests``](https://gitlab.kitware.com/cmake/cmake/merge_requests/1056). - BSD 3-clause "New" or "Revised" License. - Copyright 2000-2017 Kitware, Inc. and Contributors. All rights reserved.
78 lines
2.1 KiB
CMake
78 lines
2.1 KiB
CMake
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
|
|
|
set(prefix "${TEST_PREFIX}")
|
|
set(suffix "${TEST_SUFFIX}")
|
|
set(spec ${TEST_SPEC})
|
|
set(extra_args ${TEST_EXTRA_ARGS})
|
|
set(properties ${TEST_PROPERTIES})
|
|
set(script)
|
|
set(suite)
|
|
set(tests)
|
|
|
|
function(add_command NAME)
|
|
set(_args "")
|
|
foreach(_arg ${ARGN})
|
|
if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
|
|
set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
|
|
else()
|
|
set(_args "${_args} ${_arg}")
|
|
endif()
|
|
endforeach()
|
|
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
# Run test executable to get list of available tests
|
|
if(NOT EXISTS "${TEST_EXECUTABLE}")
|
|
message(FATAL_ERROR
|
|
"Specified test executable '${TEST_EXECUTABLE}' does not exist"
|
|
)
|
|
endif()
|
|
execute_process(
|
|
COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
|
|
OUTPUT_VARIABLE output
|
|
RESULT_VARIABLE result
|
|
)
|
|
# Catch --list-test-names-only reports the number of tests, so 0 is... surprising
|
|
if(${result} EQUAL 0)
|
|
message(WARNING
|
|
"Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
|
|
)
|
|
elseif(${result} LESS 0)
|
|
message(FATAL_ERROR
|
|
"Error running test executable '${TEST_EXECUTABLE}':\n"
|
|
" Result: ${result}\n"
|
|
" Output: ${output}\n"
|
|
)
|
|
endif()
|
|
|
|
string(REPLACE "\n" ";" output "${output}")
|
|
|
|
# Parse output
|
|
foreach(line ${output})
|
|
# Test name; strip spaces to get just the name...
|
|
string(REGEX REPLACE " +" "" test "${line}")
|
|
# ...and add to script
|
|
add_command(add_test
|
|
"${prefix}${test}${suffix}"
|
|
${TEST_EXECUTOR}
|
|
"${TEST_EXECUTABLE}"
|
|
"${test}"
|
|
${extra_args}
|
|
)
|
|
add_command(set_tests_properties
|
|
"${prefix}${test}${suffix}"
|
|
PROPERTIES
|
|
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
|
|
${properties}
|
|
)
|
|
list(APPEND tests "${prefix}${test}${suffix}")
|
|
endforeach()
|
|
|
|
# Create a list of all discovered tests, which users may use to e.g. set
|
|
# properties on the tests
|
|
add_command(set ${TEST_LIST} ${tests})
|
|
|
|
# Write CTest script
|
|
file(WRITE "${CTEST_FILE}" "${script}")
|