Skip to content

Commit 90dfd7a

Browse files
authored
Escape special characters when building test filters (#1926)
Co-authored-by: Richard Webb <[email protected]>
1 parent a7f3f72 commit 90dfd7a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Components/TestExplorer.fs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,32 @@ module Interactions =
11361136
)
11371137
|> Promise.ofThenable
11381138

1139+
let private filterEscapeCharacter = '\\'
1140+
let private filterSpecialCharacters = [| '\\'; '('; ')'; '&'; '|'; '='; '!'; '~' |]
1141+
let private filterSpecialCharactersSet = Set.ofArray filterSpecialCharacters
11391142

11401143
let private buildFilterExpression (tests: TestItem array) =
1144+
// Escape any special characters in test names, as per https://github.com/microsoft/vstest/blob/main/docs/filter.md
1145+
let escapeFilterExpression (str: string) =
1146+
1147+
if str.IndexOfAny(filterSpecialCharacters) < 0 then
1148+
str
1149+
else
1150+
let builder = StringBuilder()
1151+
1152+
for i = 0 to str.Length - 1 do
1153+
let currentChar = str[i]
1154+
1155+
if filterSpecialCharactersSet.Contains currentChar then
1156+
builder.Append(filterEscapeCharacter) |> ignore
1157+
1158+
builder.Append(currentChar) |> ignore
1159+
1160+
builder.ToString()
1161+
11411162
let testToFilterExpression (test: TestItem) =
1142-
let fullName = TestItem.getFullName test.id
1163+
let fullTestName = TestItem.getFullName test.id
1164+
let fullName = escapeFilterExpression fullTestName
11431165

11441166
if fullName.Contains(" ") && test.TestFramework = TestFrameworkId.NUnit then
11451167
// workaround for https://github.com/nunit/nunit3-vs-adapter/issues/876

0 commit comments

Comments
 (0)