-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unescape \u in test output so ANSI color codes can be interpreted cor…
…rectly (#984) * Unescape \u when reading test output. This fix colors when running test in `dotnet test` * Disable windows specific test when running on linux * Add Unix test alternative for DumpXmlTests.ThatPathIsCorrectlyParsedInDiscoveryPhase * Fix azure devops pipeline on windows * Fix build / test pipeline on azure devops (Linux) * Convert UnEscapeUnicodeCharacters to an extension method
- Loading branch information
Showing
8 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.NUnitEngine | ||
{ | ||
internal static class UnicodeEscapeHelper | ||
{ | ||
public static string UnEscapeUnicodeCharacters(this string text) | ||
{ | ||
if (text == null) | ||
return null; | ||
|
||
// Small optimization, if there are no "\u", then there is no need to rewrite the string | ||
var firstEscapeIndex = text.IndexOf("\\u", StringComparison.Ordinal); | ||
if (firstEscapeIndex == -1) | ||
return text; | ||
|
||
var stringBuilder = new StringBuilder(text.Substring(0, firstEscapeIndex)); | ||
for (var position = firstEscapeIndex; position < text.Length; position++) | ||
{ | ||
char c = text[position]; | ||
if (c == '\\' && TryUnEscapeOneCharacter(text, position, out var escapedChar, out var extraCharacterRead)) | ||
{ | ||
stringBuilder.Append(escapedChar); | ||
position += extraCharacterRead; | ||
} | ||
else | ||
{ | ||
stringBuilder.Append(c); | ||
} | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
private static bool TryUnEscapeOneCharacter(string text, int position, out char escapedChar, out int extraCharacterRead) | ||
{ | ||
const string unicodeEscapeSample = "u0000"; | ||
|
||
extraCharacterRead = 0; | ||
escapedChar = '\0'; | ||
if (position + unicodeEscapeSample.Length >= text.Length) | ||
return false; | ||
|
||
|
||
extraCharacterRead = unicodeEscapeSample.Length; | ||
if (!int.TryParse(text.Substring(position + 2, unicodeEscapeSample.Length - 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var escapeValue)) | ||
return false; | ||
|
||
escapedChar = (char)escapeValue; | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
19 changes: 19 additions & 0 deletions
19
src/NUnitTestAdapterTests/NUnitEngineTests/UnicodeEscapeHelperTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using NUnit.Framework; | ||
using NUnit.VisualStudio.TestAdapter.NUnitEngine; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.Tests.NUnitEngineTests | ||
{ | ||
public class UnicodeEscapeHelperTests | ||
{ | ||
[TestCase("\\u001b", "\u001b")] | ||
[TestCase("\\u001", "\\u001")] | ||
[TestCase("\\u01", "\\u01")] | ||
[TestCase("\\u1", "\\u1")] | ||
[TestCase("\\u001b6", "\u001b6")] | ||
[TestCase("some-text", "some-text")] | ||
public void UnEscapeUnicodeCharacters_ShouldReplaceBackslashU(string value, string expected) | ||
{ | ||
Assert.That(value.UnEscapeUnicodeCharacters(), Is.EqualTo(expected)); | ||
} | ||
} | ||
} |