-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathTestBuilder.cs
More file actions
1952 lines (1701 loc) · 90.1 KB
/
Copy pathTestBuilder.cs
File metadata and controls
1952 lines (1701 loc) · 90.1 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Requests;
using TUnit.Core;
using TUnit.Core.Enums;
using TUnit.Core.Exceptions;
using TUnit.Core.Helpers;
using TUnit.Core.Interfaces;
using TUnit.Core.Services;
using TUnit.Engine.Building.Interfaces;
using TUnit.Engine.Extensions;
using TUnit.Engine.Helpers;
using TUnit.Engine.Services;
using TUnit.Engine.Utilities;
namespace TUnit.Engine.Building;
internal sealed class TestBuilder : ITestBuilder
{
private readonly string _sessionId;
private readonly EventReceiverOrchestrator _eventReceiverOrchestrator;
private readonly IContextProvider _contextProvider;
private readonly ObjectLifecycleService _objectLifecycleService;
private readonly Discovery.IHookRegistrar _hookDiscoveryService;
private readonly IMetadataFilterMatcher _filterMatcher;
public TestBuilder(
string sessionId,
EventReceiverOrchestrator eventReceiverOrchestrator,
IContextProvider contextProvider,
ObjectLifecycleService objectLifecycleService,
Discovery.IHookRegistrar hookDiscoveryService,
IMetadataFilterMatcher filterMatcher)
{
_sessionId = sessionId;
_hookDiscoveryService = hookDiscoveryService;
_eventReceiverOrchestrator = eventReceiverOrchestrator;
_contextProvider = contextProvider;
_objectLifecycleService = objectLifecycleService;
_filterMatcher = filterMatcher ?? throw new ArgumentNullException(nameof(filterMatcher));
}
/// <summary>
/// Initializes class data objects during test building.
/// Only IAsyncDiscoveryInitializer objects are initialized during discovery.
/// Regular IAsyncInitializer objects are deferred to execution phase.
/// </summary>
private static async Task InitializeClassDataAsync(object?[] classData)
{
if (classData == null || classData.Length == 0)
{
return;
}
foreach (var data in classData)
{
// Discovery: only IAsyncDiscoveryInitializer objects are initialized.
// Regular IAsyncInitializer objects are deferred to execution phase.
await ObjectInitializer.InitializeForDiscoveryAsync(data);
}
}
private async Task<object> CreateInstance(TestMetadata metadata, Type[] resolvedClassGenericArgs, object?[] classData, TestBuilderContext builderContext)
{
foreach (var data in classData)
{
await _objectLifecycleService.InitializeObjectForExecutionAsync(data);
}
// First try to create instance with ClassConstructor attribute
// Use attributes from context if available
var attributes = builderContext.InitializedAttributes ?? metadata.GetOrCreateAttributes();
var instance = await ClassConstructorHelper.TryCreateInstanceWithClassConstructor(
attributes,
metadata.TestClassType,
builderContext,
metadata.TestSessionId);
if (instance != null)
{
return instance;
}
// Fall back to InstanceFactory if no ClassConstructor or it returned null
if (metadata.InstanceFactory == null)
{
throw new InvalidOperationException($"No instance factory or class constructor available for {metadata.TestClassType.FullName}.");
}
try
{
instance = metadata.InstanceFactory(resolvedClassGenericArgs, classData);
if (instance is null)
{
throw new InvalidOperationException($"Error creating test class instance for {metadata.TestClassType.FullName}.");
}
return instance;
}
catch (NotSupportedException ex)
{
// This can happen when:
// 1. ClassConstructor is present but wasn't found or returned null
// 2. AOT scenarios where generic type instantiation fails
if (ex.Message.Contains("missing native code or metadata"))
{
throw new InvalidOperationException($"Failed to create instance of {metadata.TestClassType.FullName} in AOT scenario. Ensure types are properly annotated for AOT compatibility.", ex);
}
throw new InvalidOperationException($"Failed to create instance of {metadata.TestClassType.FullName}. This may be due to a ClassConstructor attribute issue or AOT incompatibility.", ex);
}
}
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("Test building in reflection mode uses generic type resolution which requires unreferenced code")]
#endif
public async Task<IEnumerable<AbstractExecutableTest>> BuildTestsFromMetadataAsync(TestMetadata metadata, TestBuildingContext buildingContext, CancellationToken cancellationToken = default)
{
// OPTIMIZATION: Pre-filter in execution mode to skip building tests that cannot match the filter
if (buildingContext.IsForExecution && buildingContext.Filter != null)
{
if (!CouldTestMatchFilter(buildingContext.Filter, metadata))
{
// This test class cannot match the filter - skip all expensive work!
return [];
}
}
var tests = new List<AbstractExecutableTest>();
try
{
// Create a context for capturing output during test building
using var buildContext = new TestBuildContext();
TestBuildContext.Current = buildContext;
// Handle GenericTestMetadata with ConcreteInstantiations
if (metadata is GenericTestMetadata { ConcreteInstantiations.Count: > 0 } genericMetadata)
{
// Build tests from each concrete instantiation
foreach (var concreteMetadata in genericMetadata.ConcreteInstantiations.Values)
{
var concreteTests = await BuildTestsFromMetadataAsync(concreteMetadata, buildingContext, cancellationToken);
tests.AddRange(concreteTests);
}
return tests;
}
// Use pre-extracted repeat count from metadata (avoids instantiating attributes)
var repeatCount = metadata.RepeatCount ?? 0;
// Create and initialize attributes ONCE
var attributes = await InitializeAttributesAsync(metadata.GetOrCreateAttributes(), cancellationToken);
if (HasInstanceDataAccessor(metadata.ClassDataSources))
{
var failedTest = CreateFailedTestForClassDataSourceCircularDependency(metadata);
tests.Add(failedTest);
return tests;
}
// Create a single context accessor that we'll reuse, updating its Current property for each test
// StateBag and Events are lazy-initialized, so we don't need to pre-allocate them
var testBuilderContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
InitializedAttributes = attributes // Store the initialized attributes
};
// Set the static AsyncLocal immediately so it's available for property data sources
// This must be set BEFORE any operations that might invoke data source methods
TestBuilderContext.Current = testBuilderContext;
// Check for ClassConstructor attribute and set it early if present (reuse already created attributes)
var classConstructorAttribute = attributes.FirstOfType<ClassConstructorAttribute>();
if (classConstructorAttribute != null)
{
testBuilderContext.ClassConstructor = (IClassConstructor)Activator.CreateInstance(classConstructorAttribute.ClassConstructorType)!;
}
var contextAccessor = new TestBuilderContextAccessor(testBuilderContext);
// DeferEnumeration: emit a single placeholder node instead of enumerating the data source(s).
// The placeholder is built during both discovery and the execution build (so it matches the
// IDE's UID filter); DeferredTestExpander later re-runs this method with IgnoreDeferral set to
// produce the real cases. Skipped entirely when IgnoreDeferral is set (the expansion pass).
if (!buildingContext.IgnoreDeferral && HasDeferredDataSource(metadata))
{
tests.Add(await BuildDeferredPlaceholderAsync(metadata, testBuilderContext, cancellationToken));
return tests;
}
var classDataAttributeIndex = 0;
foreach (var classDataSource in await GetDataSourcesAsync(metadata.ClassDataSources, cancellationToken))
{
classDataAttributeIndex++;
var classDataLoopIndex = 0;
var hasAnyClassData = false;
await foreach (var classDataFactory in GetInitializedDataRowsAsync(
classDataSource,
DataGeneratorMetadataCreator.CreateDataGeneratorMetadata
(
testMetadata: metadata,
testSessionId: _sessionId,
generatorType: DataGeneratorType.ClassParameters,
testClassInstance: null, // Never pass instance for class data sources (circular dependency)
classInstanceArguments: null,
contextAccessor
),
cancellationToken))
{
hasAnyClassData = true;
classDataLoopIndex++;
var classDataResult = await classDataFactory() ?? [];
var classData = DataUnwrapper.Unwrap(classDataResult);
#if NET
// Register scope for class data objects from the simple path.
// Also registered in the repeat loop below for re-fetched instances.
TraceScopeRegistry.RegisterFromDataSource(classDataSource, classData);
#endif
// Initialize objects before method data sources are evaluated.
// ObjectInitializer is phase-aware and will only initialize IAsyncDiscoveryInitializer during Discovery.
await InitializeClassDataAsync(classData);
var needsInstanceForMethodDataSources = HasInstanceDataAccessor(metadata.DataSources);
object? instanceForMethodDataSources = null;
var discoveryInstanceUsed = false;
if (needsInstanceForMethodDataSources)
{
var instanceResult = await CreateInstanceForMethodDataSources(
metadata, classDataAttributeIndex, classDataLoopIndex, classData, testBuilderContext);
if (!instanceResult.Success)
{
var failedTest = CreateFailedTestForInstanceDataSourceError(metadata, instanceResult.Exception!);
tests.Add(failedTest);
continue;
}
instanceForMethodDataSources = instanceResult.Instance;
// Initialize property data sources on the early instance so that
// method data sources can access fully-initialized properties.
if (instanceForMethodDataSources != null)
{
var tempObjectBag = new ConcurrentDictionary<string, object?>();
var tempEvents = new TestContextEvents();
await _objectLifecycleService.RegisterObjectAsync(
instanceForMethodDataSources,
tempObjectBag,
metadata.MethodMetadata,
tempEvents,
cancellationToken);
// Discovery: only IAsyncDiscoveryInitializer is initialized
await ObjectInitializer.InitializeForDiscoveryAsync(instanceForMethodDataSources);
}
}
var methodDataAttributeIndex = 0;
foreach (var methodDataSource in await GetDataSourcesAsync(metadata.DataSources, cancellationToken))
{
methodDataAttributeIndex++;
var methodDataLoopIndex = 0;
var hasAnyMethodData = false;
await foreach (var methodDataFactory in GetInitializedDataRowsAsync(
methodDataSource,
DataGeneratorMetadataCreator.CreateDataGeneratorMetadata
(
testMetadata: metadata,
testSessionId: _sessionId,
generatorType: DataGeneratorType.TestParameters,
testClassInstance: methodDataSource is IAccessesInstanceData ? instanceForMethodDataSources : null,
classInstanceArguments: classData,
contextAccessor
),
cancellationToken))
{
hasAnyMethodData = true;
methodDataLoopIndex++;
for (var i = 0; i < repeatCount + 1; i++)
{
// Update context BEFORE calling data factories so they track objects in the right context
// StateBag and Events are lazy-initialized for performance
contextAccessor.Current = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
DataSourceAttribute = methodDataSource,
InitializedAttributes = testBuilderContext.InitializedAttributes,
ClassConstructor = testBuilderContext.ClassConstructor
};
testBuilderContext.CopyStateBagTo(contextAccessor.Current);
var (classDataUnwrapped, classRowMetadata) = DataUnwrapper.UnwrapWithMetadata(await classDataFactory() ?? []);
classData = classDataUnwrapped;
var (methodData, methodRowMetadata) = DataUnwrapper.UnwrapWithTypesAndMetadata(await methodDataFactory() ?? [], metadata.MethodMetadata.Parameters);
#if NET
// Re-register: classDataFactory() was called again above and may return
// a different instance for non-shared objects. First-registration-wins
// semantics make this a no-op for the same instance.
TraceScopeRegistry.RegisterFromDataSource(classDataSource, classData);
TraceScopeRegistry.RegisterFromDataSource(methodDataSource, methodData);
#endif
// Extract and merge metadata from data source attributes and TestDataRow wrappers
var classAttrMetadata = DataSourceMetadataExtractor.ExtractFromAttribute(classDataSource);
var methodAttrMetadata = DataSourceMetadataExtractor.ExtractFromAttribute(methodDataSource);
var mergedClassMetadata = DataSourceMetadataExtractor.Merge(classRowMetadata, classAttrMetadata);
var mergedMethodMetadata = DataSourceMetadataExtractor.Merge(methodRowMetadata, methodAttrMetadata);
var finalMetadata = DataSourceMetadataExtractor.Merge(mergedMethodMetadata, mergedClassMetadata);
// Initialize method data objects (ObjectInitializer is phase-aware)
await InitializeClassDataAsync(methodData);
// For concrete generic instantiations, check if the data is compatible with the expected types
if (metadata.GenericMethodTypeArguments is { Length: > 0 })
{
if (!IsDataCompatibleWithExpectedTypes(metadata, methodData))
{
// Skip this data source as it's not compatible with the expected types
continue;
}
}
var tempTestData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(null!), // Temporary placeholder
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = methodDataLoopIndex,
MethodData = methodData,
RepeatIndex = i,
InheritanceDepth = metadata.InheritanceDepth
};
Type[] resolvedClassGenericArgs;
Type[] resolvedMethodGenericArgs;
try
{
var resolution = TestGenericTypeResolver.Resolve(metadata, tempTestData);
resolvedClassGenericArgs = resolution.ResolvedClassGenericArguments;
resolvedMethodGenericArgs = resolution.ResolvedMethodGenericArguments;
}
catch (GenericTypeResolutionException) when (
metadata.TestClassType.IsGenericTypeDefinition &&
classData.Length == 0 &&
methodData.Length > 0)
{
// Special handling for generic classes with no constructor arguments
// but with method parameters that can help infer the generic types
try
{
resolvedClassGenericArgs = TryInferClassGenericsFromMethodData(
metadata, methodData);
resolvedMethodGenericArgs = Type.EmptyTypes; // No method generics in this case
}
catch (Exception innerEx)
{
// If we still can't resolve, create a failed test
var failedTest = CreateFailedTestForDataGenerationError(metadata, innerEx);
tests.Add(failedTest);
continue;
}
}
catch (Exception ex)
{
// If generic resolution fails, create a failed test
var failedTest = CreateFailedTestForDataGenerationError(metadata, ex);
tests.Add(failedTest);
continue;
}
if (metadata.TestClassType.IsGenericTypeDefinition && resolvedClassGenericArgs.Length == 0)
{
throw new InvalidOperationException($"Cannot create test for generic class '{metadata.TestClassType.Name}': No type arguments could be inferred. Add [GenerateGenericTest<ConcreteType>] to the class, or use a data source (like [ClassDataSource<T>] or [Arguments]) that provides constructor arguments to infer the generic type arguments from.");
}
var basicSkipReason = GetBasicSkipReason(metadata, attributes);
Func<Task<object>> instanceFactory;
var isReusingDiscoveryInstance = false;
if (basicSkipReason is { Length: > 0 })
{
instanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance);
}
else if (methodDataLoopIndex == 1 && i == 0 && instanceForMethodDataSources != null && !discoveryInstanceUsed)
{
// Reuse the discovery instance for the first test to avoid duplicate initialization
var capturedInstance = instanceForMethodDataSources;
discoveryInstanceUsed = true;
isReusingDiscoveryInstance = true;
instanceFactory = () => Task.FromResult(capturedInstance);
}
else
{
var capturedMetadata = metadata;
var capturedClassGenericArgs = resolvedClassGenericArgs;
var capturedClassData = classData;
var capturedContext = contextAccessor.Current;
instanceFactory = () => CreateInstance(capturedMetadata, capturedClassGenericArgs, capturedClassData, capturedContext);
}
var testData = new TestData
{
TestClassInstanceFactory = instanceFactory,
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = methodDataLoopIndex,
MethodData = methodData,
RepeatIndex = i,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = resolvedMethodGenericArgs,
Metadata = finalMetadata
};
// Events is lazy-initialized; explicitly share StateBag from per-iteration context
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
StateBag = contextAccessor.Current.StateBag,
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = contextAccessor.Current.DataSourceAttribute,
InitializedAttributes = attributes
};
var test = await BuildTestAsync(metadata, testData, testSpecificContext, isReusingDiscoveryInstance, cancellationToken);
// If we have a basic skip reason, set it immediately
if (!string.IsNullOrEmpty(basicSkipReason))
{
test.Context.SkipReason = basicSkipReason;
}
tests.Add(test);
// Context already updated at the beginning of the loop before calling factories
}
}
// If no data was yielded and SkipIfEmpty is true, create a skipped test
if (!hasAnyMethodData && methodDataSource.SkipIfEmpty)
{
const string skipReason = "Data source returned no data";
Type[] resolvedClassGenericArgs;
Exception? genericResolutionException = null;
try
{
resolvedClassGenericArgs = metadata.TestClassType.IsGenericTypeDefinition
? TryInferClassGenericsFromDataSources(metadata)
: Type.EmptyTypes;
}
catch (Exception ex)
{
resolvedClassGenericArgs = Type.EmptyTypes;
genericResolutionException = ex;
}
// If generic type inference failed, create a failed test instead of skipped
if (genericResolutionException != null)
{
var failedTest = CreateFailedTestForDataGenerationError(metadata, genericResolutionException);
tests.Add(failedTest);
}
else
{
var testData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance),
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = 1, // Use 1 since we're creating a single skipped test
MethodData = [],
RepeatIndex = 0,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = methodDataSource,
InitializedAttributes = attributes
};
testBuilderContext.CopyStateBagTo(testSpecificContext);
var test = await BuildTestAsync(metadata, testData, testSpecificContext, cancellationToken: cancellationToken);
test.Context.SkipReason = skipReason;
tests.Add(test);
}
}
}
}
// If no class data was yielded and SkipIfEmpty is true, create a skipped test
if (!hasAnyClassData && classDataSource.SkipIfEmpty)
{
const string skipReason = "Data source returned no data";
Type[] resolvedClassGenericArgs;
Exception? genericResolutionException = null;
try
{
resolvedClassGenericArgs = metadata.TestClassType.IsGenericTypeDefinition
? TryInferClassGenericsFromDataSources(metadata)
: Type.EmptyTypes;
}
catch (Exception ex)
{
resolvedClassGenericArgs = Type.EmptyTypes;
genericResolutionException = ex;
}
// If generic type inference failed, create a failed test instead of skipped
if (genericResolutionException != null)
{
var failedTest = CreateFailedTestForDataGenerationError(metadata, genericResolutionException);
tests.Add(failedTest);
}
else
{
var testData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance),
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = 1, // Use 1 since we're creating a single skipped test
ClassData = [],
MethodDataSourceAttributeIndex = 0,
MethodDataLoopIndex = 0,
MethodData = [],
RepeatIndex = 0,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = classDataSource,
InitializedAttributes = attributes
};
testBuilderContext.CopyStateBagTo(testSpecificContext);
var test = await BuildTestAsync(metadata, testData, testSpecificContext, cancellationToken: cancellationToken);
test.Context.SkipReason = skipReason;
tests.Add(test);
}
}
}
// Transfer captured build-time output to all test contexts
var capturedOutput = buildContext.GetCapturedOutput();
var capturedErrorOutput = buildContext.GetCapturedErrorOutput();
if (!string.IsNullOrEmpty(capturedOutput) || !string.IsNullOrEmpty(capturedErrorOutput))
{
// Also route ALL build-time output to TestSessionContext.
// This intentionally covers all sharing types (None, PerClass, PerAssembly, etc.),
// making TestSessionContext a superset of every data source's build-time output.
// The primary motivation is SharedType.PerTestSession: a single Lazy<T>
// (ExecutionAndPublication) means the factory runs exactly once in whichever
// test's buildContext wins the parallel-build race, so the output would otherwise
// be lost from every other test. Session-level routing removes that race.
// Note: the winning test batch also receives the output via SetBuildTimeOutput
// below, so its output appears in both TestContext and TestSessionContext.
if (!string.IsNullOrEmpty(capturedOutput))
TestSessionContext.Current?.OutputWriter.Write(capturedOutput);
if (!string.IsNullOrEmpty(capturedErrorOutput))
TestSessionContext.Current?.ErrorOutputWriter.Write(capturedErrorOutput);
foreach (var test in tests)
{
test.Context.SetBuildTimeOutput(capturedOutput, capturedErrorOutput);
}
}
}
catch (Exception ex)
{
var failedTest = CreateFailedTestForDataGenerationError(metadata, ex);
tests.Add(failedTest);
return tests;
}
return tests;
}
private static Type[] TryInferClassGenericsFromMethodData(TestMetadata metadata, object?[] methodData)
{
var genericClassType = metadata.TestClassType;
var genericParameters = genericClassType.GetGenericArguments();
var typeMapping = new Dictionary<Type, Type>();
// Try to match method parameter types with actual data types
var methodParameters = metadata.MethodMetadata.Parameters;
for (var i = 0; i < Math.Min(methodParameters.Length, methodData.Length); i++)
{
var methodParam = methodParameters[i];
var paramType = methodParam.Type;
var argValue = methodData[i];
if (argValue != null)
{
var argType = argValue.GetType();
// If the parameter type is object (placeholder for generic parameter in source-gen)
// and we have data, we can infer the type
if (paramType == typeof(object))
{
// Check if this corresponds to a class generic parameter
// by looking at the method metadata
if (methodParam.TypeInfo is GenericParameter { IsMethodParameter: false } gp)
{
var genericParamName = gp.Name;
// Find the matching generic parameter in the class
var matchingClassParam = genericParameters.FirstOrDefault(p => p.Name == genericParamName);
if (matchingClassParam != null)
{
typeMapping[matchingClassParam] = argType;
}
}
}
}
}
var resolvedTypes = new Type[genericParameters.Length];
for (var i = 0; i < genericParameters.Length; i++)
{
var genericParam = genericParameters[i];
if (!typeMapping.TryGetValue(genericParam, out var resolvedType))
{
throw new InvalidOperationException(
$"Could not resolve type for generic parameter '{genericParam.Name}' of type '{genericClassType.Name}' from method data");
}
resolvedTypes[i] = resolvedType;
}
return resolvedTypes;
}
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("Generic type inference uses reflection on data sources and parameters")]
#endif
private static Type[] TryInferClassGenericsFromDataSources(TestMetadata metadata)
{
var genericClassType = metadata.TestClassType;
var genericParameters = genericClassType.GetGenericArguments();
var typeMapping = new Dictionary<Type, Type>();
// First, check if we have typed data sources that can help infer the generic type
foreach (var dataSource in metadata.DataSources)
{
var dataSourceType = dataSource.GetType();
// Check if this data source inherits from a generic base type
var baseType = dataSourceType.BaseType;
while (baseType != null)
{
if (baseType.IsGenericType)
{
var genericDef = baseType.GetGenericTypeDefinition();
var genericDefName = genericDef.FullName ?? genericDef.Name;
// Check if it's a typed data source attribute
if (genericDefName.Contains("DataSourceGeneratorAttribute`") ||
genericDefName.Contains("AsyncDataSourceGeneratorAttribute`"))
{
// Get the type argument (e.g., int from AsyncDataSourceGeneratorAttribute<int>)
var typeArgs = baseType.GetGenericArguments();
if (typeArgs.Length > 0 && genericParameters.Length > 0)
{
// For now, assume the first generic parameter maps to the data source type
// This handles simple cases like GenericClass<T> with IntDataSource
typeMapping[genericParameters[0]] = typeArgs[0];
}
break;
}
}
baseType = baseType.BaseType;
}
}
if (HasInstanceDataAccessor(metadata.DataSources))
{
// Look at the test method parameters to find attributes that can help with generic type inference
foreach (var param in metadata.MethodMetadata.Parameters)
{
// Get the actual parameter type from reflection
var actualParamType = param.Type;
// Check if the actual parameter type is a generic parameter of the class
if (actualParamType.IsGenericParameter &&
genericParameters.Contains(actualParamType))
{
// Check for Matrix attributes using reflection
var attrs = param.ReflectionInfo.GetCustomAttributes(false);
foreach (var attr in attrs)
{
var attrType = attr.GetType();
// Check if it's a generic Matrix attribute
if (attrType.IsGenericType &&
attrType.GetGenericTypeDefinition().Name.StartsWith("Matrix"))
{
var matrixTypeArg = attrType.GetGenericArguments()[0];
typeMapping[actualParamType] = matrixTypeArg;
break;
}
}
}
}
}
// Look for instance method data sources
foreach (var dataSource in metadata.DataSources)
{
if (dataSource is InstanceMethodDataSourceAttribute instanceMethodDataSource)
{
// Get the method info
var methodName = instanceMethodDataSource.MethodNameProvidingDataSource;
var method = genericClassType.GetMethod(methodName,
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
if (method != null)
{
var returnType = method.ReturnType;
// Check if return type is IEnumerable<T> or similar
if (returnType.IsGenericType)
{
var genericDef = returnType.GetGenericTypeDefinition();
if (genericDef == typeof(IEnumerable<>) ||
genericDef == typeof(IAsyncEnumerable<>) ||
genericDef == typeof(List<>) ||
genericDef == typeof(Task<>))
{
var elementType = returnType.GetGenericArguments()[0];
// If Task<T>, unwrap to get the actual type
if (genericDef == typeof(Task<>) && elementType.IsGenericType)
{
var innerGenericDef = elementType.GetGenericTypeDefinition();
if (innerGenericDef == typeof(IEnumerable<>) ||
innerGenericDef == typeof(IAsyncEnumerable<>) ||
innerGenericDef == typeof(List<>))
{
elementType = elementType.GetGenericArguments()[0];
}
}
// Now try to match this element type with method parameters
// that use the class generic parameter
for (var i = 0; i < metadata.MethodMetadata.Parameters.Length; i++)
{
var paramType = metadata.MethodMetadata.Parameters[i].Type;
if (paramType == typeof(object)) // Placeholder for generic parameter
{
var methodParam = metadata.MethodMetadata.Parameters[i];
if (methodParam.TypeInfo is GenericParameter { IsMethodParameter: false } gp2)
{
var genericParamName = gp2.Name;
var matchingClassParam = genericParameters.FirstOrDefault(p => p.Name == genericParamName);
if (matchingClassParam != null)
{
typeMapping[matchingClassParam] = elementType;
}
}
}
}
}
}
}
}
}
var resolvedTypes = new Type[genericParameters.Length];
for (var i = 0; i < genericParameters.Length; i++)
{
var genericParam = genericParameters[i];
if (!typeMapping.TryGetValue(genericParam, out var resolvedType))
{
throw new InvalidOperationException(
$"Could not resolve type for generic parameter '{genericParam.Name}' of type '{genericClassType.Name}' from data sources");
}
resolvedTypes[i] = resolvedType;
}
return resolvedTypes;
}
private static readonly IDataSourceAttribute[] _dataSourceArray = [NoDataSource.Instance];
private async Task<IDataSourceAttribute[]> GetDataSourcesAsync(IDataSourceAttribute[] dataSources, CancellationToken cancellationToken = default)
{
if (dataSources.Length == 0)
{
return _dataSourceArray;
}
// Inject properties into data sources during discovery (IAsyncInitializer deferred to execution)
foreach (var dataSource in dataSources)
{
await _objectLifecycleService.InjectPropertiesAsync(dataSource, cancellationToken: cancellationToken);
}
return dataSources;
}
/// <summary>
/// Ensures a data source is initialized before use and returns data rows.
/// This centralizes the initialization logic for all data source usage.
/// </summary>
private async IAsyncEnumerable<Func<Task<object?[]?>>> GetInitializedDataRowsAsync(
IDataSourceAttribute dataSource,
DataGeneratorMetadata dataGeneratorMetadata,
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// Inject properties into data source during discovery (IAsyncInitializer deferred to execution)
var propertyInjectedDataSource = await _objectLifecycleService.InjectPropertiesAsync(
dataSource,
dataGeneratorMetadata.TestBuilderContext.Current.StateBag,
dataGeneratorMetadata.TestInformation,
dataGeneratorMetadata.TestBuilderContext.Current.Events,
cancellationToken);
// Now get data rows from the property-injected data source
await foreach (var dataRow in propertyInjectedDataSource.GetDataRowsAsync(dataGeneratorMetadata))
{
yield return dataRow;
}
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Hook discovery service handles mode-specific logic; reflection calls suppressed in AOT mode")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Hook discovery service handles mode-specific logic; dynamic code suppressed in AOT mode")]
public async Task<AbstractExecutableTest> BuildTestAsync(TestMetadata metadata, TestData testData, TestBuilderContext testBuilderContext, bool isReusingDiscoveryInstance = false, CancellationToken cancellationToken = default)
{
// Discover instance hooks for closed generic types (no-op in source gen mode)
if (metadata.TestClassType is { IsGenericType: true, IsGenericTypeDefinition: false })
{
_hookDiscoveryService.DiscoverInstanceHooksForType(metadata.TestClassType);
}
var testId = TestIdentifierService.GenerateTestId(metadata, testData);
var context = await CreateTestContextAsync(testId, metadata, testData, testBuilderContext, cancellationToken);
// Mark if this test is reusing the discovery instance (already initialized)
context.IsDiscoveryInstanceReused = isReusingDiscoveryInstance;
context.Metadata.TestDetails.ClassInstance = PlaceholderInstance.Instance;
// Apply metadata from TestDataRow or data source attributes
if (testData.Metadata is { } dataRowMetadata)
{
// Apply custom display name (will be processed by DisplayNameBuilder)
if (!string.IsNullOrEmpty(dataRowMetadata.DisplayName))
{
context.SetDataSourceDisplayName(dataRowMetadata.DisplayName!);
}
// Apply data expression for default display name formatting (only when no explicit DisplayName)
if (string.IsNullOrEmpty(dataRowMetadata.DisplayName) && !string.IsNullOrEmpty(dataRowMetadata.DataExpression))
{
context.SetDataSourceExpression(dataRowMetadata.DataExpression!);
}
// Apply skip reason from data source
if (!string.IsNullOrEmpty(dataRowMetadata.Skip))
{
context.SkipReason = dataRowMetadata.Skip;
}
// Apply categories from data source
if (dataRowMetadata.Categories is { Length: > 0 })
{
foreach (var category in dataRowMetadata.Categories)
{
context.Metadata.TestDetails.Categories.Add(category);
}
}
}
// Arguments will be tracked by TestArgumentTrackingService during TestRegistered event
// This ensures proper reference counting for shared instances
// Create the test object BEFORE invoking event receivers
// This ensures context.InternalExecutableTest is set for error handling in registration
var creationContext = new ExecutableTestCreationContext
{
TestId = testId,
DisplayName = context.GetDisplayName(),
Arguments = testData.MethodData,
ClassArguments = testData.ClassData,
Context = context,
TestClassInstanceFactory = testData.TestClassInstanceFactory,
ResolvedMethodGenericArguments = testData.ResolvedMethodGenericArguments,
ResolvedClassGenericArguments = testData.ResolvedClassGenericArguments
};
var test = metadata.CreateExecutableTestFactory(creationContext, metadata);
// Set InternalExecutableTest so it's available during registration for error handling
context.InternalExecutableTest = test;
// Test argument registration (property injection + reference counting) is NOT done here.
// It happens post-filter in TestFilterService.RegisterTest so that shared-object reference
// counts only include tests that will actually execute. Registering at build time inflated
// the counts with built-but-filtered-out tests (e.g. [Explicit] siblings or single
// [Arguments] cases selected by an IDE uid filter), so the count never drained to zero and
// shared fixtures were never disposed (#6151).
// ITestRegisteredEventReceiver and ITestDiscoveryEventReceiver are invoked later in
// InvokePostResolutionEventsAsync after dependencies are resolved.
return test;
}
/// <summary>
/// Invokes event receivers after dependencies have been resolved.
/// This is called from TestDiscoveryService after all tests are built and dependencies resolved.
/// </summary>
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type comes from runtime objects that cannot be annotated")]
#endif
public async ValueTask InvokePostResolutionEventsAsync(AbstractExecutableTest test)
{
var context = test.Context;
// Set TestContext.Current so output capture works via AsyncLocal
// This ensures any console output during event receiver invocation is captured
TestContext.Current = context;
// Populate TestContext._dependencies from resolved test.Dependencies
// This makes dependencies available to event receivers and hooks
PopulateDependenciesOnly(test);
// Invoke discovery event receivers (for all tests during discovery phase)
// Note: ITestRegisteredEventReceiver.OnTestRegistered is NOT called here.
// Registration events fire later in TestFilterService.RegisterTest for filtered tests only.
// This ensures OnTestRegistered is called exactly once per test that will actually run.
await InvokeDiscoveryEventReceiversAsync(context);
}
/// <inheritdoc />
public void PopulateDependenciesOnly(AbstractExecutableTest test)
{
var context = test.Context;
// Skip if already populated to make this idempotent
if (context._dependenciesPopulated)
{
return;
}
PopulateDependencies(test, context._dependencies);
context._dependenciesPopulated = true;
}
private static void PopulateDependencies(AbstractExecutableTest test, List<TestDetails> dependencies)
{
var collected = new HashSet<TestDetails>();
var visited = new HashSet<AbstractExecutableTest>();
CollectAllDependencies(test, collected, visited);
foreach (var dependency in collected)
{
dependencies.Add(dependency);
}
}