Fix escape character handling in test display names with multi-target framework optimization#6174
Merged
Conversation
Copilot
AI
changed the title
[WIP] passed/failed/skipped {TestDisplayName} interprets escape characters
Fix escape character handling in test display names
Jul 25, 2025
Evangelink
reviewed
Jul 25, 2025
Member
Evangelink
left a comment
There was a problem hiding this comment.
Update the added tests to be parameterized and test all the special characters we handle
Contributor
Author
Updated the tests to be parameterized with all 32 C0 control characters (U+0000-U+001F) that are handled by the |
- Enhanced NormalizeSpecialCharacters to handle all C0 control characters (U+0000-U+001F) - Added NormalizeTestDisplayName method to apply normalization to test names - Updated RenderTestCompleted to normalize display names when showing test results - Updated TestDiscovered and TestInProgress to normalize display names when storing - Added comprehensive tests for escape character normalization - Control characters like \n, \t, \r, ESC are now displayed as Unicode control pictures (␊, ␉, ␍, ␛) This prevents escape characters in test display names from breaking console output formatting. Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
b4acefb to
1913168
Compare
Evangelink
approved these changes
Jul 30, 2025
Evangelink
reviewed
Jul 30, 2025
nohwnd
reviewed
Jul 31, 2025
Co-authored-by: Jakub Jareš <me@jakubjares.com>
Co-authored-by: Jakub Jareš <me@jakubjares.com>
nohwnd
reviewed
Jul 31, 2025
nohwnd
reviewed
Jul 31, 2025
nohwnd
reviewed
Jul 31, 2025
Co-authored-by: nohwnd <5735905+nohwnd@users.noreply.github.com>
Co-authored-by: nohwnd <5735905+nohwnd@users.noreply.github.com>
# Conflicts: # src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs
Youssef1313
reviewed
Aug 4, 2025
Youssef1313
reviewed
Aug 4, 2025
Youssef1313
previously approved these changes
Aug 4, 2025
… use char.IsControl Co-authored-by: nohwnd <5735905+nohwnd@users.noreply.github.com>
Evangelink
approved these changes
Aug 13, 2025
Youssef1313
approved these changes
Aug 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes the issue where test display names containing escape characters (like
\n,\t, etc.) are interpreted as actual control characters in console output instead of being displayed literally, and includes performance optimization with multi-targeting framework support.Problem
When test display names contain escape characters, they break the console output formatting. As shown in the original issue, a test framework publishing test nodes with display names like
"Hello(\nWorld {i})"causes the console output to be malformed:The newlines in test names are interpreted as actual line breaks, making the output difficult to read and potentially interfering with terminal functionality.
Solution
The fix replaces all C0 control characters (U+0000-U+001F) with their Unicode control picture equivalents:
\n(newline) →␊(U+240A)\t(tab) →␉(U+2409)\r(carriage return) →␍(U+240D)\x001B(escape) →␛(U+241B)\0(null) →␀(U+2400)After the fix, test names like
"Hello(\nWorld 0)"display cleanly as"Hello(␊World 0)"where the control character is visible but doesn't break the console layout.Multi-Targeting Framework Optimization
The
NormalizeSpecialCharactersmethod has been optimized with conditional compilation to support the project's multi-targeting requirements (netstandard2.0, net6.0, net7.0, net8.0, net9.0):For .NET 8.0 and later:
SearchValues<char>for O(1) detection of control charactersAsSpan().IndexOfAny()for optimal performanceFor earlier frameworks (netstandard2.0, net6.0, net7.0):
char[]arrays withIndexOfAny()Both implementations provide:
StringBuilderImplementation
Enhanced
NormalizeSpecialCharactersmethod:Applied normalization in key locations:
RenderTestCompleted- for test execution results outputTestDiscovered- when storing discovered test namesTestInProgress- when tracking active testsFiles Modified
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cstest/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterTests.csTesting
✅ Added comprehensive parameterized unit tests covering all 32 C0 control characters (U+0000-U+001F)
✅ Manual verification confirms the fix resolves console formatting issues
✅ Existing tests preserved - simple test names without special characters remain unchanged
✅ Multi-targeting validation - optimization works across all supported framework versions
✅ Performance optimized - normalization only occurs when special characters are present with minimal overhead
Impact
Fixes #5133.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.