1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4set(prefix "${TEST_PREFIX}")
5set(suffix "${TEST_SUFFIX}")
6set(extra_args ${TEST_EXTRA_ARGS})
7set(properties ${TEST_PROPERTIES})
8set(script)
9set(suite)
10set(tests)
11
12function(add_command NAME)
13  set(_args "")
14  foreach(_arg ${ARGN})
15    if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
16      set(_args "${_args} [==[${_arg}]==]")
17    else()
18      set(_args "${_args} ${_arg}")
19    endif()
20  endforeach()
21  set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
22endfunction()
23
24# Run test executable to get list of available tests
25if(NOT EXISTS "${TEST_EXECUTABLE}")
26  message(FATAL_ERROR
27    "Specified test executable does not exist.\n"
28    "  Path: '${TEST_EXECUTABLE}'"
29  )
30endif()
31execute_process(
32  COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --gtest_list_tests
33  TIMEOUT ${TEST_DISCOVERY_TIMEOUT}
34  OUTPUT_VARIABLE output
35  RESULT_VARIABLE result
36)
37if(NOT ${result} EQUAL 0)
38  string(REPLACE "\n" "\n    " output "${output}")
39  message(FATAL_ERROR
40    "Error running test executable.\n"
41    "  Path: '${TEST_EXECUTABLE}'\n"
42    "  Result: ${result}\n"
43    "  Output:\n"
44    "    ${output}\n"
45  )
46endif()
47
48string(REPLACE "\n" ";" output "${output}")
49
50# Parse output
51foreach(line ${output})
52  # Skip header
53  if(NOT line MATCHES "gtest_main\\.cc")
54    # Do we have a module name or a test name?
55    if(NOT line MATCHES "^  ")
56      # Module; remove trailing '.' to get just the name...
57      string(REGEX REPLACE "\\.( *#.*)?" "" suite "${line}")
58      if(line MATCHES "#" AND NOT NO_PRETTY_TYPES)
59        string(REGEX REPLACE "/[0-9]\\.+ +#.*= +" "/" pretty_suite "${line}")
60      else()
61        set(pretty_suite "${suite}")
62      endif()
63      string(REGEX REPLACE "^DISABLED_" "" pretty_suite "${pretty_suite}")
64    else()
65      # Test name; strip spaces and comments to get just the name...
66      string(REGEX REPLACE " +" "" test "${line}")
67      if(test MATCHES "#" AND NOT NO_PRETTY_VALUES)
68        string(REGEX REPLACE "/[0-9]+#GetParam..=" "/" pretty_test "${test}")
69      else()
70        string(REGEX REPLACE "#.*" "" pretty_test "${test}")
71      endif()
72      string(REGEX REPLACE "^DISABLED_" "" pretty_test "${pretty_test}")
73      string(REGEX REPLACE "#.*" "" test "${test}")
74      # ...and add to script
75      add_command(add_test
76        "${prefix}${pretty_suite}.${pretty_test}${suffix}"
77        ${TEST_EXECUTOR}
78        "${TEST_EXECUTABLE}"
79        "--gtest_filter=${suite}.${test}"
80        "--gtest_also_run_disabled_tests"
81        ${extra_args}
82      )
83      if(suite MATCHES "^DISABLED" OR test MATCHES "^DISABLED")
84        add_command(set_tests_properties
85          "${prefix}${pretty_suite}.${pretty_test}${suffix}"
86          PROPERTIES DISABLED TRUE
87        )
88      endif()
89      add_command(set_tests_properties
90        "${prefix}${pretty_suite}.${pretty_test}${suffix}"
91        PROPERTIES
92        WORKING_DIRECTORY "${TEST_WORKING_DIR}"
93        ${properties}
94      )
95     list(APPEND tests "${prefix}${pretty_suite}.${pretty_test}${suffix}")
96    endif()
97  endif()
98endforeach()
99
100# Create a list of all discovered tests, which users may use to e.g. set
101# properties on the tests
102add_command(set ${TEST_LIST} ${tests})
103
104# Write CTest script
105file(WRITE "${CTEST_FILE}" "${script}")
106