Skip to content

Commit c0dda3f

Browse files
authored
Add ability to wait for debugger attach (#4829)
1 parent ee39352 commit c0dda3f

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ public static async Task<ITestApplicationBuilder> CreateBuilderAsync(string[] ar
7373
string createBuilderEntryTime = createBuilderStart.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);
7474
testApplicationOptions ??= new TestApplicationOptions();
7575

76-
LaunchAttachDebugger(systemEnvironment);
76+
SystemConsole systemConsole = new();
77+
SystemProcessHandler systemProcess = new();
78+
AttachDebuggerIfNeeded(systemEnvironment, systemConsole, systemProcess);
7779

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

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

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

237-
private static void LaunchAttachDebugger(SystemEnvironment environment)
238+
private static void AttachDebuggerIfNeeded(SystemEnvironment environment, SystemConsole console, SystemProcessHandler systemProcess)
238239
{
239240
if (environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER) == "1")
240241
{
241242
Debugger.Launch();
242243
}
244+
245+
if (environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER) == "1")
246+
{
247+
IProcess currentProcess = systemProcess.GetCurrentProcess();
248+
console.WriteLine($"Waiting for debugger to attach... Process Id: {currentProcess.Id}, Name: {currentProcess.Name}");
249+
250+
while (!Debugger.IsAttached)
251+
{
252+
Thread.Sleep(1000);
253+
}
254+
255+
Debugger.Break();
256+
}
243257
}
244258

245259
/*

src/Platform/Microsoft.Testing.Platform/Helpers/EnvironmentVariableConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal static class EnvironmentVariableConstants
3232

3333
// Debugging
3434
public const string TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER = nameof(TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER);
35+
public const string TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER = nameof(TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER);
3536

3637
// dotnet test
3738
public const string TESTINGPLATFORM_DOTNETTEST_EXECUTIONID = nameof(TESTINGPLATFORM_DOTNETTEST_EXECUTIONID);

src/Platform/Microsoft.Testing.Platform/Helpers/System/IProcess.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ namespace Microsoft.Testing.Platform.Helpers;
55

66
internal interface IProcess : IDisposable
77
{
8-
public event EventHandler Exited;
8+
event EventHandler Exited;
99

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

13+
/// <inheritdoc cref="System.Diagnostics.Process.ProcessName" />
14+
string Name { get; }
15+
1316
/// <inheritdoc cref="System.Diagnostics.Process.ExitCode" />
1417
int ExitCode { get; }
1518

src/Platform/Microsoft.Testing.Platform/Helpers/System/SystemProcess.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public SystemProcess(Process process)
1919

2020
public int Id => _process.Id;
2121

22+
public string Name => _process.ProcessName;
23+
2224
public int ExitCode => _process.ExitCode;
2325

2426
#if NETCOREAPP

0 commit comments

Comments
 (0)