@@ -221,6 +221,10 @@ function(catch_discover_tests_impl)
221221 set (script)
222222 set (suite)
223223 set (tests)
224+ # Holds the list of all registered test names **as a string**, not list.
225+ # This avoids issue of CMake removing semicolon escapes even inside
226+ # bracket-quoted strings.
227+ set (_test_names)
224228
225229 if (WIN32 )
226230 set (dl_paths_variable_name PATH )
@@ -431,13 +435,23 @@ function(catch_discover_tests_impl)
431435 string (APPEND script "${_Command} " )
432436 endif ()
433437
434- list (APPEND tests "${prefix}${plain_name}${suffix} " )
438+ # The test name has to be escaped using the same rules as prepare_command
439+ # uses for the arguments, so that it keeps being single element in list
440+ # even with weird characters and semicolons.
441+ set (full_list_name "${prefix}${plain_name}${suffix} " )
442+ string (REPLACE ";" "\\ ;" full_list_name "${full_list_name} " )
443+ if (full_list_name MATCHES "[^-./:a-zA-Z0-9_]" )
444+ # The space before the start of quote is important, so that we get
445+ # space-separated list in the final file.
446+ string (APPEND _test_names " [==[${full_list_name} ]==]" )
447+ else ()
448+ string (APPEND _test_names " ${full_list_name} " )
449+ endif ()
435450 endforeach ()
436451
437452 # Create a list of all discovered tests, which users may use to e.g. set
438453 # properties on the tests
439- prepare_command (set ${_TEST_LIST} ${tests} )
440- string (APPEND script "${_Command} " )
454+ string (APPEND script "set(${_TEST_LIST}${_test_names} )\n " )
441455
442456 # Write any script leftovers we have
443457 file (APPEND "${_CTEST_FILE} " "${script} " )
0 commit comments