-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathTUnitInitializer.cs
More file actions
76 lines (63 loc) · 2.26 KB
/
Copy pathTUnitInitializer.cs
File metadata and controls
76 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Diagnostics;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using TUnit.Core;
using TUnit.Engine.CommandLineProviders;
using TUnit.Engine.Discovery;
using TUnit.Engine.Exceptions;
namespace TUnit.Engine;
internal class TUnitInitializer(ICommandLineOptions commandLineOptions, IHookRegistrar hookDiscoveryService)
{
public void Initialize(ExecuteRequestContext context)
{
ConfigureGlobalExceptionHandlers(context);
SetUpExceptionListeners();
ParseParameters();
// Discover hooks using the mode-specific service
hookDiscoveryService.DiscoverHooks();
if (!string.IsNullOrEmpty(TestContext.OutputDirectory))
{
TestContext.WorkingDirectory = TestContext.OutputDirectory!;
}
}
private void SetUpExceptionListeners()
{
Trace.Listeners.Insert(0, new ThrowListener());
}
private void ParseParameters()
{
if (!commandLineOptions.TryGetOptionArgumentList(ParametersCommandProvider.TestParameter, out var parameters))
{
return;
}
foreach (var parameter in parameters)
{
var split = parameter.Split('=');
var key = split[0];
var value = split[1];
var list = TestContext.InternalParametersDictionary.GetOrAdd(key, static _ => []);
list.Add(value);
}
}
private static void ConfigureGlobalExceptionHandlers(ExecuteRequestContext context)
{
// Handle unhandled exceptions on any thread
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
var exception = args.ExceptionObject as Exception;
Console.Error.WriteLine($"Unhandled exception in AppDomain: {exception}");
// Force exit to prevent hanging
if (args.IsTerminating)
{
context.Complete();
}
};
// Handle unobserved task exceptions
TaskScheduler.UnobservedTaskException += (_, args) =>
{
Console.Error.WriteLine($"Unobserved task exception: {args.Exception}");
// Mark as observed to prevent process termination
args.SetObserved();
};
}
}