Skip to content

Commit 8776b8e

Browse files
committed
add DEF_SOURCE_LINE to ctests
GoogleTest sets this property on ctest tests, and vscode-cmake-tools will use it to dramatically improve the Test Explorer user experience. CppUTest doesn't record file paths or line numbers for test groups, only for tests, so when CPPUTEST_TESTS_DETAILED=0, use the lowest line number of the tests in that group.
1 parent 694d00d commit 8776b8e

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed
Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(script)
1+
set(script "" CACHE INTERNAL "")
22

33
function(add_command NAME)
44
set(_args "")
@@ -12,7 +12,7 @@ function(add_command NAME)
1212
set(_args "${_args} ${_arg}")
1313
endif()
1414
endforeach()
15-
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
15+
set(script "${script}${NAME}(${_args})\n" CACHE INTERNAL "")
1616
endfunction()
1717

1818
if(NOT EXISTS "${EXECUTABLE}")
@@ -21,16 +21,27 @@ if(NOT EXISTS "${EXECUTABLE}")
2121
)
2222
endif()
2323

24-
if(TESTS_DETAILED)
25-
set(discovery_arg "-ln")
26-
set(select_arg "-st")
27-
else()
28-
set(discovery_arg "-lg")
29-
set(select_arg "-sg")
30-
endif()
24+
function (add_test_to_script TEST_NAME TEST_LOCATION SELECT_ARG)
25+
add_command(
26+
add_test
27+
"${TEST_NAME}"
28+
${EMULATOR}
29+
"${EXECUTABLE}"
30+
${ARGS}
31+
${SELECT_ARG}
32+
${TEST_NAME}
33+
)
34+
add_command(
35+
set_tests_properties
36+
"${TEST_NAME}"
37+
PROPERTIES
38+
DEF_SOURCE_LINE
39+
"${TEST_LOCATION}"
40+
)
41+
endfunction()
3142

3243
execute_process(
33-
COMMAND ${EMULATOR} "${EXECUTABLE}" ${discovery_arg}
44+
COMMAND ${EMULATOR} "${EXECUTABLE}" -ll
3445
OUTPUT_VARIABLE discovered_tests
3546
RESULT_VARIABLE result
3647
ERROR_VARIABLE error
@@ -41,17 +52,37 @@ if(NOT ${result} EQUAL 0)
4152
"${error}"
4253
)
4354
endif()
44-
separate_arguments(discovered_tests)
45-
foreach(test_name IN LISTS discovered_tests)
46-
add_command(
47-
add_test
48-
"${test_name}"
49-
${EMULATOR}
50-
"${EXECUTABLE}"
51-
${ARGS}
52-
${select_arg}
53-
${test_name}
54-
)
55-
endforeach()
55+
56+
set(LL_LINE_REGEX "^([^.]*)\\.([^.]*)\\.(.*)\\.([^.]*)\n")
57+
string(REGEX MATCHALL "[^\n]+\n" discovered_test_lines "${discovered_tests}")
58+
if(TESTS_DETAILED)
59+
foreach(line IN LISTS discovered_test_lines)
60+
string(REGEX MATCH "${LL_LINE_REGEX}" __ign "${line}")
61+
set(test_name "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}")
62+
set(test_location "${CMAKE_MATCH_3}:${CMAKE_MATCH_4}")
63+
add_test_to_script("${test_name}" "${test_location}" -st)
64+
endforeach()
65+
else()
66+
foreach(line IN LISTS discovered_test_lines)
67+
string(REGEX MATCH "${LL_LINE_REGEX}" __ign "${line}")
68+
set(test_name "${CMAKE_MATCH_1}")
69+
set(test_file "${CMAKE_MATCH_3}")
70+
set(test_line "${CMAKE_MATCH_4}")
71+
if (NOT _${test_name}_file)
72+
# if the group spans two files, arbitrarily choose the first one encountered
73+
set(_${test_name}_file "${test_file}")
74+
set(_${test_name}_line "${test_line}")
75+
elseif(test_file STREQUAL _${test_name}_file AND test_line LESS _${test_name}_line)
76+
# line number will eventually be the first line of the first test in the group's file
77+
set(_${test_name}_line ${test_line})
78+
endif()
79+
list(APPEND groups_seen ${test_name})
80+
endforeach()
81+
list(REMOVE_DUPLICATES groups_seen)
82+
foreach(test_name IN LISTS groups_seen)
83+
set(test_location "${_${test_name}_file}:${_${test_name}_line}")
84+
add_test_to_script("${test_name}" "${test_location}" -sg)
85+
endforeach()
86+
endif()
5687

5788
file(WRITE "${CTEST_FILE}" "${script}")

0 commit comments

Comments
 (0)