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
22 changes: 18 additions & 4 deletions src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ public static async Task<ITestApplicationBuilder> CreateBuilderAsync(string[] ar
string createBuilderEntryTime = createBuilderStart.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);
testApplicationOptions ??= new TestApplicationOptions();

LaunchAttachDebugger(systemEnvironment);
SystemConsole systemConsole = new();
SystemProcessHandler systemProcess = new();
AttachDebuggerIfNeeded(systemEnvironment, systemConsole, systemProcess);

// First step is to parse the command line from where we get the second input layer.
// The first one should be the env vars handled autonomously by extensions and part of the test platform.
CommandLineParseResult parseResult = CommandLineParser.Parse(args, systemEnvironment);
TestHostControllerInfo testHostControllerInfo = new(parseResult);
SystemProcessHandler systemProcess = new();
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(systemEnvironment, systemProcess);

// Create the UnhandledExceptionHandler that will be set inside the TestHostBuilder.
LazyInitializer.EnsureInitialized(ref s_unhandledExceptionHandler, () => new UnhandledExceptionHandler(systemEnvironment, new SystemConsole(), parseResult.IsOptionSet(PlatformCommandLineProvider.TestHostControllerPIDOptionKey)));
LazyInitializer.EnsureInitialized(ref s_unhandledExceptionHandler, () => new UnhandledExceptionHandler(systemEnvironment, systemConsole, parseResult.IsOptionSet(PlatformCommandLineProvider.TestHostControllerPIDOptionKey)));
ApplicationStateGuard.Ensure(s_unhandledExceptionHandler is not null);

// First task is to setup the logger if enabled and we take the info from the command line or env vars.
Expand Down Expand Up @@ -234,12 +235,25 @@ public ValueTask DisposeAsync()
public async Task<int> RunAsync()
=> await _testHost.RunAsync();

private static void LaunchAttachDebugger(SystemEnvironment environment)
private static void AttachDebuggerIfNeeded(SystemEnvironment environment, SystemConsole console, SystemProcessHandler systemProcess)
{
if (environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER) == "1")
{
Debugger.Launch();
}

if (environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER) == "1")
{
IProcess currentProcess = systemProcess.GetCurrentProcess();
console.WriteLine($"Waiting for debugger to attach... Process Id: {currentProcess.Id}, Name: {currentProcess.Name}");

while (!Debugger.IsAttached)
{
Thread.Sleep(1000);
}

Debugger.Break();
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal static class EnvironmentVariableConstants

// Debugging
public const string TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER = nameof(TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER);
public const string TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER = nameof(TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER);

// dotnet test
public const string TESTINGPLATFORM_DOTNETTEST_EXECUTIONID = nameof(TESTINGPLATFORM_DOTNETTEST_EXECUTIONID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ namespace Microsoft.Testing.Platform.Helpers;

internal interface IProcess : IDisposable
{
public event EventHandler Exited;
event EventHandler Exited;

/// <inheritdoc cref="System.Diagnostics.Process.Id" />
int Id { get; }

/// <inheritdoc cref="System.Diagnostics.Process.ProcessName" />
string Name { get; }

/// <inheritdoc cref="System.Diagnostics.Process.ExitCode" />
int ExitCode { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public SystemProcess(Process process)

public int Id => _process.Id;

public string Name => _process.ProcessName;

public int ExitCode => _process.ExitCode;

#if NETCOREAPP
Expand Down