Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ansi escape in terminal logger reporter #5084

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading