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 @@ -65,17 +65,14 @@ internal sealed partial class TerminalTestReporter : IDisposable
public TerminalTestReporter(IConsole console, TerminalTestReporterOptions options)
{
_options = options;
TestProgressStateAwareTerminal terminalWithProgress;

// When not writing to ANSI we write the progress to screen and leave it there so we don't want to write it more often than every few seconds.
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;
bool showProgress = _options.ShowProgress;

ITerminal terminal;
if (_options.AnsiMode == AnsiMode.SimpleAnsi)
{
// We are told externally that we are in CI, use simplified ANSI mode.
terminalWithProgress = new TestProgressStateAwareTerminal(new SimpleAnsiTerminal(console), _options.ShowProgress, writeProgressImmediatelyAfterOutput: true, updateEvery: nonAnsiUpdateCadenceInMs);
terminal = new SimpleAnsiTerminal(console);
showProgress = false;
}
else
{
Expand All @@ -86,20 +83,16 @@ public TerminalTestReporter(IConsole console, TerminalTestReporterOptions option

bool useAnsi = _options.AnsiMode switch
{
AnsiMode.ForceAnsi => true,
AnsiMode.NoAnsi => false,
AnsiMode.AnsiIfPossible => consoleAcceptsAnsiCodes,
_ => throw new UnreachableException(),
};

terminalWithProgress = new TestProgressStateAwareTerminal(
useAnsi ? new AnsiTerminal(console, _options.BaseDirectory) : new NonAnsiTerminal(console),
_options.ShowProgress,
writeProgressImmediatelyAfterOutput: useAnsi,
updateEvery: useAnsi ? ansiUpdateCadenceInMs : nonAnsiUpdateCadenceInMs);
showProgress = showProgress && useAnsi;
terminal = useAnsi ? new AnsiTerminal(console, _options.BaseDirectory) : new NonAnsiTerminal(console);
}

_terminalWithProgress = terminalWithProgress;
_terminalWithProgress = new TestProgressStateAwareTerminal(terminal, showProgress);
}

public void TestExecutionStarted(DateTimeOffset testStartTime, int workerCount, bool isDiscovery, bool isHelp, bool isRetry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ internal sealed class TerminalTestReporterOptions
public int MinimumExpectedTests { get; init; }

/// <summary>
/// Gets a value indicating whether we should write the progress periodically to screen. When ANSI is allowed we update the progress as often as we can. When ANSI is not allowed we update it every 3 seconds.
/// Gets a value indicating whether we should write the progress periodically to screen. When ANSI is allowed we update the progress as often as we can.
/// When ANSI is not allowed we never have progress.
/// </summary>
public bool ShowProgress { get; init; }

Expand Down Expand Up @@ -63,10 +64,4 @@ internal enum AnsiMode
/// Enable ANSI escape codes, including cursor movement, when the capabilities of the console allow it.
/// </summary>
AnsiIfPossible,

/// <summary>
/// Force ANSI escape codes, regardless of the capabilities of the console.
/// This is needed only for testing.
/// </summary>
ForceAnsi,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.DotNet.Cli.Commands.Test.Terminal;
/// <summary>
/// Terminal that updates the progress in place when progress reporting is enabled.
/// </summary>
internal sealed partial class TestProgressStateAwareTerminal(ITerminal terminal, bool showProgress, bool writeProgressImmediatelyAfterOutput, int updateEvery) : IDisposable
internal sealed partial class TestProgressStateAwareTerminal(ITerminal terminal, bool showProgress) : IDisposable
{
/// <summary>
/// A cancellation token to signal the rendering thread that it should exit.
Expand All @@ -20,8 +20,6 @@ internal sealed partial class TestProgressStateAwareTerminal(ITerminal terminal,

private readonly ITerminal _terminal = terminal;
private readonly bool _showProgress = showProgress;
private readonly bool _writeProgressImmediatelyAfterOutput = writeProgressImmediatelyAfterOutput;
private readonly int _updateEvery = updateEvery;
private TestProgressState?[] _progressItems = [];

/// <summary>
Expand All @@ -37,7 +35,11 @@ private void ThreadProc()
{
try
{
while (!_cts.Token.WaitHandle.WaitOne(_updateEvery))
// 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.
// When writing to non-ANSI, we never show progress as the output can get long and messy.
const int AnsiUpdateCadenceInMs = 500;
while (!_cts.Token.WaitHandle.WaitOne(AnsiUpdateCadenceInMs))
{
lock (_lock)
{
Expand Down Expand Up @@ -118,10 +120,7 @@ internal void WriteToTerminal(Action<ITerminal> write)
_terminal.StartUpdate();
_terminal.EraseProgress();
write(_terminal);
if (_writeProgressImmediatelyAfterOutput)
{
_terminal.RenderProgress(_progressItems);
}
_terminal.RenderProgress(_progressItems);
}
finally
{
Expand Down
Loading