Repository navigation
fix(mocks): eliminate trim/AOT warnings on Native AOT publish #7029
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2f8237
fix(mocks): suppress trim/AOT warnings via attributes so ILC honours …
thomhurst de3d819
fix(mocks): pre-register closed generic mocks and guard AOT warnings …
thomhurst 3ae6127
docs(mocks): prefer T.Mock() over Mock.Of<T>() in examples and guidance
thomhurst f83882c
fix(mocks): address review on closed generic registrations
thomhurst fbe129f
fix(mocks): recommend T.Mock() in the Native AOT fallback error
thomhurst 8d4f84b
test(mocks): T.Mock() with a private nested value-type argument
thomhurst 19f45d0
refactor(mocks): keep fallback suppressions on the reflection method …
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/TUnit.Mocks.SourceGenerator/Builders/ClosedGenericRegistrationBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| using System.Collections.Immutable; | ||
| using TUnit.Mocks.SourceGenerator.Models; | ||
|
|
||
| namespace TUnit.Mocks.SourceGenerator.Builders; | ||
|
|
||
| /// <summary> | ||
| /// Emits closed factory registrations for generic interface mocks named with concrete type | ||
| /// arguments, so <c>Mock.Of<IRepository<int>>()</c> never reaches the | ||
| /// <c>MakeGenericType</c> fallback in <c>MockRegistry</c>. See issue #7027. | ||
| /// </summary> | ||
| internal static class ClosedGenericRegistrationBuilder | ||
| { | ||
| public const string HintName = "TUnit.Mocks.ClosedGenericFactories.g.cs"; | ||
|
|
||
| public static string Build(EquatableArray<ClosedGenericFactoryEmission> registrations) | ||
| { | ||
| var writer = new CodeWriter(); | ||
|
|
||
| writer.AppendLine("// <auto-generated/>"); | ||
| writer.AppendLine("#pragma warning disable"); | ||
| writer.AppendLine("#nullable enable"); | ||
| writer.AppendLine(); | ||
| writer.AppendLine("namespace TUnit.Mocks.Generated;"); | ||
| writer.AppendLine(); | ||
|
|
||
| using (writer.Block("file static class ClosedGenericMockFactories")) | ||
| { | ||
| writer.AppendLine("[global::System.Runtime.CompilerServices.ModuleInitializer]"); | ||
| using (writer.Block("internal static void Register()")) | ||
| { | ||
| foreach (var registration in registrations) | ||
| { | ||
| var closedType = registration.ClosedTypeFullyQualifiedName; | ||
| var displayName = closedType.Replace("global::", string.Empty); | ||
|
|
||
| writer.AppendLine($"global::TUnit.Mocks.MockRegistry.RegisterFactory<{closedType}>(static (behavior, constructorArgs) =>"); | ||
| writer.AppendLine("{"); | ||
| writer.AppendLine($" if (constructorArgs.Length > 0) throw new global::System.ArgumentException($\"Interface mock '{displayName}' does not support constructor arguments, but {{constructorArgs.Length}} were provided.\");"); | ||
| writer.AppendLine($" return {registration.FactoryMethod}(behavior);"); | ||
| writer.AppendLine("});"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return writer.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Pairs each distinct registration with the open mock generated for it. Registrations whose | ||
| /// open mock was not generated as a plain single-interface mock (dropped, colliding, or bridged | ||
| /// for static abstract members) are skipped and keep using the runtime fallback. | ||
| /// </summary> | ||
| public static EquatableArray<ClosedGenericFactoryEmission> Resolve( | ||
| ImmutableArray<ClosedGenericMockRegistration> registrations, | ||
| ImmutableArray<MockTypeModel> models) | ||
| { | ||
| if (registrations.IsDefaultOrEmpty) | ||
| { | ||
| return EquatableArray<ClosedGenericFactoryEmission>.Empty; | ||
| } | ||
|
|
||
| var openModels = new Dictionary<string, MockTypeModel>(StringComparer.Ordinal); | ||
| foreach (var model in models) | ||
| { | ||
| if (model is { IsInterface: true, IsPartialMock: false, IsWrapMock: false, IsDelegateType: false, IsSecondaryMemberSurface: false, HasStaticAbstractMembers: false, CollidesWith: null } | ||
| && model.AdditionalInterfaceNames.Length == 0 | ||
| && model.TypeParameters.Length > 0) | ||
| { | ||
| openModels[model.FullyQualifiedName] = model; | ||
| } | ||
| } | ||
|
|
||
| var seen = new HashSet<string>(StringComparer.Ordinal); | ||
| var emissions = new List<ClosedGenericFactoryEmission>(); | ||
| foreach (var registration in registrations) | ||
| { | ||
| if (!openModels.TryGetValue(registration.OpenTypeFullyQualifiedName, out var model) | ||
| || model.TypeParameters.Length != registration.TypeArguments.Length | ||
| || !seen.Add(registration.ClosedTypeFullyQualifiedName)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| emissions.Add(new ClosedGenericFactoryEmission( | ||
| registration.ClosedTypeFullyQualifiedName, | ||
| $"{MockImplBuilder.GetGlobalMockNamespacePrefix(model)}{MockImplBuilder.GetCompositeShortSafeName(model)}MockFactory.CreateAutoMock<{string.Join(", ", registration.TypeArguments)}>")); | ||
| } | ||
|
|
||
| // Ordinal order keeps the emitted file stable regardless of call-site discovery order. | ||
| emissions.Sort(static (x, y) => StringComparer.Ordinal.Compare(x.ClosedTypeFullyQualifiedName, y.ClosedTypeFullyQualifiedName)); | ||
| return new EquatableArray<ClosedGenericFactoryEmission>(emissions.ToImmutableArray()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.