From 253801c133bc9d6b3db51cb99b05d8c83b4dfcfa Mon Sep 17 00:00:00 2001 From: Agent Driver Date: Fri, 27 Nov 2026 14:44:50 -0500 Subject: [PATCH 1/3] Report DM0021 for VerificationGlue.Mocks.MockAttribute DM0021 recognized only DependencyModules' [Mock], so a VerificationGlue test with VG's [Mock] and a [TestExport] for the same service got no report, though VG gives the parameter the same precedence. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + docs/design/verificationglue.md | 7 +- .../KnownTypes.cs | 22 +++ .../TestAttributeDiagnostics.cs | 24 ++- .../Attributes/MockAttribute.cs | 3 +- ...icApiTests.SourceGeneratorApi.verified.txt | 8 + .../MockTestExportDiagnosticTests.cs | 175 ++++++++++++++++++ website/reference/diagnostics.md | 2 + 8 files changed, 235 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c26d4ca..5847a4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `IDependencyModule` has the new member `InternalApplyGlue`, with a default implementation. The load of the modules has a last step that adds the collections and the `Func` registrations. (#132) +- DM0021 also reads `[Mock]` from `VerificationGlue.Mocks`. A VerificationGlue test with that + `[Mock]` and a `[TestExport]` for the same service on one method now gets DM0021. ## [1.7.0] - 2026-09-25 diff --git a/docs/design/verificationglue.md b/docs/design/verificationglue.md index f01a4e5..1808664 100644 --- a/docs/design/verificationglue.md +++ b/docs/design/verificationglue.md @@ -566,8 +566,11 @@ second report would be a duplicate. - **DM0016 and DM0019** read assembly-level module attributes. In a VG project, DM0016 is right unchanged. DM0019 reports only when an `ApplicationModule` was generated, and DM generates one only from a file named `Program.cs` (`BaseSourceGenerator.CreateSourceValueProvider`, - `ApproveFilter`). MTP writes the entry point of a VG project. I did not check that file's name. - If it were `Program.cs`, `[assembly: SutModule]` in any other file would report DM0019 wrongly. + `ApproveFilter`). MTP writes the entry point of a VG project. Microsoft.Testing.Platform.MSBuild + 2.5.3 and 3.0.0-preview name it `MicrosoftTestingPlatformEntryPoint.cs` + (`_TestingPlatformEntryPointSourceName`), which does not end in `Program.cs`. So DM0019 is right + unchanged too. Had it been `Program.cs`, `[assembly: SutModule]` in any other file would have + reported DM0019 wrongly. - DM's `MockAttribute` remarks say that it throws without a mock-support attribute. The remarks get a sentence saying that under VerificationGlue it is a VerificationGlue mock. diff --git a/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs b/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs index 36f78f2..c14b5d6 100644 --- a/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs +++ b/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs @@ -304,6 +304,28 @@ private static ITypeDefinition Close(string name, ITypeDefinition argument) => } } + /// + /// VerificationGlue types, matched by name. No project here references VerificationGlue. + /// + public static class VerificationGlue + { + public static class Mocks + { + public const string Namespace = "VerificationGlue.Mocks"; + + /// + /// VerificationGlue's own [Mock]. Under VerificationGlue it has the same + /// precedence over [TestExport] as DependencyModules' [Mock], so DM0021 + /// reads both. + /// + public static readonly ITypeDefinition MockAttribute = TypeDefinition.Get( + TypeDefinitionEnum.ClassDefinition, + Namespace, + "MockAttribute" + ); + } + } + public static class System { public static readonly ITypeDefinition ArgumentOutOfRangeException = TypeDefinition.Get( diff --git a/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs b/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs index ea91400..7b18262 100644 --- a/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs +++ b/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs @@ -25,6 +25,12 @@ namespace DependencyModules.SourceGenerator; /// everything under it, and one test overriding that for one argument is what having both scopes is /// for — reporting that would be reporting the feature. /// +/// +/// [Mock] is either DependencyModules.Testing.Attributes.MockAttribute or +/// VerificationGlue.Mocks.MockAttribute. Under VerificationGlue both win over a +/// [TestExport] on the same method. VerificationGlue reports no diagnostic of its own for +/// this, so this is the only report. +/// /// internal static class TestAttributeDiagnostics { @@ -95,11 +101,19 @@ System.Threading.CancellationToken cancellationToken // A keyed mock is registered under its key, so the unkeyed export stays in use. if ( - !Carries( - parameter, - KnownTypes.DependencyModules.Testing.MockAttribute, - context, - cancellationToken + !( + Carries( + parameter, + KnownTypes.DependencyModules.Testing.MockAttribute, + context, + cancellationToken + ) + || Carries( + parameter, + KnownTypes.VerificationGlue.Mocks.MockAttribute, + context, + cancellationToken + ) ) || Carries( parameter, diff --git a/src/DependencyModules.Testing/Attributes/MockAttribute.cs b/src/DependencyModules.Testing/Attributes/MockAttribute.cs index ed812f6..2162247 100644 --- a/src/DependencyModules.Testing/Attributes/MockAttribute.cs +++ b/src/DependencyModules.Testing/Attributes/MockAttribute.cs @@ -16,7 +16,8 @@ namespace DependencyModules.Testing.Attributes; /// Creating the double is delegated to whichever mocking package is in scope, which must implement /// : [NSubstituteSupport], [MoqSupport] or /// [FakeItEasySupport]. Without one this throws rather than silently handing back a real -/// service. +/// service. Under VerificationGlue the parameter is a VerificationGlue mock, the same as one marked +/// with VerificationGlue.Mocks.MockAttribute, and no mock-support attribute is needed. /// /// A library that separates the double from the object it produces may let the parameter name either. /// With Moq, [Mock] IFoo gives the object and Mock<IFoo> gives the mock — and on a diff --git a/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt b/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt index e08cf1a..dfb6efc 100644 --- a/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt +++ b/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt @@ -1485,6 +1485,14 @@ namespace DependencyModules.SourceGenerator.Impl public static CSharpAuthor.ITypeDefinition AsyncEnumerable(CSharpAuthor.ITypeDefinition item) { } public static CSharpAuthor.ITypeDefinition ValueTask(CSharpAuthor.ITypeDefinition result) { } } + public static class VerificationGlue + { + public static class Mocks + { + public const string Namespace = "VerificationGlue.Mocks"; + public static readonly CSharpAuthor.ITypeDefinition MockAttribute; + } + } } public class ModuleAttributeWriter : DependencyModules.SourceGenerator.Impl.Utilities.BaseAttributeWriter { diff --git a/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs b/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs index 9fdfc74..85aa7ee 100644 --- a/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs +++ b/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs @@ -192,6 +192,181 @@ public void Conflicting([global::DependencyModules.Testing.Attributes.Mock] IThi Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); } + /// + /// VerificationGlue's [Mock] wins over [TestExport] as DependencyModules' does, so + /// the two spellings get the same report at the same place. + /// + [Fact] + public void VerificationGlueMock_GetsTheSameReportAsDependencyModulesMock() + { + const string body = """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Conflicting([Mock] IThing thing) { } + """; + + var dependencyModules = Assert.Single( + Run(body).GeneratorDiagnostics, + d => d.Id == "DM0021" + ); + var verificationGlueResult = RunVerificationGlue(body); + var verificationGlue = Assert.Single( + verificationGlueResult.GeneratorDiagnostics, + d => d.Id == "DM0021" + ); + + Assert.Empty(verificationGlueResult.Errors); + Assert.Equal(dependencyModules.GetMessage(), verificationGlue.GetMessage()); + Assert.Equal(dependencyModules.Severity, verificationGlue.Severity); + Assert.Equal( + "Test.cs", + System.IO.Path.GetFileName(verificationGlue.Location.GetLineSpan().Path) + ); + Assert.Equal( + "[Mock] IThing thing", + verificationGlue + .Location.SourceTree!.GetText() + .ToString(verificationGlue.Location.SourceSpan) + ); + } + + [Fact] + public void VerificationGlueMock_QualifiedSpelling_IsReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Conflicting([global::VerificationGlue.Mocks.Mock] IThing thing) { } + """ + ); + + Assert.Empty(result.Errors); + Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + [Fact] + public void VerificationGlueMock_Keyed_IsNotReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Keyed([Mock] [Microsoft.Extensions.DependencyInjection.FromKeyedServices("k")] IThing thing) { } + """ + ); + + Assert.Empty(result.Errors); + Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + [Fact] + public void VerificationGlueMock_NamingAnotherService_IsNotReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IOther), Implementation = typeof(RealOther))] + public void Unrelated([Mock] IThing thing) { } + """ + ); + + Assert.Empty(result.Errors); + Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + [Fact] + public void VerificationGlueMock_WithTestExportOnTheClass_IsNotReported() + { + var result = GeneratorTestHarness.Run( + $$""" + {{VerificationGluePreamble}} + + namespace TestNamespace + { + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public class Fixture { + public void Overriding([Mock] IThing thing) { } + } + } + """ + ); + + Assert.Empty(result.Errors); + Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + /// + /// Matched by namespace and name, so a MockAttribute of another library is not read. + /// + [Fact] + public void AMockAttributeInAnotherNamespace_IsNotReported() + { + var result = GeneratorTestHarness.Run( + $$""" + using DependencyModules.Testing.Attributes; + + namespace Elsewhere + { + [System.AttributeUsage(System.AttributeTargets.Parameter)] + public sealed class MockAttribute : System.Attribute; + } + + namespace TestNamespace + { + public interface IThing; + + public class RealThing : IThing; + + public class Fixture { + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void NotAMock([Elsewhere.Mock] IThing thing) { } + } + } + """ + ); + + Assert.Empty(result.Errors); + Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + /// + /// A VerificationGlue test file. The stub stands in for VerificationGlue.Mocks, which no + /// project here references. [TestExport] is imported by alias, because importing both + /// namespaces makes [Mock] ambiguous. + /// + private const string VerificationGluePreamble = """ + using VerificationGlue.Mocks; + using TestExportAttribute = DependencyModules.Testing.Attributes.TestExportAttribute; + + namespace VerificationGlue.Mocks + { + [System.AttributeUsage(System.AttributeTargets.Parameter)] + public sealed class MockAttribute : System.Attribute; + } + + namespace TestNamespace + { + public interface IThing; + + public interface IOther; + + public class RealThing : IThing; + + public class RealOther : IOther; + } + """; + + private static GeneratorResult RunVerificationGlue(string body) => + GeneratorTestHarness.Run( + $$""" + {{VerificationGluePreamble}} + + namespace TestNamespace + { + public class Fixture { + {{body}} + } + } + """ + ); + private const string Preamble = """ using DependencyModules.Runtime.Attributes; using DependencyModules.Testing.Attributes; diff --git a/website/reference/diagnostics.md b/website/reference/diagnostics.md index 6615d8e..4b105cb 100644 --- a/website/reference/diagnostics.md +++ b/website/reference/diagnostics.md @@ -230,6 +230,8 @@ To correct the problem, set `Realm` on `[Intercept]` to the module that register A test method has `[TestExport]` for a service type and a `[Mock]` parameter of the same type. The mock replaces the `[TestExport]` registration. The generator gives DM0021 only if the test project references `DependencyModules.SourceGenerator`. +`[Mock]` can be `DependencyModules.Testing.Attributes.MockAttribute` or `VerificationGlue.Mocks.MockAttribute`. The generator gives the same DM0021 for both. + The generator gives no DM0021 for the two exceptions, because the `[TestExport]` registration stays in use: - The `[Mock]` parameter has `[FromKeyedServices]`. From dc84070d6adb2e48e58679bcf3b13d19a5387554 Mon Sep 17 00:00:00 2001 From: Agent Driver Date: Fri, 27 Nov 2026 14:49:34 -0500 Subject: [PATCH 2/3] Pass the test cancellation token to GetText in the DM0021 test Co-Authored-By: Claude Opus 5.5 --- .../GeneratorTests/MockTestExportDiagnosticTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs b/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs index 85aa7ee..18747b7 100644 --- a/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs +++ b/tests/DependencyModules.Tests/GeneratorTests/MockTestExportDiagnosticTests.cs @@ -224,7 +224,7 @@ public void Conflicting([Mock] IThing thing) { } Assert.Equal( "[Mock] IThing thing", verificationGlue - .Location.SourceTree!.GetText() + .Location.SourceTree!.GetText(TestContext.Current.CancellationToken) .ToString(verificationGlue.Location.SourceSpan) ); } From f631362da50e30d66afb8e2fb82dcdba16933e51 Mon Sep 17 00:00:00 2001 From: Agent Driver Date: Fri, 27 Nov 2026 17:20:17 -0500 Subject: [PATCH 3/3] Convert the VerificationGlue DM0021 tests to VerifyTest The merge of main moved MockTestExportDiagnosticTests into DependencyModules.Specs, which runs on VerificationGlue.Tests, so the six tests this branch added could not use xUnit. Co-Authored-By: Claude Opus 5.5 --- .../MockTestExportDiagnosticTests.cs | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs b/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs index 6a6e747..093406c 100644 --- a/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs +++ b/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs @@ -195,7 +195,7 @@ public void Conflicting([global::DependencyModules.Testing.Attributes.Mock] IThi /// VerificationGlue's [Mock] wins over [TestExport] as DependencyModules' does, so /// the two spellings get the same report at the same place. /// - [Fact] + [VerifyTest] public void VerificationGlueMock_GetsTheSameReportAsDependencyModulesMock() { const string body = """ @@ -203,32 +203,27 @@ public void VerificationGlueMock_GetsTheSameReportAsDependencyModulesMock() public void Conflicting([Mock] IThing thing) { } """; - var dependencyModules = Assert.Single( - Run(body).GeneratorDiagnostics, - d => d.Id == "DM0021" - ); + var dependencyModules = Single(Run(body).GeneratorDiagnostics, d => d.Id == "DM0021"); var verificationGlueResult = RunVerificationGlue(body); - var verificationGlue = Assert.Single( + var verificationGlue = Single( verificationGlueResult.GeneratorDiagnostics, d => d.Id == "DM0021" ); - Assert.Empty(verificationGlueResult.Errors); - Assert.Equal(dependencyModules.GetMessage(), verificationGlue.GetMessage()); - Assert.Equal(dependencyModules.Severity, verificationGlue.Severity); - Assert.Equal( - "Test.cs", - System.IO.Path.GetFileName(verificationGlue.Location.GetLineSpan().Path) - ); - Assert.Equal( - "[Mock] IThing thing", - verificationGlue - .Location.SourceTree!.GetText(TestContext.Current.CancellationToken) - .ToString(verificationGlue.Location.SourceSpan) + Verify(!verificationGlueResult.Errors.Any()); + var reportedText = verificationGlue + .Location.SourceTree!.GetText() + .ToString(verificationGlue.Location.SourceSpan); + + Verify(dependencyModules.GetMessage() == verificationGlue.GetMessage()); + Verify(dependencyModules.Severity == verificationGlue.Severity); + Verify( + System.IO.Path.GetFileName(verificationGlue.Location.GetLineSpan().Path) == "Test.cs" ); + Verify(reportedText == "[Mock] IThing thing"); } - [Fact] + [VerifyTest] public void VerificationGlueMock_QualifiedSpelling_IsReported() { var result = RunVerificationGlue( @@ -238,11 +233,11 @@ public void Conflicting([global::VerificationGlue.Mocks.Mock] IThing thing) { } """ ); - Assert.Empty(result.Errors); - Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + Verify(!result.Errors.Any()); + Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); } - [Fact] + [VerifyTest] public void VerificationGlueMock_Keyed_IsNotReported() { var result = RunVerificationGlue( @@ -252,11 +247,11 @@ public void Keyed([Mock] [Microsoft.Extensions.DependencyInjection.FromKeyedServ """ ); - Assert.Empty(result.Errors); - Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); } - [Fact] + [VerifyTest] public void VerificationGlueMock_NamingAnotherService_IsNotReported() { var result = RunVerificationGlue( @@ -266,11 +261,11 @@ public void Unrelated([Mock] IThing thing) { } """ ); - Assert.Empty(result.Errors); - Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); } - [Fact] + [VerifyTest] public void VerificationGlueMock_WithTestExportOnTheClass_IsNotReported() { var result = GeneratorTestHarness.Run( @@ -287,14 +282,14 @@ public void Overriding([Mock] IThing thing) { } """ ); - Assert.Empty(result.Errors); - Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); } /// /// Matched by namespace and name, so a MockAttribute of another library is not read. /// - [Fact] + [VerifyTest] public void AMockAttributeInAnotherNamespace_IsNotReported() { var result = GeneratorTestHarness.Run( @@ -321,8 +316,8 @@ public void NotAMock([Elsewhere.Mock] IThing thing) { } """ ); - Assert.Empty(result.Errors); - Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); } ///