Skip to content

Commit

Permalink
Automatically add TEST_SUITE labels to discovered tests which map to …
Browse files Browse the repository at this point in the history
…the test suites of the test cases (#464)
  • Loading branch information
shivupa authored and onqtam committed Mar 22, 2021
1 parent 47e7117 commit 680d044
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion scripts/cmake/doctest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
[TEST_PREFIX prefix]
[TEST_SUFFIX suffix]
[PROPERTIES name1 value1...]
[ADD_LABELS value]
[TEST_LIST var]
[JUNIT_OUTPUT_DIR dir]
)
Expand Down Expand Up @@ -85,6 +86,9 @@ same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
Specifies additional properties to be set on all tests discovered by this
invocation of ``doctest_discover_tests``.

``ADD_LABELS value``
Specifies if the test labels should be set automatically.

``TEST_LIST var``
Make the list of tests available in the variable ``var``, rather than the
default ``<target>_TESTS``. This can be useful when the same test
Expand All @@ -106,7 +110,7 @@ function(doctest_discover_tests TARGET)
""
""
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;JUNIT_OUTPUT_DIR"
"TEST_SPEC;EXTRA_ARGS;PROPERTIES"
"TEST_SPEC;EXTRA_ARGS;PROPERTIES;ADD_LABELS"
${ARGN}
)

Expand Down Expand Up @@ -139,6 +143,7 @@ function(doctest_discover_tests TARGET)
-D "TEST_SPEC=${_TEST_SPEC}"
-D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
-D "TEST_PROPERTIES=${_PROPERTIES}"
-D "TEST_ADD_LABELS=${_ADD_LABELS}"
-D "TEST_PREFIX=${_TEST_PREFIX}"
-D "TEST_SUFFIX=${_TEST_SUFFIX}"
-D "TEST_LIST=${_TEST_LIST}"
Expand Down
28 changes: 28 additions & 0 deletions scripts/cmake/doctestAddTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(suffix "${TEST_SUFFIX}")
set(spec ${TEST_SPEC})
set(extra_args ${TEST_EXTRA_ARGS})
set(properties ${TEST_PROPERTIES})
set(add_labels ${TEST_ADD_LABELS})
set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}")
set(script)
set(suite)
Expand Down Expand Up @@ -55,6 +56,31 @@ foreach(line ${output})
continue()
endif()
set(test ${line})
set(labels "")
if(${add_labels} EQUAL 1)
# get test suite that test belongs to
execute_process(
COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --test-case=${test} --list-test-suites
OUTPUT_VARIABLE labeloutput
RESULT_VARIABLE labelresult
)
if(NOT ${labelresult} EQUAL 0)
message(FATAL_ERROR
"Error running test executable '${TEST_EXECUTABLE}':\n"
" Result: ${labelresult}\n"
" Output: ${labeloutput}\n"
)
endif()

string(REPLACE "\n" ";" labeloutput "${labeloutput}")
foreach(labelline ${labeloutput})
if("${labelline}" STREQUAL "===============================================================================" OR "${labelline}" MATCHES [==[^\[doctest\] ]==])
continue()
endif()
list(APPEND labels ${labelline})
endforeach()
endif()

if(NOT "${junit_output_dir}" STREQUAL "")
# turn testname into a valid filename by replacing all special characters with "-"
string(REGEX REPLACE "[/\\:\"|<>]" "-" test_filename "${test}")
Expand All @@ -77,8 +103,10 @@ foreach(line ${output})
"${prefix}${test}${suffix}"
PROPERTIES
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
LABELS ${labels}
${properties}
)
unset(labels)
list(APPEND tests "${prefix}${test}${suffix}")
endforeach()

Expand Down

0 comments on commit 680d044

Please sign in to comment.