Skip to content

Commit

Permalink
feat(cmake): Add JUNIT_OUTPUT_DIR option to doctest_discover_tests (#417
Browse files Browse the repository at this point in the history
)

Co-authored-by: Dennis Hezel <dh@3yourmind.com>
  • Loading branch information
Tradias and Tradias committed Sep 6, 2020
1 parent 51e4a0d commit 428b697
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion scripts/cmake/doctest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
[TEST_SUFFIX suffix]
[PROPERTIES name1 value1...]
[TEST_LIST var]
[JUNIT_OUTPUT_DIR dir]
)

``doctest_discover_tests`` sets up a post-build command on the test executable
Expand Down Expand Up @@ -90,14 +91,21 @@ same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
executable is being used in multiple calls to ``doctest_discover_tests()``.
Note that this variable is only available in CTest.

``JUNIT_OUTPUT_DIR dir``
If specified, the parameter is passed along with ``--reporters=junit``
and ``--out=`` to the test executable. The actual file name is the same
as the test target, including prefix and suffix. This should be used
instead of EXTRA_ARGS to avoid race conditions writing the XML result
output when using parallel test execution.

#]=======================================================================]

#------------------------------------------------------------------------------
function(doctest_discover_tests TARGET)
cmake_parse_arguments(
""
""
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST"
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;JUNIT_OUTPUT_DIR"
"TEST_SPEC;EXTRA_ARGS;PROPERTIES"
${ARGN}
)
Expand Down Expand Up @@ -134,6 +142,7 @@ function(doctest_discover_tests TARGET)
-D "TEST_PREFIX=${_TEST_PREFIX}"
-D "TEST_SUFFIX=${_TEST_SUFFIX}"
-D "TEST_LIST=${_TEST_LIST}"
-D "TEST_JUNIT_OUTPUT_DIR=${_JUNIT_OUTPUT_DIR}"
-D "CTEST_FILE=${ctest_tests_file}"
-P "${_DOCTEST_DISCOVER_TESTS_SCRIPT}"
VERBATIM
Expand Down
9 changes: 9 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(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}")
set(script)
set(suite)
set(tests)
Expand Down Expand Up @@ -54,6 +55,13 @@ foreach(line ${output})
continue()
endif()
set(test ${line})
if(NOT "${junit_output_dir}" STREQUAL "")
# turn testname into a valid filename by replacing all special characters with "-"
string(REGEX REPLACE "[/\\:\"|<>]" "-" test_filename "${test}")
set(TEST_JUNIT_OUTPUT_PARAM "--reporters=junit" "--out=${junit_output_dir}/${prefix}${test_filename}${suffix}.xml")
else()
unset(TEST_JUNIT_OUTPUT_PARAM)
endif()
# use escape commas to handle properly test cases with commas inside the name
string(REPLACE "," "\\," test_name ${test})
# ...and add to script
Expand All @@ -62,6 +70,7 @@ foreach(line ${output})
${TEST_EXECUTOR}
"${TEST_EXECUTABLE}"
"--test-case=${test_name}"
"${TEST_JUNIT_OUTPUT_PARAM}"
${extra_args}
)
add_command(set_tests_properties
Expand Down

0 comments on commit 428b697

Please sign in to comment.