Debian Codegraph @

cmake/Scripts/CppUTestBuildTimeDiscoverTests.cmake

raw
1 # Create CTest entries for EXECUTABLE in CTestTestfile.cmake
2 # Overwrite CTestTestfile.cmake with update version.
3
4 # Usage:
5 #
6 # This script is to be called from ../Modules/CppUTestBuildTimeDiscoverTests.cmake
7 #
8 # Notes on invocation and used variables:
9 # ${CMAKE_COMMAND} -DTESTS_DETAILED:BOOL=<ON|OFF> -DEXECUTABLE=<FULLPATH-TO-EXECUTABLE> -P <PATH-TO-THIS-SCRIPT>
10 #
11 # TESTS_DETAILED c.f. top-level CMakeLists.txt
12 # FULLPATH-TO-EXECUTABLE - use $<TARGET_FILE:${EXECUTABLE}> or explicit
13 # The overwritten CTestTestfile.cmake is located in ${CMAKE_CURRENT_BINARY_DIR}
14 #
15 # Steps to generate ADD_TEST() commands build time
16 # - Read CTestTestfile.cmake
17 # - Create update entries
18 # - Remove duplicate entries
19 # - Write new CTestTestfile.cmake
20
21 ######################################################################
22 # helpers
23 ######################################################################
24 function (buildtime_add_test)
25 # Create ADD_TEST() command string
26 # - Extract and remove testname from ARGV
27 # - Add inner quotes to test arguments
28 # - Add "ADD_TEST()", and first and last quote
29 # Append result to CTESTTESTS
30 list(GET ARGV 0 testname)
31 list(REMOVE_AT ARGV 0)
32 string (REPLACE ";" "\" \"" TEST_ARGS "${ARGV}")
33 set(test_to_add "ADD_TEST(${testname} \"${TEST_ARGS}\")")
34 list(APPEND CTESTTESTS ${test_to_add})
35 SET(CTESTTESTS ${CTESTTESTS} PARENT_SCOPE)
36 endfunction()
37
38 function (JOIN VALUES GLUE OUTPUT)
39 string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
40 set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
41 endfunction()
42
43 function (buildtime_discover_tests EXECUTABLE DISCOVER_ARG OUTPUT)
44 execute_process(COMMAND ${EXECUTABLE} ${DISCOVER_ARG}
45 OUTPUT_VARIABLE _TMP_OUTPUT
46 ERROR_VARIABLE DISCOVER_ERR
47 RESULT_VARIABLE DISCOVER_ERR)
48 if(NOT ${DISCOVER_ERR} EQUAL 0)
49 message(SEND_ERROR "Executable \"${EXECUTABLE} ${DISCOVER_ARG}\" failed with output:\n"
50 "${DISCOVER_ERR}\n"
51 "Please check that the excutable was added.")
52 endif(NOT ${DISCOVER_ERR} EQUAL 0)
53 separate_arguments(_TMP_OUTPUT)
54 set(${OUTPUT} "${_TMP_OUTPUT}" PARENT_SCOPE)
55 endfunction()
56
57
58 ######################################################################
59 # Implementation
60 ######################################################################
61
62 set(CTESTFNAME "${CMAKE_CURRENT_BINARY_DIR}/CTestTestfile.cmake")
63 file(STRINGS ${CTESTFNAME} CTESTTESTS)
64 get_filename_component(EXECUTABLE_SHORT_NAME ${EXECUTABLE} NAME_WE)
65
66 if (TESTS_DETAILED)
67 set(DISCOVER_ARG "-ln")
68 buildtime_discover_tests("${EXECUTABLE}" "${DISCOVER_ARG}" TestList_GroupsAndNames)
69 set(lastgroup "")
70 foreach(testfullname ${TestList_GroupsAndNames})
71 string(REGEX MATCH "^([^/.]+)" groupname ${testfullname})
72 string(REGEX MATCH "([^/.]+)$" testname ${testfullname})
73 if (NOT ("${groupname}" STREQUAL "${lastgroup}"))
74 message("TestGroup: ${groupname}:")
75 set(lastgroup "${groupname}")
76 endif (NOT ("${groupname}" STREQUAL "${lastgroup}"))
77 message("... ${testname}")
78 buildtime_add_test(${EXECUTABLE_SHORT_NAME}.${testfullname} ${EXECUTABLE} -sg ${groupname} -sn ${testname})
79 endforeach()
80 else (TESTS_DETAILED)
81 set(DISCOVER_ARG "-lg")
82 buildtime_discover_tests("${EXECUTABLE}" "${DISCOVER_ARG}" TestList_Groups)
83 foreach(group ${TestList_Groups})
84 message("TestGroup: ${group}")
85 buildtime_add_test(${EXECUTABLE_SHORT_NAME}.${group} "${EXECUTABLE}" -sg ${group})
86 endforeach()
87 endif (TESTS_DETAILED)
88
89
90 # create separate CTest test for each CppUTestTests test
91
92 list(REMOVE_DUPLICATES CTESTTESTS)
93 JOIN("${CTESTTESTS}" "\n" CTESTTESTS)
94 file(WRITE ${CTESTFNAME} "${CTESTTESTS}\n")