Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,29 @@ public TerminalTestReporter(
int nonAnsiUpdateCadenceInMs = 3_000;
// When writing to ANSI we update the progress in place and it should look responsive so we update every half second, because we only show seconds on the screen, so it is good enough.
int ansiUpdateCadenceInMs = 500;
if (!_options.UseAnsi || _options.ForceAnsi is false)
if (_options.AnsiMode == AnsiMode.SimpleAnsi)
{
terminalWithProgress = new TestProgressStateAwareTerminal(new NonAnsiTerminal(console), showProgress, writeProgressImmediatelyAfterOutput: false, updateEvery: nonAnsiUpdateCadenceInMs);
// We are told externally that we are in CI, use simplified ANSI mode.
terminalWithProgress = new TestProgressStateAwareTerminal(new SimpleAnsiTerminal(console), showProgress, writeProgressImmediatelyAfterOutput: true, updateEvery: nonAnsiUpdateCadenceInMs);
}
else
{
if (_options.UseCIAnsi)
// We are not in CI, or in CI non-compatible with simple ANSI, autodetect terminal capabilities
(bool consoleAcceptsAnsiCodes, bool _, uint? originalConsoleMode) = NativeMethods.QueryIsScreenAndTryEnableAnsiColorCodes();
_originalConsoleMode = originalConsoleMode;
bool useAnsi = _options.AnsiMode switch
{
// We are told externally that we are in CI, use simplified ANSI mode.
terminalWithProgress = new TestProgressStateAwareTerminal(new SimpleAnsiTerminal(console), showProgress, writeProgressImmediatelyAfterOutput: true, updateEvery: nonAnsiUpdateCadenceInMs);
}
else
{
// We are not in CI, or in CI non-compatible with simple ANSI, autodetect terminal capabilities
(bool consoleAcceptsAnsiCodes, bool _, uint? originalConsoleMode) = NativeMethods.QueryIsScreenAndTryEnableAnsiColorCodes();
_originalConsoleMode = originalConsoleMode;
terminalWithProgress = consoleAcceptsAnsiCodes || _options.ForceAnsi is true
? new TestProgressStateAwareTerminal(new AnsiTerminal(console), showProgress, writeProgressImmediatelyAfterOutput: true, updateEvery: ansiUpdateCadenceInMs)
: new TestProgressStateAwareTerminal(new NonAnsiTerminal(console), showProgress, writeProgressImmediatelyAfterOutput: false, updateEvery: nonAnsiUpdateCadenceInMs);
}
AnsiMode.ForceAnsi => true,
AnsiMode.NoAnsi => false,
AnsiMode.AnsiIfPossible => consoleAcceptsAnsiCodes,
_ => throw ApplicationStateGuard.Unreachable(),
};

terminalWithProgress = new TestProgressStateAwareTerminal(
useAnsi ? new AnsiTerminal(console) : new NonAnsiTerminal(console),
showProgress,
writeProgressImmediatelyAfterOutput: useAnsi,
updateEvery: useAnsi ? ansiUpdateCadenceInMs : nonAnsiUpdateCadenceInMs);
}

_terminalWithProgress = terminalWithProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@ internal sealed class TerminalTestReporterOptions
public bool ShowActiveTests { get; init; }

/// <summary>
/// Gets a value indicating whether we should use ANSI escape codes or disable them. When true the capabilities of the console are autodetected.
/// Gets a value indicating the ANSI mode.
/// </summary>
public bool UseAnsi { get; init; }
public AnsiMode AnsiMode { get; init; }
}

internal enum AnsiMode
{
/// <summary>
/// Disable ANSI escape codes.
/// </summary>
NoAnsi,

/// <summary>
/// Use simplified ANSI renderer, which colors output, but does not move cursor.
/// This is used in compatible CI environments.
/// </summary>
SimpleAnsi,

/// <summary>
/// Gets a value indicating whether we are running in compatible CI, and should use simplified ANSI renderer, which colors output, but does not move cursor.
/// Setting <see cref="UseAnsi"/> to false will disable this option.
/// Enable ANSI escape codes, including cursor movement, when the capabilities of the console allow it.
/// </summary>
public bool UseCIAnsi { get; init; }
AnsiIfPossible,

/// <summary>
/// Gets a value indicating whether we should force ANSI escape codes. When true the ANSI is used without auto-detecting capabilities of the console. This is needed only for testing.
/// Force ANSI escape codes, regardless of the capabilities of the console.
/// This is needed only for testing.
/// </summary>
internal /* for testing */ bool? ForceAnsi { get; init; }
ForceAnsi,
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ await _policiesService.RegisterOnAbortCallbackAsync(

// TODO: Replace this with proper CI detection that we already have in telemetry. https://github.com/microsoft/testfx/issues/5533#issuecomment-2838893327
bool inCI = string.Equals(_environment.GetEnvironmentVariable("TF_BUILD"), "true", StringComparison.OrdinalIgnoreCase) || string.Equals(_environment.GetEnvironmentVariable("GITHUB_ACTIONS"), "true", StringComparison.OrdinalIgnoreCase);

AnsiMode ansiMode = AnsiMode.AnsiIfPossible;
if (noAnsi)
{
// User explicitly specified --no-ansi.
// We should respect that.
ansiMode = AnsiMode.NoAnsi;
}
else if (inCI)
{
ansiMode = AnsiMode.SimpleAnsi;
}

bool noProgress = _commandLineOptions.IsOptionSet(TerminalTestReporterCommandLineOptionsProvider.NoProgressOption);

// _runtimeFeature.IsHotReloadEnabled is not set to true here, even if the session will be HotReload,
Expand Down Expand Up @@ -158,8 +171,7 @@ await _policiesService.RegisterOnAbortCallbackAsync(
{
ShowPassedTests = showPassed,
MinimumExpectedTests = PlatformCommandLineProvider.GetMinimumExpectedTests(_commandLineOptions),
UseAnsi = !noAnsi,
UseCIAnsi = inCI,
AnsiMode = ansiMode,
ShowActiveTests = true,
ShowProgress = shouldShowProgress,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void NonAnsiTerminal_OutputFormattingIsCorrect()
ShowPassedTests = () => true,

// Like --no-ansi in commandline, should disable ANSI altogether.
UseAnsi = false,
AnsiMode = AnsiMode.NoAnsi,

ShowProgress = () => false,
});
Expand Down Expand Up @@ -177,10 +177,9 @@ public void SimpleAnsiTerminal_OutputFormattingIsCorrect()
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,

// Like if we autodetect that we are in CI (e.g. by looking at TF_BUILD, and we don't disable ANSI.
UseAnsi = true,
UseCIAnsi = true,
ForceAnsi = true,
AnsiMode = AnsiMode.SimpleAnsi,

ShowProgress = () => false,
});
Expand Down Expand Up @@ -278,9 +277,7 @@ public void AnsiTerminal_OutputFormattingIsCorrect()
{
ShowPassedTests = () => true,
// Like if we autodetect that we are in ANSI capable terminal.
UseAnsi = true,
UseCIAnsi = false,
ForceAnsi = true,
AnsiMode = AnsiMode.ForceAnsi,

ShowProgress = () => false,
});
Expand Down Expand Up @@ -379,9 +376,7 @@ public void AnsiTerminal_OutputProgressFrameIsCorrect()
{
ShowPassedTests = () => true,
// Like if we autodetect that we are in ANSI capable terminal.
UseAnsi = true,
UseCIAnsi = false,
ForceAnsi = true,
AnsiMode = AnsiMode.ForceAnsi,

ShowActiveTests = true,
ShowProgress = () => true,
Expand Down Expand Up @@ -642,7 +637,7 @@ public void TestDisplayNames_WithControlCharacters_AreNormalized(char controlCha
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
UseAnsi = false,
AnsiMode = AnsiMode.NoAnsi,
ShowProgress = () => false,
});

Expand Down Expand Up @@ -722,7 +717,7 @@ public void TestDiscovery_WithControlCharacters_AreNormalized(char controlChar,
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
UseAnsi = false,
AnsiMode = AnsiMode.NoAnsi,
ShowProgress = () => false,
});

Expand Down Expand Up @@ -794,7 +789,7 @@ public void TerminalTestReporter_WhenInDiscoveryMode_ShouldIncrementDiscoveredTe
var terminalReporter = new TerminalTestReporter(assembly, "net8.0", "x64", stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => false,
UseAnsi = false,
AnsiMode = AnsiMode.NoAnsi,
ShowProgress = () => false,
});

Expand Down
Loading