From e66796d3049f9234a2e81eed293a846d62410af4 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 12 Mar 2026 14:06:32 +0100 Subject: [PATCH] Simplify TerminalOutputDevice --- samples/Playground/Playground.csproj | 1 + samples/Playground/Program.cs | 2 +- .../OutputDevice/TerminalOutputDevice.cs | 38 +++---------------- 3 files changed, 8 insertions(+), 33 deletions(-) diff --git a/samples/Playground/Playground.csproj b/samples/Playground/Playground.csproj index 0a49079430..41f818e519 100644 --- a/samples/Playground/Playground.csproj +++ b/samples/Playground/Playground.csproj @@ -28,6 +28,7 @@ + diff --git a/samples/Playground/Program.cs b/samples/Playground/Program.cs index e9eae7f8ca..2c2c5fc59b 100644 --- a/samples/Playground/Program.cs +++ b/samples/Playground/Program.cs @@ -50,7 +50,7 @@ public static async Task Main(string[] args) // testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus())); // Enable Trx - // testApplicationBuilder.AddTrxReportProvider(); + testApplicationBuilder.AddTrxReportProvider(); // Enable Telemetry // testApplicationBuilder.AddAppInsightsTelemetryProvider(); diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs index 932804b208..865df3052f 100644 --- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs +++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs @@ -5,7 +5,6 @@ using Microsoft.Testing.Platform.Extensions; using Microsoft.Testing.Platform.Extensions.Messages; using Microsoft.Testing.Platform.Extensions.OutputDevice; -using Microsoft.Testing.Platform.Extensions.TestHost; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.OutputDevice.Terminal; @@ -22,7 +21,6 @@ namespace Microsoft.Testing.Platform.OutputDevice; internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDevice, IDataConsumer, IOutputDeviceDataProducer, - ITestSessionLifetimeHandler, IDisposable, IAsyncInitializableExtension { @@ -55,11 +53,11 @@ internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDev private readonly string _assemblyName; private TerminalTestReporter? _terminalTestReporter; - private bool _firstCallTo_OnSessionStartingAsync = true; private bool _bannerDisplayed; private bool _isListTests; private bool _isServerMode; private ILogger? _logger; + private TestProcessRole? _processRole; public TerminalOutputDevice( IConsole console, @@ -325,7 +323,7 @@ private async Task DisplayAfterSessionEndRunInternalAsync() using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout).ConfigureAwait(false)) { - if (!_firstCallTo_OnSessionStartingAsync) + if (_processRole == TestProcessRole.TestHost) { _terminalTestReporter.AssemblyRunCompleted(); _terminalTestReporter.TestExecutionCompleted(_clock.UtcNow); @@ -333,28 +331,6 @@ private async Task DisplayAfterSessionEndRunInternalAsync() } } - public Task OnTestSessionFinishingAsync(ITestSessionContext testSessionContext) => Task.CompletedTask; - - public Task OnTestSessionStartingAsync(ITestSessionContext testSessionContext) - { - CancellationToken cancellationToken = testSessionContext.CancellationToken; - cancellationToken.ThrowIfCancellationRequested(); - if (_isServerMode) - { - return Task.CompletedTask; - } - - // We implement IDataConsumerService and IOutputDisplayService. - // So the engine is calling us before as IDataConsumerService and after as IOutputDisplayService. - // The engine look for the ITestSessionLifetimeHandler in both case and call it. - if (_firstCallTo_OnSessionStartingAsync) - { - _firstCallTo_OnSessionStartingAsync = false; - } - - return Task.CompletedTask; - } - /// /// Displays provided data through IConsole, which is typically System.Console. /// @@ -416,9 +392,8 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo foreach (FileArtifactProperty artifact in testNodeStateChanged.TestNode.Properties.OfType()) { - bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync; _terminalTestReporter.ArtifactAdded( - isOutOfProcessArtifact, + outOfProcess: _processRole != TestProcessRole.TestHost, testNodeStateChanged.TestNode.DisplayName, artifact.FileInfo.FullName); } @@ -532,9 +507,8 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo case SessionFileArtifact artifact: { - bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync; _terminalTestReporter.ArtifactAdded( - isOutOfProcessArtifact, + outOfProcess: _processRole != TestProcessRole.TestHost, testName: null, artifact.FileInfo.FullName); } @@ -542,9 +516,8 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo break; case FileArtifact artifact: { - bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync; _terminalTestReporter.ArtifactAdded( - isOutOfProcessArtifact, + outOfProcess: _processRole != TestProcessRole.TestHost, testName: null, artifact.FileInfo.FullName); } @@ -560,6 +533,7 @@ public void Dispose() public async Task HandleProcessRoleAsync(TestProcessRole processRole, CancellationToken cancellationToken) { + _processRole = processRole; if (processRole == TestProcessRole.TestHost) { await _policiesService.RegisterOnMaxFailedTestsCallbackAsync(