Skip to content

Commit 42ee66b

Browse files
authored
Fix handling of semicolon and backslash characters in CMake test discovery (#2676)
This PR fixes the handling of semicolon and backslash characters in test names in the CMake test discovery Closes #2674
1 parent a0c6a28 commit 42ee66b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

extras/CatchAddTests.cmake

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function(catch_discover_tests_impl)
7474
)
7575
endif()
7676

77+
# Make sure to escape ; (semicolons) in test names first, because
78+
# that'd break the foreach loop for "Parse output" later and create
79+
# wrongly splitted and thus failing test cases (false positives)
80+
string(REPLACE ";" "\;" output "${output}")
7781
string(REPLACE "\n" ";" output "${output}")
7882

7983
# Prepare reporter
@@ -119,15 +123,16 @@ function(catch_discover_tests_impl)
119123

120124
# Parse output
121125
foreach(line ${output})
122-
set(test ${line})
126+
set(test "${line}")
123127
# Escape characters in test case names that would be parsed by Catch2
124-
set(test_name ${test})
125-
foreach(char , [ ])
126-
string(REPLACE ${char} "\\${char}" test_name ${test_name})
128+
# Note that the \ escaping must happen FIRST! Do not change the order.
129+
set(test_name "${test}")
130+
foreach(char \\ , [ ])
131+
string(REPLACE ${char} "\\${char}" test_name "${test_name}")
127132
endforeach(char)
128133
# ...add output dir
129134
if(output_dir)
130-
string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean ${test_name})
135+
string(REGEX REPLACE "[^A-Za-z0-9_]" "_" test_name_clean "${test_name}")
131136
set(output_dir_arg "--out ${output_dir}/${output_prefix}${test_name_clean}${output_suffix}")
132137
endif()
133138

tests/TestScripts/DiscoverTests/register-tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
#include <catch2/catch_test_macros.hpp>
1010

11+
TEST_CASE("@Script[C:\\EPM1A]=x;\"SCALA_ZERO:\"", "[script regressions]"){}
1112
TEST_CASE("Some test") {}

0 commit comments

Comments
 (0)