Skip to content

Commit

Permalink
Handle ansi escape in terminal logger reporter (#5084)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Jun 17, 2024
1 parent 86cc894 commit 7cb67f1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Microsoft.TestPlatform.Build/Tasks/VSTestTask2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ public class VSTestTask2 : ToolTask, ITestTask

private readonly string _messageSplitter = "||||";
private readonly string[] _messageSplitterArray = new[] { "||||" };
private readonly string _ansiReset = "\x1b[39;49m";

private readonly bool _disableUtf8ConsoleEncoding;
private readonly bool _canBePrependedWithAnsi;

protected override string? ToolName => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";

public VSTestTask2()
{
// Unless user opted out, use UTF encoding, which we force in vstest.console.
_disableUtf8ConsoleEncoding = Environment.GetEnvironmentVariable("VSTEST_DISABLE_UTF8_CONSOLE_ENCODING") == "1";
var isPrependedWithAnsi = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION");
// On macOS and Linux the messages are prepended with ANSI reset sequence.
_canBePrependedWithAnsi = isPrependedWithAnsi?.ToLowerInvariant() == "true" || isPrependedWithAnsi == "1";
LogStandardErrorAsError = false;
StandardOutputImportance = "High";
}
Expand All @@ -66,6 +71,17 @@ protected override void LogEventsFromTextOutput(string singleLine, MessageImport
{
var useTerminalLogger = true;
Debug.WriteLine($"VSTESTTASK2: Received output {singleLine}, importance {messageImportance}");
bool wasPrependedWithAnsi = false;

if (_canBePrependedWithAnsi)
{
while (singleLine.StartsWith(_ansiReset))
{
wasPrependedWithAnsi = true;
singleLine = singleLine.Substring(_ansiReset.Length);
}
}

if (TryGetMessage(singleLine, out string name, out string?[] data))
{
// See MSBuildLogger.cs for the messages produced.
Expand All @@ -75,7 +91,9 @@ protected override void LogEventsFromTextOutput(string singleLine, MessageImport
case "output-info":
if (data[0] != null)
{
LogMSBuildOutputMessage(data[0]!);
// This is console output was prepended with ANSI reset, add it back.
string info = wasPrependedWithAnsi ? _ansiReset + data[0]! : data[0]!;
LogMSBuildOutputMessage(info);
}
break;
case "output-warning":
Expand Down

0 comments on commit 7cb67f1

Please sign in to comment.