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 @@ -181,8 +181,7 @@ await _logger.LogDebugAsync($"""
""").ConfigureAwait(false);
}

if (_commandLineOptionsService.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName)
&& !OperatingSystem.IsBrowser())
if (TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptionsService))
{
ApplicationStateGuard.Ensure(_trxTestApplicationLifecycleCallbacks is not null);
ApplicationStateGuard.Ensure(_trxTestApplicationLifecycleCallbacks.NamedPipeClient is not null);
Expand Down Expand Up @@ -236,11 +235,11 @@ public async Task OnTestSessionFinishingAsync(ITestSessionContext testSessionCon
}

// If crash dump is not enabled we run trx in-process only
if (!_commandLineOptionsService.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName))
if (!TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptionsService))
{
await _messageBus.PublishAsync(this, new SessionFileArtifact(testSessionContext.SessionUid, new FileInfo(reportFileName), ExtensionResources.TrxReportArtifactDisplayName, ExtensionResources.TrxReportArtifactDescription)).ConfigureAwait(false);
}
else if (!OperatingSystem.IsBrowser())
else
{
ApplicationStateGuard.Ensure(_trxTestApplicationLifecycleCallbacks is not null);
ApplicationStateGuard.Ensure(_trxTestApplicationLifecycleCallbacks.NamedPipeClient is not null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Task<bool> IsEnabledAsync()
// TrxReportGenerator is enabled only when trx report is enabled
_commandLineOptions.IsOptionSet(TrxReportGeneratorCommandLine.TrxReportOptionName)
// If crash dump is not enabled we run trx in-process only
&& _commandLineOptions.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName));
&& TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptions));
#pragma warning restore SA1114 // Parameter list should follow declaration

public Task UpdateAsync(IEnvironmentVariables environmentVariables)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.CommandLine;

namespace Microsoft.Testing.Extensions.TrxReport;

internal static class TrxModeHelpers
{
[UnsupportedOSPlatformGuard("BROWSER")]
public static bool ShouldUseOutOfProcessTrxGeneration(ICommandLineOptions commandLineOptions)
=> commandLineOptions.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName) &&
!RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace Microsoft.Testing.Extensions.TrxReport.Abstractions;

[UnsupportedOSPlatform("browser")]
internal sealed class TrxProcessLifetimeHandler :
ITestHostProcessLifetimeHandler,
IDataConsumer,
Expand Down Expand Up @@ -94,23 +93,36 @@ public Task<bool> IsEnabledAsync()
// TrxReportGenerator is enabled only when trx report is enabled
_commandLineOptions.IsOptionSet(TrxReportGeneratorCommandLine.TrxReportOptionName)
// If crash dump is not enabled we run trx in-process only
&& _commandLineOptions.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName));
&& TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptions));
#pragma warning restore SA1114 // Parameter list should follow declaration

public Task BeforeTestHostProcessStartAsync(CancellationToken cancellation)
{
_waitConnectionTask = _task.Run(
// IsEnabledAsync will only return true if we are out of process.
// If we are not out of process, then we are disabled. Hence, this won't be called.
// The extra check is to let the platform compatibility analyzer know that we are not running in browser.
if (!TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptions))
{
throw ApplicationStateGuard.Unreachable();
}

// Note: Inlining this method produces a false positive warning for platform compatibility.
BeforeTestHostProcessStartCore(cancellation);

return Task.CompletedTask;
}

[UnsupportedOSPlatform("BROWSER")]
private void BeforeTestHostProcessStartCore(CancellationToken cancellationToken)
=> _waitConnectionTask = _task.Run(
async () =>
{
_singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellation);
_singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellationToken);
_singleConnectionNamedPipeServer.RegisterSerializer(new ReportFileNameRequestSerializer(), typeof(ReportFileNameRequest));
_singleConnectionNamedPipeServer.RegisterSerializer(new TestAdapterInformationRequestSerializer(), typeof(TestAdapterInformationRequest));
_singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellation).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellation).ConfigureAwait(false);
}, cancellation);

return Task.CompletedTask;
}
await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellationToken).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken).ConfigureAwait(false);
}, cancellationToken);

public async Task OnTestHostProcessStartedAsync(ITestHostProcessInformation testHostProcessInformation, CancellationToken cancellation)
{
Expand Down Expand Up @@ -233,7 +245,12 @@ public async ValueTask DisposeAsync()
#endif

public void Dispose()
=> _singleConnectionNamedPipeServer?.Dispose();
{
if (TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(_commandLineOptions))
{
_singleConnectionNamedPipeServer?.Dispose();
}
}

private sealed class ExtensionInfo : IExtension
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ public static void AddTrxReportProvider(this ITestApplicationBuilder builder)
serviceProvider.GetTestFramework(),
serviceProvider.GetTestFrameworkCapabilities(),
serviceProvider.GetTestApplicationProcessExitCode(),
OperatingSystem.IsBrowser() ? null : serviceProvider.GetService<TrxTestApplicationLifecycleCallbacks>(),
serviceProvider.GetService<TrxTestApplicationLifecycleCallbacks>(),
serviceProvider.GetLoggerFactory().CreateLogger<TrxReportGenerator>()));

#if NETCOREAPP
if (!OperatingSystem.IsBrowser())
#else
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
#endif
{
NonBrowserRegistrations(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

namespace Microsoft.Testing.Extensions.TrxReport.Abstractions;

[UnsupportedOSPlatform("browser")]
internal sealed class TrxTestApplicationLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable
{
[UnsupportedOSPlatformGuard("BROWSER")]
private readonly bool _isEnabled;

private readonly IEnvironment _environment;

public TrxTestApplicationLifecycleCallbacks(
Expand All @@ -26,7 +27,7 @@ public TrxTestApplicationLifecycleCallbacks(
// TrxReportGenerator is enabled only when trx report is enabled
commandLineOptionsService.IsOptionSet(TrxReportGeneratorCommandLine.TrxReportOptionName) &&
// If crash dump is not enabled we run trx in-process only
commandLineOptionsService.IsOptionSet(CrashDumpCommandLineOptions.CrashDumpOptionName);
TrxModeHelpers.ShouldUseOutOfProcessTrxGeneration(commandLineOptionsService);

_environment = environment;
}
Expand Down Expand Up @@ -77,5 +78,11 @@ public async Task BeforeRunAsync(CancellationToken cancellationToken)
}
}

public void Dispose() => NamedPipeClient?.Dispose();
public void Dispose()
{
if (_isEnabled)
{
NamedPipeClient?.Dispose();
}
}
}
Loading