Repository navigation
Fix IndexOutOfRangeException in VSTestBridge BuildFilter UID escaping - #9771
Conversation
ContextAdapterBase.BuildFilter reconstructs a VSTest TestCaseFilter from a
TestNodeUidListFilter (the by-UID selection IDEs send over the server protocol).
Its hand-rolled escaper guarded the "is this char already escaped?" check with the
node index (i) instead of the character index (k): it read
currentTestNodeUid.Value[k - 1] while testing i - 1 < 0. For any node after the
first (i > 0) whose first character (k == 0) is a filter operator, this evaluated
Value[-1] and threw IndexOutOfRangeException, aborting the whole run. The same
index confusion could also under-escape an operator that follows a literal
backslash.
Replace the loop with VSTest's canonical FilterHelper.Escape, which escapes every
filter operator ('\ ( ) & | = ! ~') unconditionally -- exactly what a raw
test-node UID needs -- removing the index bug.
Add RunContextAdapterFilterTests covering BuildFilter output: FullyQualifiedName
vs Id=<guid> selection, multi-node OR joining, runsettings/command-line filters,
operator escaping, and the two regressions above (a later node starting with an
operator no longer throws; backslash-then-operator escapes both characters).
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR fixes an IndexOutOfRangeException in ContextAdapterBase.BuildFilter (in the VSTestBridge), which reconstructs a VSTest TestCaseFilter string from a by-UID TestNodeUidListFilter sent by IDEs over the Microsoft.Testing.Platform server protocol. The hand-rolled escaper's "already escaped?" guard mistakenly tested the node index i while indexing with the character index k (if (i - 1 < 0 || currentTestNodeUid.Value[k - 1] != '\\')), so any node after the first whose first character was a filter operator evaluated Value[-1] and threw, aborting the whole run. The same index confusion could also under-escape an operator following a literal backslash, splitting a FullyQualifiedName value. The fix replaces the loop with VSTest's canonical FilterHelper.Escape, which unconditionally escapes every filter operator — the correct behavior for a raw, un-escaped test-node UID.
Changes:
- Replaced the buggy hand-rolled per-character escaper in
BuildFilterwithFilterHelper.Escape(already available via the existingMicrosoft.TestPlatform.Filter.Sourcepackage reference andMicrosoft.VisualStudio.TestPlatform.Common.Filteringusing directive). - Added
RunContextAdapterFilterTestswith 14 tests exercisingBuildFilterthrough the publicRunContextAdapter.GetTestCaseFilter, covering FQN vsId=selection, multi-node OR joining, runsettings/command-line filter combination, full operator escaping, and explicit regressions for both bug variants.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/ContextAdapterBase.cs |
Removes the off-by-index hand-rolled escaper and delegates to FilterHelper.Escape, eliminating the IndexOutOfRangeException and under-escaping bug. |
test/UnitTests/Microsoft.Testing.Extensions.VSTestBridge.UnitTests/ObjectModel/RunContextAdapterFilterTests.cs |
New MSTest-based coverage for the filter-building logic, including targeted regression tests for the fixed index bug. |
There was a problem hiding this comment.
Reviewed in depth (manual pass + a second expert-review pass) and built/ran the test project locally — all 14 tests pass on net8.0, net9.0 and net462, 0 warnings.
Verdict: LGTM. The root cause is correctly diagnosed: the old escaper guard bounds-checked the node index i but indexed with the char index k (Value[k - 1] while testing i - 1 < 0), so any node after the first whose first char is a filter operator read Value[-1] and threw IndexOutOfRangeException; it also under-escaped an operator following a literal backslash. Replacing it with FilterHelper.Escape (VSTest canonical escaper, already reachable via the imported ...Common.Filtering namespace) is the right fix — MTP sends raw, unescaped UIDs, so escaping every operator unconditionally is exactly correct, and the Escape -> FilterExpressionWrapper/Condition tokenizer -> Unescape round-trip is lossless. I independently decoded every expected string in the tests against the actual output of Escape and they all match, including the subtle A.B\\\|C triple-backslash case.
Only a few optional, non-blocking follow-ups below.
- (Low) An empty UID list (
new TestNodeUidListFilter([])) is constructible and yields the filter string"()"(pre-existing behavior, not introduced here). Since this PR adds the definitive filter-building suite, a test pinning that outcome would be nice to have.
No public API added; assertion style and conventions are compliant. Thanks for the thorough tests!
Assert the built filter not only produces the expected escaped string but also actually matches (via ITestCaseFilterExpression.MatchTestCase) a test case whose FullyQualifiedName is exactly the escaped name and rejects others, guarding the full escape -> parse -> match path against a future parser-side regression. Co-authored-by: Copilot App <[email protected]>
…ode and empty-list coverage Co-authored-by: Copilot <[email protected]>
|
Addressed the follow-ups from your review:
All tests pass locally on net8.0 (net9.0 SDK is not installed on my box, but the change is test-only). |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
TestNodeUid lives in Microsoft.Testing.Platform.Extensions.Messages; that using was removed as 'unused' but CreateUidFilter constructs TestNodeUid, so all TFMs failed to compile with CS0246. Re-add the directive in alphabetical order. Co-authored-by: Copilot App <[email protected]>
|
/backport to rel/4.3 |
|
Started backporting to rel/4.3: https://github.com/microsoft/testfx/actions/runs/29831564084 |
… by @azat-msft in #9771 (backport to rel/4.3) (#10112) Co-authored-by: Azat Mukhametshin <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Amaury Levé <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Summary
ContextAdapterBase.BuildFilterreconstructs a VSTestTestCaseFilterstring from aTestNodeUidListFilter— the by-UID test selection that IDEs (e.g. Visual Studio Test Explorer) send over the Microsoft.Testing.Platform server protocol. Its hand-rolled escaper had an index bug: the "is this character already escaped?" guard used the node indexiinstead of the character indexk:It reads
Value[k - 1]but testsi - 1 < 0. So for any node after the first (i > 0) whose first character (k == 0) is a filter operator (\ ( ) & | = ! ~), it evaluatesValue[-1]and throwsIndexOutOfRangeException, aborting the whole run. The same index confusion could also under-escape an operator that follows a literal backslash, splitting aFullyQualifiedNamevalue at what should be an escaped operator.Fix
Replace the hand-rolled loop with VSTest's canonical
FilterHelper.Escape, which escapes every filter operator unconditionally — exactly what a raw (un-escaped) test-node UID needs — removing the index bug entirely.Tests
Adds
RunContextAdapterFilterTestscoveringBuildFilteroutput through the publicRunContextAdapter.GetTestCaseFilter(...):FullyQualifiedName=vsId=<guid>selection (and mixed), multi-node|(OR) joining.TestCaseFilterand command-line--filter, and their&combination with a UID selection.i == 0) and a later node (i > 0) whose name starts with a filter operator both escape and no longer throw.TestNodeUidListFilter([])behavior pinned.MatchTestCasetests that assert the built filter actually matches the exact test case and rejects near-misses, guarding the full escape → parse → match path.All 20 tests pass (net8.0, net9.0, net462).