Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/extras
diff options
context:
space:
mode:
authorMartin Hořeňovský2026-07-04 16:35:50 +0200
committerMartin Hořeňovský2026-07-04 16:36:05 +0200
commit9ec44dd62b1b593e01feaf95eb485d26fcdd50ce (patch)
tree70b32b57a08c8b3f5e21e2fca3cdcf9b5ae6ca99 /extras
parent675f9eaeb191c51b9d2ffb2bb198009533895051 (diff)
downloadcatch3-9ec44dd62b1b593e01feaf95eb485d26fcdd50ce.tar.gz
catch_discover_tests uses tempfile to retrieve JSON from the binary
This allows it to deal with badly behaved code, where 3rd party dependencies write into stdout during global construction. Closes #3162 Closes #3166
Diffstat (limited to 'extras')
-rw-r--r--extras/Catch.cmake4
-rw-r--r--extras/CatchAddTests.cmake47
2 files changed, 48 insertions, 3 deletions
diff --git a/extras/Catch.cmake b/extras/Catch.cmake
index fcdebd60..657bb724 100644
--- a/extras/Catch.cmake
+++ b/extras/Catch.cmake
@@ -44,7 +44,7 @@ same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
``catch_discover_tests`` sets up a post-build command on the test executable
that generates the list of tests by parsing the output from running the test
- with the ``--list-test-names-only`` argument. This ensures that the full
+ with the ``--list-tests --reporter json`` argument. This ensures that the full
list of tests is obtained. Since test discovery occurs at build time, it is
not necessary to re-run CMake when the list of tests changes.
However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
@@ -67,7 +67,7 @@ same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
``TEST_SPEC arg1...``
Specifies test cases, wildcarded test cases, tags and tag expressions to
- pass to the Catch executable with the ``--list-test-names-only`` argument.
+ pass to the Catch executable when listing the tests.
``EXTRA_ARGS arg1...``
Any extra arguments to pass on the command line to each test case.
diff --git a/extras/CatchAddTests.cmake b/extras/CatchAddTests.cmake
index 4c27f479..be3d1c8c 100644
--- a/extras/CatchAddTests.cmake
+++ b/extras/CatchAddTests.cmake
@@ -16,6 +16,42 @@ function(add_command NAME)
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
endfunction()
+# Generates random filename in the temp folder.
+# Temp folder is retrieved by checking env vars from various platforms.
+function(make_temp_file_path OUT_VARIABLE FALLBACK_PATH)
+ set(TEMP_DIR "")
+ set(ENV_VARS
+ # From XDG base dir specification
+ XDG_RUNTIME_DIR
+ # From POSIX standard
+ TMPDIR
+ # From Windows
+ TMP
+ TEMP
+ )
+
+ foreach(var ${ENV_VARS})
+ message(NOTICE "Checking ${var} env var")
+ if(DEFINED ENV{${var}} AND NOT "$ENV{${var}}" STREQUAL "")
+ set(TEMP_DIR "$ENV{${var}}")
+ break()
+ endif()
+ endforeach()
+
+ # If all checks fail, we use the fallback path
+ if(TEMP_DIR STREQUAL "")
+ set(TEMP_DIR "${FALLBACK_PATH}")
+ endif()
+
+ file(TO_CMAKE_PATH "${TEMP_DIR}" TEMP_DIR)
+
+ # Generate the random file name
+ string(RANDOM LENGTH 8 RAND_ID)
+ set(FINAL_TEMP_PATH "${TEMP_DIR}/Catch2-test-listing.${RAND_ID}.json")
+
+ set(${OUT_VARIABLE} "${FINAL_TEMP_PATH}" PARENT_SCOPE)
+endfunction()
+
function(catch_discover_tests_impl)
cmake_parse_arguments(
@@ -72,8 +108,13 @@ function(catch_discover_tests_impl)
set(ENV{DYLD_FRAMEWORK_PATH} "${paths}")
endif()
+ make_temp_file_path(listing_output_path "${_TEST_WORKING_DIR}")
+
execute_process(
- COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" ${spec} --list-tests --reporter json
+ COMMAND ${_TEST_EXECUTOR} "${_TEST_EXECUTABLE}" ${spec}
+ --list-tests
+ --reporter json
+ --out "${listing_output_path}"
OUTPUT_VARIABLE listing_output
RESULT_VARIABLE result
WORKING_DIRECTORY "${_TEST_WORKING_DIR}"
@@ -86,6 +127,10 @@ function(catch_discover_tests_impl)
)
endif()
+ # Read the JSON output back from the output file (and then get rid of the file)
+ file(READ ${listing_output_path} listing_output)
+ file(REMOVE ${listing_output_path})
+
# Prepare reporter
if(reporter)
set(reporter_arg "--reporter ${reporter}")