Skip to content

Commit 82611a6

Browse files
authored
Simplify TerminalOutputDevice (#7533)
1 parent 1e46115 commit 82611a6

3 files changed

Lines changed: 8 additions & 33 deletions

File tree

samples/Playground/Playground.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="$(RepoRoot)src\Analyzers\MSTest.Analyzers\MSTest.Analyzers.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
2929
<PackageReference Include="StreamJsonRpc" />
3030
<ProjectReference Include="$(RepoRoot)src\Platform\Microsoft.Testing.Extensions.Telemetry\Microsoft.Testing.Extensions.Telemetry.csproj" />
31+
<ProjectReference Include="$(RepoRoot)src\Platform\Microsoft.Testing.Extensions.TrxReport\Microsoft.Testing.Extensions.TrxReport.csproj" />
3132
<ProjectReference Include="$(RepoRoot)src\Platform\Microsoft.Testing.Extensions.CrashDump\Microsoft.Testing.Extensions.CrashDump.csproj" />
3233
<ProjectReference Include="$(RepoRoot)src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\Microsoft.Testing.Extensions.OpenTelemetry.csproj" />
3334
</ItemGroup>

samples/Playground/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static async Task<int> Main(string[] args)
5050
// testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(s => new OutOfProc(s.GetMessageBus()));
5151

5252
// Enable Trx
53-
// testApplicationBuilder.AddTrxReportProvider();
53+
testApplicationBuilder.AddTrxReportProvider();
5454

5555
// Enable Telemetry
5656
// testApplicationBuilder.AddAppInsightsTelemetryProvider();

src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Testing.Platform.Extensions;
66
using Microsoft.Testing.Platform.Extensions.Messages;
77
using Microsoft.Testing.Platform.Extensions.OutputDevice;
8-
using Microsoft.Testing.Platform.Extensions.TestHost;
98
using Microsoft.Testing.Platform.Helpers;
109
using Microsoft.Testing.Platform.Logging;
1110
using Microsoft.Testing.Platform.OutputDevice.Terminal;
@@ -22,7 +21,6 @@ namespace Microsoft.Testing.Platform.OutputDevice;
2221
internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDevice,
2322
IDataConsumer,
2423
IOutputDeviceDataProducer,
25-
ITestSessionLifetimeHandler,
2624
IDisposable,
2725
IAsyncInitializableExtension
2826
{
@@ -55,11 +53,11 @@ internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDev
5553
private readonly string _assemblyName;
5654

5755
private TerminalTestReporter? _terminalTestReporter;
58-
private bool _firstCallTo_OnSessionStartingAsync = true;
5956
private bool _bannerDisplayed;
6057
private bool _isListTests;
6158
private bool _isServerMode;
6259
private ILogger? _logger;
60+
private TestProcessRole? _processRole;
6361

6462
public TerminalOutputDevice(
6563
IConsole console,
@@ -325,36 +323,14 @@ private async Task DisplayAfterSessionEndRunInternalAsync()
325323

326324
using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout).ConfigureAwait(false))
327325
{
328-
if (!_firstCallTo_OnSessionStartingAsync)
326+
if (_processRole == TestProcessRole.TestHost)
329327
{
330328
_terminalTestReporter.AssemblyRunCompleted();
331329
_terminalTestReporter.TestExecutionCompleted(_clock.UtcNow);
332330
}
333331
}
334332
}
335333

336-
public Task OnTestSessionFinishingAsync(ITestSessionContext testSessionContext) => Task.CompletedTask;
337-
338-
public Task OnTestSessionStartingAsync(ITestSessionContext testSessionContext)
339-
{
340-
CancellationToken cancellationToken = testSessionContext.CancellationToken;
341-
cancellationToken.ThrowIfCancellationRequested();
342-
if (_isServerMode)
343-
{
344-
return Task.CompletedTask;
345-
}
346-
347-
// We implement IDataConsumerService and IOutputDisplayService.
348-
// So the engine is calling us before as IDataConsumerService and after as IOutputDisplayService.
349-
// The engine look for the ITestSessionLifetimeHandler in both case and call it.
350-
if (_firstCallTo_OnSessionStartingAsync)
351-
{
352-
_firstCallTo_OnSessionStartingAsync = false;
353-
}
354-
355-
return Task.CompletedTask;
356-
}
357-
358334
/// <summary>
359335
/// Displays provided data through IConsole, which is typically System.Console.
360336
/// </summary>
@@ -416,9 +392,8 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo
416392

417393
foreach (FileArtifactProperty artifact in testNodeStateChanged.TestNode.Properties.OfType<FileArtifactProperty>())
418394
{
419-
bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync;
420395
_terminalTestReporter.ArtifactAdded(
421-
isOutOfProcessArtifact,
396+
outOfProcess: _processRole != TestProcessRole.TestHost,
422397
testNodeStateChanged.TestNode.DisplayName,
423398
artifact.FileInfo.FullName);
424399
}
@@ -532,19 +507,17 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo
532507

533508
case SessionFileArtifact artifact:
534509
{
535-
bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync;
536510
_terminalTestReporter.ArtifactAdded(
537-
isOutOfProcessArtifact,
511+
outOfProcess: _processRole != TestProcessRole.TestHost,
538512
testName: null,
539513
artifact.FileInfo.FullName);
540514
}
541515

542516
break;
543517
case FileArtifact artifact:
544518
{
545-
bool isOutOfProcessArtifact = _firstCallTo_OnSessionStartingAsync;
546519
_terminalTestReporter.ArtifactAdded(
547-
isOutOfProcessArtifact,
520+
outOfProcess: _processRole != TestProcessRole.TestHost,
548521
testName: null,
549522
artifact.FileInfo.FullName);
550523
}
@@ -560,6 +533,7 @@ public void Dispose()
560533

561534
public async Task HandleProcessRoleAsync(TestProcessRole processRole, CancellationToken cancellationToken)
562535
{
536+
_processRole = processRole;
563537
if (processRole == TestProcessRole.TestHost)
564538
{
565539
await _policiesService.RegisterOnMaxFailedTestsCallbackAsync(

0 commit comments

Comments
 (0)