-
-
Notifications
You must be signed in to change notification settings - Fork 130
Fix mocks with inaccessible constructor parameter types #6635
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -846,19 +846,38 @@ private static string GetAccessorObsoleteAttributeSyntax(string propertyObsolete | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Discovers all accessible constructors of a class type for partial mock generation. | ||
| /// Discovers constructors usable by the generated subclass. Partial-mock factories also need | ||
| /// to name every parameter type; wrap factories only accept an existing instance. | ||
| /// </summary> | ||
| public static EquatableArray<MockConstructorModel> DiscoverConstructors(INamedTypeSymbol typeSymbol, IAssemblySymbol? compilationAssembly = null) | ||
| public static EquatableArray<MockConstructorModel> DiscoverConstructors( | ||
| INamedTypeSymbol typeSymbol, | ||
| Compilation compilation, | ||
| bool requiresFactoryAccessibleParameterTypes) | ||
| { | ||
| var constructors = new List<MockConstructorModel>(); | ||
|
|
||
| foreach (var ctor in typeSymbol.InstanceConstructors) | ||
| { | ||
| if (ctor.DeclaredAccessibility == Accessibility.Private) continue; | ||
| if (!IsMemberAccessible(ctor, compilationAssembly)) continue; | ||
| if (ctor.DeclaredAccessibility == Accessibility.Private) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (!IsMemberAccessible(ctor, compilation.Assembly)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (requiresFactoryAccessibleParameterTypes | ||
| && !ctor.Parameters.All(p => TypeAccessibility.IsAccessibleFromAssembly(p.Type, compilation))) | ||
|
Comment on lines
+874
to
+875
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.
When Useful? React with 👍 / 👎. |
||
| { | ||
| continue; | ||
| } | ||
|
|
||
| constructors.Add(new MockConstructorModel | ||
| { | ||
| HasPubliclyAccessibleParameterTypes = ctor.Parameters.All( | ||
| p => TypeAccessibility.IsEffectivelyPublic(p.Type)), | ||
| Parameters = new EquatableArray<MockParameterModel>( | ||
| ctor.Parameters.Select(p => new MockParameterModel | ||
| { | ||
|
|
||
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.
When
Mock.Wraptargets an external unsafe class whose only chainable constructor takes a pointer or function-pointer parameter, this bypass treats the constructor as usable and suppresses TM006. The generator still rejects that constructor inMemberDiscovery.IsMemberAccessiblebecauseIsTypeAccessiblereturns false for pointer signatures, leaving an empty constructor model; it consequently emits only the unconstructable stub and never registers a wrap factory, soMock.Wrap(instance)throws “No wrap mock factory registered” at runtime without the intended diagnostic. Wrap should skip only the assembly-nameability portion of parameter validation, not the intrinsic pointer/function-pointer rejection.Useful? React with 👍 / 👎.