-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Implementing analyzer to prefer WebApplicationBuilder.Configuration over Configure methods #42351
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 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c190256
modify the route to display username and date
8cb31a4
added my test file and add some implementation of my analyzer
46ed4b9
added the code and tests for analyzer in issue #35815
d71c4ad
Merge branch 'main' into WebApplicationBuilderAnalyzers
anhthidao feca4d0
Update src/Http/samples/MinimalSample/Program.cs
anhthidao e562806
Changing the severity of the error. Changing the wording of the title…
db0d5e9
Changing the severity of the error. Changing the wording of the title…
941600d
Merge branch 'WebApplicationBuilderAnalyzers' of https://github.com/d…
3b624fa
Update DiagnosticDescriptors.cs
anhthidao 47e9ecb
Merge branch 'main' into WebApplicationBuilderAnalyzers
anhthidao 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
210 changes: 210 additions & 0 deletions
210
...tCoreAnalyzers/test/WebApplicationBuilder/DisallowConfigureAppConfigureHostBuilderTest.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,210 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.AspNetCore.Analyzer.Testing; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; | ||
public partial class DisallowConfigureAppConfigureHostBuilderTest | ||
{ | ||
private TestDiagnosticAnalyzerRunner Runner { get; } = new(new WebApplicationBuilderAnalyzer()); | ||
|
||
[Fact] | ||
public async Task ConfigurationBuilderRunsWithoutDiagnostic() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Configuration.AddJsonFile(""foo.json"", optional: true); | ||
"; | ||
// Act | ||
var diagnostic = await Runner.GetDiagnosticsAsync(source); | ||
|
||
// Assert | ||
Assert.Empty(diagnostic); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfigureAppHostBuilderProducesDiagnostic() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Host./*MM*/ConfigureAppConfiguration(builder => | ||
{ | ||
builder.AddJsonFile(""foo.json"", optional: true); | ||
}); | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfigureHostHostBuilderProducesDiagnostic() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Host./*MM*/ConfigureHostConfiguration(builder => | ||
{ | ||
builder.AddJsonFile(""foo.json"", optional: true); | ||
}); | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfigureAppWebHostBuilderProducesDiagnostic() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.WebHost./*MM*/ConfigureAppConfiguration(builder => | ||
{ | ||
builder.AddJsonFile(""foo.json"", optional: true); | ||
}); | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfigureAppWebHostBuilderWithContextProducesDiagnostic() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.WebHost./*MM*/ConfigureAppConfiguration((context, webHostBuilder) => { }); | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
[Fact] | ||
public async Task ConfigureAppWebHostBuilderProducesDiagnosticInMain() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
public static class Test | ||
{ | ||
public static void Main(string[]args) { | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.WebHost./*MM*/ConfigureAppConfiguration(builder => { }); | ||
} | ||
} | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
[Fact] | ||
public async Task ConfigureAppWebHostOnBuilderProducesDiagnosticInMain() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
public static class Test | ||
{ | ||
public static void Main(string[]args) { | ||
var builder = WebApplication.CreateBuilder(args); | ||
var webhost = builder.WebHost; | ||
webhost./*MM*/ConfigureAppConfiguration(builder => { }); | ||
} | ||
} | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
[Fact] | ||
public async Task TwoInvocationsProduceTwoDiagnostic() | ||
{ | ||
// Arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Host./*MM1*/ConfigureHostConfiguration(builder => | ||
{ | ||
builder.AddJsonFile(""foo.json"", optional: true); | ||
}); | ||
builder.WebHost./*MM2*/ConfigureAppConfiguration(builder => | ||
{ | ||
builder.AddJsonFile(""foo.json"", optional: true); | ||
}); | ||
"); | ||
|
||
// Act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
// Assert | ||
Assert.Equal(2, diagnostics.Length); | ||
var diagnostic1 = diagnostics[0]; | ||
var diagnostic2 = diagnostics[1]; | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic1.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic1.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic1.GetMessage(CultureInfo.InvariantCulture)); | ||
Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic2.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location); | ||
Assert.Equal("Replace with builder.Configuration", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
} |
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
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.
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.
I'd make this one a warning like #42354 and #42356 as this doesn't functionally break an application if it's used.