Skip to content
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

Register off-by-default analyzer: configuration binding generator #31654

Merged
merged 7 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<ItemGroup>
<OffByDefaultAnalyzer Include="Microsoft.AspNetCore.Http.RequestDelegateGenerator"
IsEnabled="$(EnableRequestDelegateGenerator)"/>
<OffByDefaultAnalyzer Include="Microsoft.Extensions.Configuration.Binder.SourceGeneration"
IsEnabled="$(EnableConfigurationBindingGenerator)"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,51 @@ public GivenThatWeWantToUseAnalyzers(ITestOutputHelper log) : base(log)
}

[Theory]
[InlineData("WebApp", false)]
[InlineData("WebApp", true)]
[InlineData("WebApp", null)]
public void It_resolves_requestdelegategenerator_correctly(string testAssetName, bool? isEnabled)
[MemberData(nameof(OffByDefaultGeneratorTestData))]
public void It_resolves_offbydefaultgenerator_correctly(GeneratorSpec generator, bool? isEnabled)
{
var asset = _testAssetsManager
.CopyTestAsset(testAssetName, identifier: isEnabled.ToString())
.CopyTestAsset("WebApp", identifier: isEnabled.ToString())
.WithSource()
.WithProjectChanges(project =>
{
if (isEnabled != null)
{
var ns = project.Root.Name.Namespace;
project.Root.Add(new XElement(ns + "PropertyGroup", new XElement("EnableRequestDelegateGenerator", isEnabled)));
// Delete when we have a new targeting pack that contains Microsoft.Extensions.Configuration.Binder.SourceGeneration.
if (generator == GeneratorSpec.ConfigurationBinding)
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
project.Root.Add(new XElement(ns + "ItemGroup", new XElement("Analyzer", new XAttribute("Include", "does_not_yet_exist\\Microsoft.Extensions.Configuration.Binder.SourceGeneration.dll"))));
}
project.Root.Add(new XElement(ns + "PropertyGroup", new XElement(generator.EnablingPropertyName, isEnabled)));
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
});

VerifyRequestDelegateGeneratorIsUsed(asset, isEnabled);
VerifyOffByDefaultGeneratorIsUsed(asset, generator.DllFileName, isEnabled);
}

[Fact]
public void It_enables_requestdelegategenerator_for_PublishAot()
[Theory]
[MemberData(nameof(OffByDefaultGenerators))]
public void It_enables_offbydefaultgenerator_for_PublishAot(GeneratorSpec generator)
{
var asset = _testAssetsManager
.CopyTestAsset("WebApp")
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
// Delete when we have a new targeting pack that contains Microsoft.Extensions.Configuration.Binder.SourceGeneration.
if (generator == GeneratorSpec.ConfigurationBinding)
{
project.Root.Add(new XElement(ns + "ItemGroup", new XElement("Analyzer", new XAttribute("Include", "does_not_yet_exist\\Microsoft.Extensions.Configuration.Binder.SourceGeneration.dll"))));
}
project.Root.Add(new XElement(ns + "PropertyGroup", new XElement("PublishAot", "true")));
});

VerifyRequestDelegateGeneratorIsUsed(asset, expectEnabled: true);
VerifyOffByDefaultGeneratorIsUsed(asset, generator.DllFileName, expectEnabled: true);
}

private void VerifyRequestDelegateGeneratorIsUsed(TestAsset asset, bool? expectEnabled)
private void VerifyOffByDefaultGeneratorIsUsed(TestAsset asset, string dllFileName, bool? expectEnabled)
{
var command = new GetValuesCommand(
Log,
Expand All @@ -75,7 +84,31 @@ private void VerifyRequestDelegateGeneratorIsUsed(TestAsset asset, bool? expectE

var analyzers = command.GetValues();

Assert.Equal(expectEnabled ?? false, analyzers.Any(analyzer => analyzer.Contains("Microsoft.AspNetCore.Http.RequestDelegateGenerator.dll")));
Assert.Equal(expectEnabled ?? false, analyzers.Any(analyzer => analyzer.Contains(dllFileName)));
layomia marked this conversation as resolved.
Show resolved Hide resolved
}

public record GeneratorSpec(string EnablingPropertyName, string DllFileName)
{
public static GeneratorSpec RequestDelegate { get; } = new GeneratorSpec("EnableRequestDelegateGenerator", "Microsoft.AspNetCore.Http.RequestDelegateGenerator.dll");
public static GeneratorSpec ConfigurationBinding { get; } = new GeneratorSpec("EnableConfigurationBindingGenerator", "Microsoft.Extensions.Configuration.Binder.SourceGeneration.dll");
}

public static IEnumerable<object[]> OffByDefaultGenerators()
{
yield return new object[] { GeneratorSpec.RequestDelegate };
yield return new object[] { GeneratorSpec.ConfigurationBinding };
}

public static IEnumerable<object[]> OffByDefaultGeneratorTestData()
{
foreach (object[] generator in OffByDefaultGenerators())
{
// Test asset name, generator to test, whether generator is enabled.
var spec = (GeneratorSpec)generator[0];
yield return new object[] { spec, false };
yield return new object[] { spec, true };
yield return new object[] { spec, null };
}
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Copyright (c) .NET Foundation. All rights reserved.
-->
<PropertyGroup Condition="'$(PublishAot)' == 'true'">
layomia marked this conversation as resolved.
Show resolved Hide resolved
<EnableRequestDelegateGenerator Condition="'$(EnableRequestDelegateGenerator)' == ''">true</EnableRequestDelegateGenerator>
<EnableConfigurationBindingGenerator Condition="'$(EnableConfigurationBindingGenerator)' == ''">true</EnableConfigurationBindingGenerator>
layomia marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<!--
Expand Down