diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 0afde0e..281d8e0 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -68,6 +68,20 @@ jobs: path: | **/*.nupkg **/*.snupkg + + # Test that MSBuild properties to disable generators work correctly + - name: Test Generator Disable Properties + run: | + <# Create a local NuGet source directory with the packed packages #> + New-Item -ItemType Directory -Force -Path ./nupkgs + Copy-Item -Path *.nupkg -Destination ./nupkgs/ + + <# Build and run the disable tests using the local NuGet package #> + <# The test project's NuGet.config points to ../../nupkgs relative to the project #> + dotnet test --project ./GeneratorTests/Moq.AutoMock.Generator.DisableTests/Moq.AutoMock.Generator.DisableTests.csproj ` + --configuration Release ` + --verbosity normal ` + /p:LocalNuGetSource=true # Update the docs - name: Update Docs diff --git a/.gitignore b/.gitignore index 00e1278..4a3724b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ bin/ obj/ -build/ packages/ *.suo diff --git a/Directory.Packages.props b/Directory.Packages.props index 4ee9d5d..5ee2014 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,6 +10,7 @@ + diff --git a/GeneratorTests/Moq.AutoMock.Generator.DisableTests/GeneratorDisableTests.cs b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/GeneratorDisableTests.cs new file mode 100644 index 0000000..aef3c6e --- /dev/null +++ b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/GeneratorDisableTests.cs @@ -0,0 +1,93 @@ +using System.Reflection; +using Xunit; + +namespace Moq.AutoMock.Generator.DisableTests; + +/// +/// These tests verify that when the MSBuild properties are set to disable the generators, +/// the generated extension methods are not present. +/// +/// Each generator produces a static extension class with specific methods. When disabled, +/// these types should not exist in the current assembly (the test project's compilation). +/// The source generators add code to the consuming project, so we check the current assembly. +/// +public class GeneratorDisableTests +{ + private static readonly Assembly CurrentAssembly = typeof(GeneratorDisableTests).Assembly; + + [Fact] + public void OptionsGenerator_WhenDisabled_ExtensionMethodDoesNotExist() + { + // When EnableMoqAutoMockerOptionsGenerator=false, the AutoMockerOptionsExtensions class + // should not be generated in this assembly + + bool hasGeneratedOptionsExtension = HasExtensionMethod("WithOptions"); + Assert.False(hasGeneratedOptionsExtension, + "WithOptions extension method should not exist when EnableMoqAutoMockerOptionsGenerator=false"); + } + + [Fact] + public void KeyedServicesGenerator_WhenDisabled_ExtensionMethodDoesNotExist() + { + // When EnableMoqAutoMockerKeyedServicesGenerator=false, the keyed services extension + // methods should not be generated + + bool hasGeneratedKeyedExtension = HasExtensionMethod("WithKeyedService"); + Assert.False(hasGeneratedKeyedExtension, + "WithKeyedService extension method should not exist when EnableMoqAutoMockerKeyedServicesGenerator=false"); + } + + [Fact] + public void FakeLoggingGenerator_WhenDisabled_ExtensionMethodDoesNotExist() + { + // When EnableMoqAutoMockerFakeLoggingGenerator=false, the fake logging extension + // methods should not be generated + + bool hasGeneratedLoggingExtension = HasExtensionMethod("WithFakeLogging"); + Assert.False(hasGeneratedLoggingExtension, + "WithFakeLogging extension method should not exist when EnableMoqAutoMockerFakeLoggingGenerator=false"); + } + + [Fact] + public void ApplicationInsightsGenerator_WhenDisabled_ExtensionMethodDoesNotExist() + { + // When EnableMoqAutoMockerApplicationInsightsGenerator=false, the Application Insights + // extension methods should not be generated + + bool hasGeneratedAppInsightsExtension = HasExtensionMethod("WithApplicationInsights"); + Assert.False(hasGeneratedAppInsightsExtension, + "WithApplicationInsights extension method should not exist when EnableMoqAutoMockerApplicationInsightsGenerator=false"); + } + + /// + /// Checks if an extension method with the given name exists for the AutoMocker type + /// in the current assembly (generated code is added to the consuming project). + /// + private static bool HasExtensionMethod(string methodName) + { + // Check all types in the current assembly for extension methods on AutoMocker + foreach (var type in CurrentAssembly.GetTypes()) + { + if (!type.IsSealed || !type.IsAbstract) // static classes are sealed and abstract + continue; + + if (!type.Namespace?.StartsWith("Moq.AutoMock") ?? true) + continue; + + var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public); + foreach (var method in methods) + { + if (method.Name == methodName) + { + var parameters = method.GetParameters(); + if (parameters.Length > 0 && parameters[0].ParameterType == typeof(AutoMocker)) + { + return true; + } + } + } + } + + return false; + } +} diff --git a/GeneratorTests/Moq.AutoMock.Generator.DisableTests/Moq.AutoMock.Generator.DisableTests.csproj b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/Moq.AutoMock.Generator.DisableTests.csproj new file mode 100644 index 0000000..c144dfd --- /dev/null +++ b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/Moq.AutoMock.Generator.DisableTests.csproj @@ -0,0 +1,61 @@ + + + + net8.0 + enable + enable + false + true + + + false + false + false + false + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + Analyzer + false + + + + + + + + + diff --git a/GeneratorTests/Moq.AutoMock.Generator.DisableTests/NuGet.config b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/NuGet.config new file mode 100644 index 0000000..0aaa12d --- /dev/null +++ b/GeneratorTests/Moq.AutoMock.Generator.DisableTests/NuGet.config @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + diff --git a/Moq.AutoMock/Moq.AutoMock.csproj b/Moq.AutoMock/Moq.AutoMock.csproj index 406fc0a..b66b5e3 100644 --- a/Moq.AutoMock/Moq.AutoMock.csproj +++ b/Moq.AutoMock/Moq.AutoMock.csproj @@ -6,7 +6,7 @@ Copyright © $([System.DateTime]::UtcNow.ToString("yyyy")) en-US - 3.6.1 + 3.6.2 Tim Kellogg, Adam Hewitt, Kevin Bost An auto-mocking container that generates mocks using Moq @@ -49,6 +49,7 @@ + diff --git a/Moq.AutoMock/build/Moq.AutoMock.props b/Moq.AutoMock/build/Moq.AutoMock.props new file mode 100644 index 0000000..068da9c --- /dev/null +++ b/Moq.AutoMock/build/Moq.AutoMock.props @@ -0,0 +1,12 @@ + + + + + + + + +