forked from thomhurst/TUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookExecutor.cs
More file actions
389 lines (335 loc) · 13.5 KB
/
HookExecutor.cs
File metadata and controls
389 lines (335 loc) · 13.5 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.ExceptionServices;
using TUnit.Core;
using TUnit.Core.Exceptions;
using TUnit.Core.Services;
using TUnit.Engine.Interfaces;
namespace TUnit.Engine.Services;
/// <summary>
/// Responsible for executing hooks and event receivers with proper context hierarchy.
/// Merges the functionality of hooks and first/last event receivers for unified lifecycle management.
/// Follows Single Responsibility Principle - only handles hook and event receiver execution.
/// </summary>
internal sealed class HookExecutor
{
private readonly IHookCollectionService _hookCollectionService;
private readonly IContextProvider _contextProvider;
private readonly EventReceiverOrchestrator _eventReceiverOrchestrator;
public HookExecutor(
IHookCollectionService hookCollectionService,
IContextProvider contextProvider,
EventReceiverOrchestrator eventReceiverOrchestrator)
{
_hookCollectionService = hookCollectionService;
_contextProvider = contextProvider;
_eventReceiverOrchestrator = eventReceiverOrchestrator;
}
public async ValueTask ExecuteBeforeTestSessionHooksAsync(CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectBeforeTestSessionHooksAsync().ConfigureAwait(false);
if (hooks.Count == 0)
{
return;
}
foreach (var hook in hooks)
{
try
{
_contextProvider.TestSessionContext.RestoreExecutionContext();
await hook(_contextProvider.TestSessionContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}
if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}
throw new BeforeTestSessionException($"BeforeTestSession hook failed: {ex.Message}", ex);
}
}
}
public async ValueTask<List<Exception>> ExecuteAfterTestSessionHooksAsync(CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectAfterTestSessionHooksAsync().ConfigureAwait(false);
if (hooks.Count == 0)
{
return [];
}
// Defer exception list allocation until actually needed
List<Exception>? exceptions = null;
foreach (var hook in hooks)
{
try
{
_contextProvider.TestSessionContext.RestoreExecutionContext();
await hook(_contextProvider.TestSessionContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
// Collect hook exceptions instead of throwing immediately
// This allows all hooks to run even if some fail
exceptions ??= [];
exceptions.Add(new AfterTestSessionException($"AfterTestSession hook failed: {ex.Message}", ex));
}
}
return exceptions ?? [];
}
public async ValueTask ExecuteBeforeAssemblyHooksAsync(Assembly assembly, CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectBeforeAssemblyHooksAsync(assembly).ConfigureAwait(false);
if (hooks.Count == 0)
{
return;
}
foreach (var hook in hooks)
{
try
{
var context = _contextProvider.GetOrCreateAssemblyContext(assembly);
context.RestoreExecutionContext();
await hook(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}
if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}
throw new BeforeAssemblyException($"BeforeAssembly hook failed: {ex.Message}", ex);
}
}
}
public async ValueTask<List<Exception>> ExecuteAfterAssemblyHooksAsync(Assembly assembly, CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectAfterAssemblyHooksAsync(assembly).ConfigureAwait(false);
if (hooks.Count == 0)
{
return [];
}
// Defer exception list allocation until actually needed
List<Exception>? exceptions = null;
foreach (var hook in hooks)
{
try
{
var context = _contextProvider.GetOrCreateAssemblyContext(assembly);
context.RestoreExecutionContext();
await hook(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
// Collect hook exceptions instead of throwing immediately
// This allows all hooks to run even if some fail
exceptions ??= [];
exceptions.Add(new AfterAssemblyException($"AfterAssembly hook failed: {ex.Message}", ex));
}
}
return exceptions ?? [];
}
public async ValueTask ExecuteBeforeClassHooksAsync(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
Type testClass, CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectBeforeClassHooksAsync(testClass).ConfigureAwait(false);
if (hooks.Count == 0)
{
return;
}
foreach (var hook in hooks)
{
try
{
var context = _contextProvider.GetOrCreateClassContext(testClass);
context.RestoreExecutionContext();
await hook(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}
if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}
throw new BeforeClassException($"BeforeClass hook failed: {ex.Message}", ex);
}
}
}
public async ValueTask<List<Exception>> ExecuteAfterClassHooksAsync(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
Type testClass, CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectAfterClassHooksAsync(testClass).ConfigureAwait(false);
if (hooks.Count == 0)
{
return [];
}
// Defer exception list allocation until actually needed
List<Exception>? exceptions = null;
foreach (var hook in hooks)
{
try
{
var context = _contextProvider.GetOrCreateClassContext(testClass);
context.RestoreExecutionContext();
await hook(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
// Collect hook exceptions instead of throwing immediately
// This allows all hooks to run even if some fail
exceptions ??= [];
exceptions.Add(new AfterClassException($"AfterClass hook failed: {ex.Message}", ex));
}
}
return exceptions ?? [];
}
public async ValueTask ExecuteBeforeTestHooksAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
{
var testClassType = test.Metadata.TestClassType;
// Execute BeforeEvery(Test) hooks first (global test hooks run before specific hooks)
var beforeEveryTestHooks = await _hookCollectionService.CollectBeforeEveryTestHooksAsync(testClassType).ConfigureAwait(false);
if (beforeEveryTestHooks.Count > 0)
{
foreach (var hook in beforeEveryTestHooks)
{
try
{
test.Context.RestoreExecutionContext();
await hook(test.Context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}
if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}
throw new BeforeTestException($"BeforeEveryTest hook failed: {ex.Message}", ex);
}
}
}
// Execute Before(Test) hooks after BeforeEvery hooks
var beforeTestHooks = await _hookCollectionService.CollectBeforeTestHooksAsync(testClassType).ConfigureAwait(false);
if (beforeTestHooks.Count > 0)
{
foreach (var hook in beforeTestHooks)
{
try
{
test.Context.RestoreExecutionContext();
await hook(test.Context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}
if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}
throw new BeforeTestException($"BeforeTest hook failed: {ex.Message}", ex);
}
}
}
}
public async ValueTask<IReadOnlyList<Exception>> ExecuteAfterTestHooksAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
{
// Defer exception list allocation until actually needed
List<Exception>? exceptions = null;
var testClassType = test.Metadata.TestClassType;
// Execute After(Test) hooks first (specific hooks run before global hooks for cleanup)
var afterTestHooks = await _hookCollectionService.CollectAfterTestHooksAsync(testClassType).ConfigureAwait(false);
if (afterTestHooks.Count > 0)
{
foreach (var hook in afterTestHooks)
{
try
{
test.Context.RestoreExecutionContext();
await hook(test.Context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
exceptions ??= [];
exceptions.Add(new AfterTestException($"After(Test) hook failed: {ex.Message}", ex));
}
}
}
// Execute AfterEvery(Test) hooks after After hooks (global test hooks run last for cleanup)
var afterEveryTestHooks = await _hookCollectionService.CollectAfterEveryTestHooksAsync(testClassType).ConfigureAwait(false);
if (afterEveryTestHooks.Count > 0)
{
foreach (var hook in afterEveryTestHooks)
{
try
{
test.Context.RestoreExecutionContext();
await hook(test.Context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
exceptions ??= [];
exceptions.Add(new AfterTestException($"AfterEvery(Test) hook failed: {ex.Message}", ex));
}
}
}
return exceptions == null ? Array.Empty<Exception>() : exceptions;
}
public async ValueTask ExecuteBeforeTestDiscoveryHooksAsync(CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectBeforeTestDiscoveryHooksAsync().ConfigureAwait(false);
if (hooks.Count == 0)
{
return;
}
foreach (var hook in hooks)
{
try
{
_contextProvider.BeforeTestDiscoveryContext.RestoreExecutionContext();
await hook(_contextProvider.BeforeTestDiscoveryContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new BeforeTestDiscoveryException($"BeforeTestDiscovery hook failed: {ex.Message}", ex);
}
}
}
public async ValueTask ExecuteAfterTestDiscoveryHooksAsync(CancellationToken cancellationToken)
{
var hooks = await _hookCollectionService.CollectAfterTestDiscoveryHooksAsync().ConfigureAwait(false);
if (hooks.Count == 0)
{
return;
}
foreach (var hook in hooks)
{
try
{
_contextProvider.TestDiscoveryContext.RestoreExecutionContext();
await hook(_contextProvider.TestDiscoveryContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new AfterTestDiscoveryException($"AfterTestDiscovery hook failed: {ex.Message}", ex);
}
}
}
}