-
Notifications
You must be signed in to change notification settings - Fork 311
Use generated descriptors for MTP discovery #10777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,12 @@ | |||||
| /// <param name="warnings"> Contains warnings if any, that need to be passed back to the caller. </param> | ||||||
| /// <returns> list of test cases.</returns> | ||||||
| internal virtual List<UnitTestElement>? Enumerate(List<string> warnings) | ||||||
| => EnumerateCore(warnings, useGeneratedDescriptors: false); | ||||||
|
|
||||||
| internal virtual List<UnitTestElement>? Enumerate(List<string> warnings, bool useGeneratedDescriptors) | ||||||
| => EnumerateCore(warnings, useGeneratedDescriptors); | ||||||
|
|
||||||
| private List<UnitTestElement>? EnumerateCore(List<string> warnings, bool useGeneratedDescriptors) | ||||||
| { | ||||||
| if (!_typeValidator.IsValidTestClass(_type, warnings)) | ||||||
| { | ||||||
|
|
@@ -66,7 +72,13 @@ | |||||
| #endif | ||||||
|
|
||||||
| // If test class is valid, then get the tests | ||||||
| return GetTests(warnings); | ||||||
| return useGeneratedDescriptors | ||||||
| && PlatformServiceProvider.Instance.ReflectionOperations.TryGetTestMethodDescriptors( | ||||||
| _type, | ||||||
| out MethodInfo[]? descriptorMethods, | ||||||
| out bool areAllTestMethodsSupported) | ||||||
| ? GetTests(warnings, descriptorMethods, areAllTestMethodsSupported) | ||||||
| : GetTests(warnings); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
|
|
@@ -75,26 +87,46 @@ | |||||
| /// <param name="warnings"> Contains warnings if any, that need to be passed back to the caller. </param> | ||||||
| /// <returns> List of Valid Tests. </returns> | ||||||
| internal List<UnitTestElement> GetTests(List<string> warnings) | ||||||
| => GetTests(warnings, [], areAllTestMethodsSupported: false); | ||||||
|
|
||||||
| private List<UnitTestElement> GetTests(List<string> warnings, MethodInfo[] descriptorMethods, bool areAllTestMethodsSupported) | ||||||
| { | ||||||
| bool foundDuplicateTests = false; | ||||||
| var foundTests = new HashSet<string>(); | ||||||
| var tests = new List<UnitTestElement>(); | ||||||
| var tests = new List<UnitTestElement>(descriptorMethods.Length); | ||||||
| HashSet<MethodInfo>? descriptorMethodSet = descriptorMethods.Length == 0 | ||||||
| ? null | ||||||
| : new HashSet<MethodInfo>(descriptorMethods); | ||||||
|
Check failure on line 99 in src/Adapter/MSTestAdapter.PlatformServices/Discovery/TypeEnumerator.cs
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧
Suggested change
|
||||||
|
|
||||||
| // Instead of asking reflect helper to query the type for every method we have, we ask once for the type. | ||||||
| bool classDisablesParallelization = _reflectHelper.IsAttributeDefined<DoNotParallelizeAttribute>(_type); | ||||||
|
|
||||||
| // Test class is already valid. Verify methods. | ||||||
| // PERF: GetRuntimeMethods is used here to get all methods, including non-public, and static methods. | ||||||
| // if we rely on analyzers to identify all invalid methods on build, we can change this to fit the current settings. | ||||||
| foreach (MethodInfo method in PlatformServiceProvider.Instance.ReflectionOperations.GetRuntimeMethods(_type)) | ||||||
| foreach (MethodInfo method in descriptorMethods) | ||||||
| { | ||||||
| foundDuplicateTests = foundDuplicateTests || !foundTests.Add(method.ToString() ?? method.Name); | ||||||
| tests.Add(GetTestFromMethod(method, classDisablesParallelization, warnings, isFromGeneratedDescriptor: true)); | ||||||
| } | ||||||
|
|
||||||
| if (!areAllTestMethodsSupported) | ||||||
| { | ||||||
| if (_testMethodValidator.IsValidTestMethod(method, _type, warnings)) | ||||||
| foreach (MethodInfo method in PlatformServiceProvider.Instance.ReflectionOperations.GetRuntimeMethods(_type)) | ||||||
| { | ||||||
| // ToString() outputs method name and its signature. This is necessary for overloaded methods to be recognized as distinct tests. | ||||||
| foundDuplicateTests = foundDuplicateTests || !foundTests.Add(method.ToString() ?? method.Name); | ||||||
| UnitTestElement testMethod = GetTestFromMethod(method, classDisablesParallelization, warnings); | ||||||
| if (descriptorMethodSet?.Contains(method) == true) | ||||||
|
|
||||||
| { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| tests.Add(testMethod); | ||||||
| if (_testMethodValidator.IsValidTestMethod(method, _type, warnings)) | ||||||
| { | ||||||
| // ToString() outputs method name and its signature. This is necessary for overloaded methods to be recognized as distinct tests. | ||||||
| foundDuplicateTests = foundDuplicateTests || !foundTests.Add(method.ToString() ?? method.Name); | ||||||
| UnitTestElement testMethod = GetTestFromMethod(method, classDisablesParallelization, warnings); | ||||||
|
|
||||||
| tests.Add(testMethod); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -134,8 +166,9 @@ | |||||
| /// <param name="method">The reflected method.</param> | ||||||
| /// <param name="classDisablesParallelization">Whether the test class disables parallelization.</param> | ||||||
| /// <param name="warnings">Contains warnings if any, that need to be passed back to the caller.</param> | ||||||
| /// <param name="isFromGeneratedDescriptor">Whether native MTP discovery selected this method from generated metadata.</param> | ||||||
| /// <returns> Returns a UnitTestElement.</returns> | ||||||
| internal UnitTestElement GetTestFromMethod(MethodInfo method, bool classDisablesParallelization, ICollection<string> warnings) | ||||||
| internal UnitTestElement GetTestFromMethod(MethodInfo method, bool classDisablesParallelization, ICollection<string> warnings, bool isFromGeneratedDescriptor = false) | ||||||
| { | ||||||
| // null if the current instance represents a generic type parameter. | ||||||
| DebugEx.Assert(_type.AssemblyQualifiedName != null, "AssemblyQualifiedName for method is null."); | ||||||
|
|
@@ -157,6 +190,7 @@ | |||||
| IReflectionOperations reflectionOperations = PlatformServiceProvider.Instance.ReflectionOperations; | ||||||
| var testElement = new UnitTestElement(testMethod) | ||||||
| { | ||||||
| IsFromGeneratedDescriptor = isFromGeneratedDescriptor, | ||||||
| TestCategory = reflectionOperations.GetTestCategories(method, _type), | ||||||
| DoNotParallelize = classDisablesParallelization || _reflectHelper.IsAttributeDefined<DoNotParallelizeAttribute>(method), | ||||||
| ResourceLocks = MergeResourceLocks(GetClassResourceLocks(), ReadResourceLocks(method)), | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| #nullable enable | ||
| static Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.SourceGeneration.ReflectionMetadataHook.Register(System.Reflection.Assembly! assembly, System.Type![]! types, System.Collections.Generic.IReadOnlyDictionary<System.Type!, System.Reflection.MethodInfo![]!>! testMethods, System.Collections.Generic.IReadOnlyDictionary<System.Type!, System.Attribute![]!>! typeAttributes, object![]! assemblyAttributes, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Attribute![]!>! methodAttributes, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Func<object?, object?[]?, object?>!>! methodInvokers, System.Collections.Generic.IReadOnlyDictionary<System.Type!, Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.SourceGeneration.ConstructorInvokerInfo[]!>! constructorInvokers, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.PropertyInfo!, System.Action<object?, object?>!>! propertySetters) -> void | ||
| static Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.SourceGeneration.ReflectionMetadataHook.Register(System.Reflection.Assembly! assembly, System.Type![]! types, System.Collections.Generic.IReadOnlyDictionary<System.Type!, System.Reflection.MethodInfo![]!>! testMethods, System.Collections.Generic.IReadOnlyDictionary<System.Type!, System.Attribute![]!>! typeAttributes, object![]! assemblyAttributes, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Attribute![]!>! methodAttributes, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Func<object?, object?[]?, object?>!>! methodInvokers, System.Collections.Generic.IReadOnlyDictionary<System.Type!, Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.SourceGeneration.ConstructorInvokerInfo[]!>! constructorInvokers, System.Collections.Generic.IReadOnlyDictionary<System.Reflection.PropertyInfo!, System.Action<object?, object?>!>! propertySetters, System.Collections.Generic.IReadOnlyDictionary<System.Type!, System.Reflection.MethodInfo![]!>! descriptorTestMethods, System.Type![]! descriptorCompleteTypes) -> void | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,6 +188,8 @@ private static SourceGeneratedReflectionDataProvider BuildMergedSnapshot(IReadOn | |
| var typeConstructorsInvoker = new Dictionary<Type, ConstructorInvoker[]>(); | ||
| var typeMethodInvokers = new Dictionary<MethodInfo, Func<object?, object?[]?, object?>>(); | ||
| var typePropertySetters = new Dictionary<PropertyInfo, Action<object?, object?>>(); | ||
| var descriptorTestMethods = new Dictionary<Type, MethodInfo[]>(); | ||
| var descriptorCompleteTypes = new Dictionary<Type, bool>(); | ||
|
|
||
| foreach (SourceGeneratedReflectionDataProvider provider in providers) | ||
| { | ||
|
|
@@ -203,6 +205,8 @@ private static SourceGeneratedReflectionDataProvider BuildMergedSnapshot(IReadOn | |
| MergeInto(typeConstructorsInvoker, provider.TypeConstructorsInvoker); | ||
| MergeInto(typeMethodInvokers, provider.TypeMethodInvokers); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge semantics for
The correct merge semantic for completeness is logical AND: a type is only complete if all providers declare it complete. Similarly, the Consider replacing if (target.TryGetValue(kvp.Key, out bool existing))
target[kvp.Key] = existing && kvp.Value;
else
target[kvp.Key] = kvp.Value; |
||
| MergeInto(typePropertySetters, provider.TypePropertySetters); | ||
| MergeInto(descriptorTestMethods, provider.DescriptorTestMethods); | ||
| MergeInto(descriptorCompleteTypes, provider.DescriptorCompleteTypes); | ||
| } | ||
|
|
||
| return new SourceGeneratedReflectionDataProvider | ||
|
|
@@ -219,6 +223,8 @@ private static SourceGeneratedReflectionDataProvider BuildMergedSnapshot(IReadOn | |
| TypeConstructorsInvoker = typeConstructorsInvoker, | ||
| TypeMethodInvokers = typeMethodInvokers, | ||
| TypePropertySetters = typePropertySetters, | ||
| DescriptorTestMethods = descriptorTestMethods, | ||
| DescriptorCompleteTypes = descriptorCompleteTypes, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ternary branch is redundant — just pass the value directly (Code Structure).
Since the two-arg overload simply delegates to the same
EnumerateCore, this can be simplified to a single call:The overload
Enumerate(List<string> warnings, bool useGeneratedDescriptors)already exists and handles both paths.