Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e53ae55
Terminal progress
drognanar Nov 25, 2024
046c328
Fixes
nohwnd Aug 23, 2024
f8d84dc
Update terminal reporting to include the active tests in progress
drognanar Dec 2, 2024
82e00bf
Add resource strings
drognanar Dec 2, 2024
3805a59
Fixup small code style issues
drognanar Dec 2, 2024
bb60c96
Merge remote-tracking branch 'microsoft/main' into show_running_tests
drognanar Dec 3, 2024
4bc3904
Remove long running test from playground
drognanar Dec 3, 2024
5af15db
Improve distribution of the additional lines to render
drognanar Dec 4, 2024
5bf23a4
Merge remote-tracking branch 'microsoft/main' into show_running_tests
drognanar Dec 4, 2024
f3f8469
Do not show the "... and 1 more test" message
drognanar Dec 4, 2024
7fb2340
Only show tests longer than 1s
drognanar Dec 4, 2024
5aed36c
Remove 1s limit when showing active tests
drognanar Dec 5, 2024
7d1305c
Add an option to disable display of active tests
drognanar Dec 5, 2024
eb81444
Small refactorings
drognanar Dec 5, 2024
d2b9c82
Show "{0} tests running" if no active tests can fit on the screen
drognanar Dec 5, 2024
7df3907
Add a progress indicator test
drognanar Dec 6, 2024
9713409
Update src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/…
drognanar Dec 6, 2024
366b428
Fix terminal logger reporter test for MacOS
drognanar Dec 6, 2024
43bac64
Merge branch 'show_running_tests' of https://github.com/drognanar/tes…
drognanar Dec 6, 2024
e2fa5d6
Use Interlocked for _counter updates
drognanar Dec 9, 2024
b3e5528
Implement the stopwatch directly as a class
drognanar Dec 9, 2024
f7ecea0
Merge remote-tracking branch 'microsoft/main' into show_running_tests
drognanar Dec 9, 2024
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 @@ -45,7 +45,7 @@ internal sealed class AnsiTerminal : ITerminal
private readonly bool _useBusyIndicator;
private readonly StringBuilder _stringBuilder = new();
private bool _isBatching;
private AnsiTerminalTestProgressFrame _currentFrame = new(Array.Empty<TestProgressState>(), 0, 0);
private AnsiTerminalTestProgressFrame _currentFrame = new(0, 0);

public AnsiTerminal(IConsole console, string? baseDirectory)
{
Expand Down Expand Up @@ -274,27 +274,20 @@ public void SetCursorHorizontal(int position)
/// </summary>
public void EraseProgress()
{
if (_currentFrame.ProgressCount == 0)
if (_currentFrame.RenderedLines == null || _currentFrame.RenderedLines.Count == 0)
{
return;
}

AppendLine($"{AnsiCodes.CSI}{_currentFrame.ProgressCount + 2}{AnsiCodes.MoveUpToLineStart}");
AppendLine($"{AnsiCodes.CSI}{_currentFrame.RenderedLines.Count + 2}{AnsiCodes.MoveUpToLineStart}");
Append($"{AnsiCodes.CSI}{AnsiCodes.EraseInDisplay}");
_currentFrame.Clear();
}

public void RenderProgress(TestProgressState?[] progress)
{
AnsiTerminalTestProgressFrame newFrame = new(progress, Width, Height);

// Do not render delta but clear everything if Terminal width or height have changed.
if (newFrame.Width != _currentFrame.Width || newFrame.Height != _currentFrame.Height)
{
EraseProgress();
}

newFrame.Render(_currentFrame, this);
AnsiTerminalTestProgressFrame newFrame = new(Width, Height);
newFrame.Render(_currentFrame, progress, terminal: this);

_currentFrame = newFrame;
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public static void Append(ITerminal terminal, TimeSpan duration, bool wrapInPare
private static string GetFormattedPart(int value, bool hasParentValue, string suffix, int paddingWitdh = 2)
=> $"{(hasParentValue ? " " : string.Empty)}{(hasParentValue ? value.ToString(CultureInfo.InvariantCulture).PadLeft(paddingWitdh, '0') : value.ToString(CultureInfo.InvariantCulture))}{suffix}";

public static string Render(TimeSpan duration, bool wrapInParentheses = true, bool showMilliseconds = false)
public static string Render(TimeSpan? duration, bool wrapInParentheses = true, bool showMilliseconds = false)
{
if (duration is null)
{
return string.Empty;
}

bool hasParentValue = false;

var stringBuilder = new StringBuilder();
Expand All @@ -66,35 +71,35 @@ public static string Render(TimeSpan duration, bool wrapInParentheses = true, bo
stringBuilder.Append('(');
}

if (duration.Days > 0)
if (duration.Value.Days > 0)
{
stringBuilder.Append(CultureInfo.CurrentCulture, $"{duration.Days}d");
stringBuilder.Append(CultureInfo.CurrentCulture, $"{duration.Value.Days}d");
hasParentValue = true;
}

if (duration.Hours > 0 || hasParentValue)
if (duration.Value.Hours > 0 || hasParentValue)
{
stringBuilder.Append(GetFormattedPart(duration.Hours, hasParentValue, "h"));
stringBuilder.Append(GetFormattedPart(duration.Value.Hours, hasParentValue, "h"));
hasParentValue = true;
}

if (duration.Minutes > 0 || hasParentValue)
if (duration.Value.Minutes > 0 || hasParentValue)
{
stringBuilder.Append(GetFormattedPart(duration.Minutes, hasParentValue, "m"));
stringBuilder.Append(GetFormattedPart(duration.Value.Minutes, hasParentValue, "m"));
hasParentValue = true;
}

if (duration.Seconds > 0 || hasParentValue || !showMilliseconds)
if (duration.Value.Seconds > 0 || hasParentValue || !showMilliseconds)
{
stringBuilder.Append(GetFormattedPart(duration.Seconds, hasParentValue, "s"));
stringBuilder.Append(GetFormattedPart(duration.Value.Seconds, hasParentValue, "s"));
hasParentValue = true;
}

if (showMilliseconds)
{
if (duration.Milliseconds >= 0 || hasParentValue)
if (duration.Value.Milliseconds >= 0 || hasParentValue)
{
stringBuilder.Append(GetFormattedPart(duration.Milliseconds, hasParentValue, "ms", paddingWitdh: 3));
stringBuilder.Append(GetFormattedPart(duration.Value.Milliseconds, hasParentValue, "ms", paddingWitdh: 3));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public void RenderProgress(TestProgressState?[] progress)

// Use just ascii here, so we don't put too many restrictions on fonts needing to
// properly show unicode, or logs being saved in particular encoding.
string? detail = !RoslynString.IsNullOrWhiteSpace(p.Detail) ? $"- {p.Detail}" : null;
TestDetailState? activeTest = p.TestNodeResultsState?.GetFirstRunningTask();
string? detail = !RoslynString.IsNullOrWhiteSpace(activeTest?.Text) ? $"- {activeTest.Text}" : null;
Append('[');
SetColor(TerminalColor.DarkGreen);
Append('+');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ internal sealed partial class TerminalTestReporter : IDisposable

internal Func<IStopwatch> CreateStopwatch { get; set; } = SystemStopwatch.StartNew;

internal event EventHandler OnProgressStartUpdate
{
add => _terminalWithProgress.OnProgressStartUpdate += value;
remove => _terminalWithProgress.OnProgressStartUpdate -= value;
}

internal event EventHandler OnProgressStopUpdate
{
add => _terminalWithProgress.OnProgressStopUpdate += value;
remove => _terminalWithProgress.OnProgressStopUpdate -= value;
}

private readonly ConcurrentDictionary<string, TestProgressState> _assemblies = new();

private readonly List<TestRunArtifact> _artifacts = new();
Expand Down Expand Up @@ -108,10 +120,12 @@ private static Regex GetFrameRegex()
}
#endif

private int _counter;

/// <summary>
/// Initializes a new instance of the <see cref="TerminalTestReporter"/> class with custom terminal and manual refresh for testing.
/// </summary>
internal TerminalTestReporter(IConsole console, TerminalTestReporterOptions options)
public TerminalTestReporter(IConsole console, TerminalTestReporterOptions options)
Comment thread
drognanar marked this conversation as resolved.
{
_options = options;

Expand Down Expand Up @@ -168,15 +182,15 @@ private TestProgressState GetOrAddAssemblyRun(string assembly, string? targetFra
return _assemblies.GetOrAdd(key, _ =>
{
IStopwatch sw = CreateStopwatch();
var assemblyRun = new TestProgressState(assembly, targetFramework, architecture, sw);
var assemblyRun = new TestProgressState(Interlocked.Increment(ref _counter), assembly, targetFramework, architecture, sw);
int slotIndex = _terminalWithProgress.AddWorker(assemblyRun);
assemblyRun.SlotIndex = slotIndex;

return assemblyRun;
});
}

internal void TestExecutionCompleted(DateTimeOffset endTime)
public void TestExecutionCompleted(DateTimeOffset endTime)
{
_testExecutionEndTime = endTime;
_terminalWithProgress.StopShowingProgress();
Expand Down Expand Up @@ -375,6 +389,7 @@ internal void TestCompleted(
string? targetFramework,
string? architecture,
string? executionId,
string testNodeUid,
string displayName,
TestOutcome outcome,
TimeSpan duration,
Expand All @@ -391,6 +406,7 @@ internal void TestCompleted(
targetFramework,
architecture,
executionId,
testNodeUid,
displayName,
outcome,
duration,
Expand All @@ -406,6 +422,7 @@ internal void TestCompleted(
string? targetFramework,
string? architecture,
string? executionId,
string testNodeUid,
string displayName,
TestOutcome outcome,
TimeSpan duration,
Expand All @@ -417,6 +434,11 @@ internal void TestCompleted(
{
TestProgressState asm = _assemblies[$"{assembly}|{targetFramework}|{architecture}|{executionId}"];

if (_options.ShowActiveTests)
{
asm.TestNodeResultsState?.RemoveRunningTestNode(testNodeUid);
}

switch (outcome)
{
case TestOutcome.Error:
Expand Down Expand Up @@ -802,7 +824,7 @@ public void ArtifactAdded(bool outOfProcess, string? assembly, string? targetFra
/// <summary>
/// Let the user know that cancellation was triggered.
/// </summary>
internal void StartCancelling()
public void StartCancelling()
{
_wasCancelled = true;
_terminalWithProgress.WriteToTerminal(terminal =>
Expand Down Expand Up @@ -857,7 +879,7 @@ internal void WriteWarningMessage(string assembly, string? targetFramework, stri
internal void WriteErrorMessage(string assembly, string? targetFramework, string? architecture, string? executionId, Exception exception)
=> WriteErrorMessage(assembly, targetFramework, architecture, executionId, exception.ToString(), padding: null);

internal void WriteMessage(string text, SystemConsoleColor? color = null, int? padding = null)
public void WriteMessage(string text, SystemConsoleColor? color = null, int? padding = null)
{
if (color != null)
{
Expand Down Expand Up @@ -980,4 +1002,24 @@ private static TerminalColor ToTerminalColor(ConsoleColor consoleColor)
ConsoleColor.White => TerminalColor.White,
_ => TerminalColor.Default,
};

public void TestInProgress(
string assembly,
string? targetFramework,
string? architecture,
string testNodeUid,
string displayName,
string? executionId)
{
TestProgressState asm = _assemblies[$"{assembly}|{targetFramework}|{architecture}|{executionId}"];

if (_options.ShowActiveTests)
{
asm.TestNodeResultsState ??= new(Interlocked.Increment(ref _counter));
asm.TestNodeResultsState.AddRunningTestNode(
Interlocked.Increment(ref _counter), testNodeUid, displayName, CreateStopwatch());
}

_terminalWithProgress.UpdateWorker(asm.SlotIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal sealed class TerminalTestReporterOptions
/// </summary>
public Func<bool?> ShowProgress { get; init; } = () => true;

/// <summary>
/// Gets a value indicating whether the active tests should be visible when the progress is shown.
/// </summary>
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.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.Helpers;

namespace Microsoft.Testing.Platform.OutputDevice.Terminal;

internal sealed class TestDetailState
{
private string _text;

public TestDetailState(long id, IStopwatch? stopwatch, string text)
{
Id = id;
Stopwatch = stopwatch;
_text = text;
}

public long Id { get; }

public long Version { get; set; }

public IStopwatch? Stopwatch { get; }

public string Text
{
get => _text;
set
{
if (!_text.Equals(value, StringComparison.Ordinal))
{
Version++;
_text = value;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Concurrent;
using System.Globalization;

using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Resources;

namespace Microsoft.Testing.Platform.OutputDevice.Terminal;

internal sealed class TestNodeResultsState
{
public TestNodeResultsState(long id)
{
Id = id;
_summaryDetail = new(id, stopwatch: null, text: string.Empty);
}

public long Id { get; }

private readonly TestDetailState _summaryDetail;
private readonly ConcurrentDictionary<string, TestDetailState> _testNodeProgressStates = new();

public int Count => _testNodeProgressStates.Count;

public void AddRunningTestNode(int id, string uid, string name, IStopwatch stopwatch) => _testNodeProgressStates[uid] = new TestDetailState(id, stopwatch, name);

public void RemoveRunningTestNode(string uid) => _testNodeProgressStates.TryRemove(uid, out _);

public TestDetailState? GetFirstRunningTask() => _testNodeProgressStates.FirstOrDefault().Value;

public IEnumerable<TestDetailState> GetRunningTasks(int maxCount)
{
var sortedDetails = _testNodeProgressStates
.Select(d => d.Value)
.OrderByDescending(d => d.Stopwatch?.Elapsed ?? TimeSpan.Zero)
.ToList();

bool tooManyItems = sortedDetails.Count > maxCount;

if (tooManyItems)
{
// Note: If there's too many items to display, the summary will take up one line.
// As such, we can only take maxCount - 1 items.
int itemsToTake = maxCount - 1;
_summaryDetail.Text =
itemsToTake == 0
// Note: If itemsToTake is 0, then we only show two lines, the project summary and the number of running tests.
? string.Format(CultureInfo.CurrentCulture, PlatformResources.ActiveTestsRunning_FullTestsCount, sortedDetails.Count)
// If itemsToTake is larger, then we show the project summary, active tests, and the number of active tests that are not shown.
: $"... {string.Format(CultureInfo.CurrentCulture, PlatformResources.ActiveTestsRunning_MoreTestsCount, sortedDetails.Count - itemsToTake)}";
sortedDetails = sortedDetails.Take(itemsToTake).ToList();
}

foreach (TestDetailState? detail in sortedDetails)
{
yield return detail;
}

if (tooManyItems)
{
yield return _summaryDetail;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace Microsoft.Testing.Platform.OutputDevice.Terminal;

internal sealed class TestProgressState
{
public TestProgressState(string assembly, string? targetFramework, string? architecture, IStopwatch stopwatch)
public TestProgressState(long id, string assembly, string? targetFramework, string? architecture, IStopwatch stopwatch)
{
Id = id;
Assembly = assembly;
TargetFramework = targetFramework;
Architecture = architecture;
Expand Down Expand Up @@ -38,11 +39,13 @@ public TestProgressState(string assembly, string? targetFramework, string? archi

public int TotalTests { get; internal set; }

public string? Detail { get; internal set; }
public TestNodeResultsState? TestNodeResultsState { get; internal set; }

public int SlotIndex { get; internal set; }

public long LastUpdate { get; internal set; }
public long Id { get; internal set; }

public long Version { get; internal set; }

public List<(string? DisplayName, string? UID)> DiscoveredTests { get; internal set; } = new();

Expand Down
Loading