-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add analyzer to suggest top level route registration #42937
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
6 commits
Select commit
Hold shift + click to select a range
0c41eda
Add analyzer to suggest top level route registration
pedro-camargo11 701a99e
Add tests for other map methods and make the diagnostic descriptor mo…
pedro-camargo11 5b00a0d
Minor changes to the tests
pedro-camargo11 02555db
Merge branch 'main' into ASP0014Analyzer
pedro-camargo11 4aea6d6
Update WebApplicationBuilderAnalyzer.cs
pedro-camargo11 90e87a3
Change operationAnalysisContext to context to match the other updated…
pedro-camargo11 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
229 changes: 229 additions & 0 deletions
229
...yzers/test/WebApplicationBuilder/UseTopLevelRouteRegistrationInsteadOfUseEndpointsTest.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,229 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Analyzer.Testing; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; | ||
public partial class UseTopLevelRouteRegistrationsInsteadOfUseEndpointsTest | ||
{ | ||
private TestDiagnosticAnalyzerRunner Runner { get; } = new(new WebApplicationBuilderAnalyzer()); | ||
|
||
[Fact] | ||
public async Task DoesNotWarnWhenEndpointRegistrationIsTopLevel() | ||
{ | ||
//arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Builder; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app.MapGet(""/"", () => ""Hello World!""); | ||
"; | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source); | ||
|
||
//assert | ||
Assert.Empty(diagnostics); | ||
} | ||
|
||
[Fact] | ||
public async Task DoesNotWarnWhenEnpointRegistrationIsTopLevel_InMain() | ||
{ | ||
//arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Builder; | ||
public static class Program | ||
{ | ||
public static void Main (string[] args) | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app.MapGet(""/"", () => ""Hello World!""); | ||
} | ||
} | ||
"; | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source); | ||
|
||
//assert | ||
Assert.Empty(diagnostics); | ||
} | ||
|
||
[Fact] | ||
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel() | ||
{ | ||
//arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app./*MM*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet(""/"", () => ""Hello World!""); | ||
}); | ||
"); | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
//assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel_OtherMapMethods() | ||
{ | ||
//arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app./*MM1*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet(""/"", () => ""This is a GET""); | ||
}); | ||
app./*MM2*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapPost(""/"", () => ""This is a POST""); | ||
}); | ||
app./*MM3*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapPut(""/"", () => ""This is a PUT""); | ||
}); | ||
app./*MM4*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapDelete(""/"", () => ""This is a DELETE""); | ||
}); | ||
"); | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
//assert | ||
Assert.Equal(4, diagnostics.Length); | ||
var diagnostic1 = diagnostics[0]; | ||
var diagnostic2 = diagnostics[1]; | ||
var diagnostic3 = diagnostics[2]; | ||
var diagnostic4 = diagnostics[3]; | ||
|
||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic1.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic1.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic1.GetMessage(CultureInfo.InvariantCulture)); | ||
|
||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic2.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); | ||
|
||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic3.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM3"], diagnostic3.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic3.GetMessage(CultureInfo.InvariantCulture)); | ||
|
||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic2.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM4"], diagnostic4.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel_InMain_MapControllers() | ||
{ | ||
//arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
public static class Program | ||
{ | ||
public static void Main (string[] args) | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services.AddControllers(); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app./*MM*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllers(); | ||
}); | ||
} | ||
} | ||
"); | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
//assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel_OnDifferentLine_WithRouteParameters() | ||
{ | ||
//arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app. | ||
/*MM*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet(""/users/{userId}/books/{bookId}"", | ||
(int userId, int bookId) => $""The user id is {userId} and book id is {bookId}""); | ||
}); | ||
"); | ||
//act | ||
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); | ||
|
||
//assert | ||
var diagnostic = Assert.Single(diagnostics); | ||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
|
||
[Fact] | ||
public async Task WarnsTwiceWhenEndpointRegistrationIsNotTopLevel_OnDifferentLine() | ||
{ | ||
//arrange | ||
var source = TestSource.Read(@" | ||
using Microsoft.AspNetCore.Builder; | ||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
app.UseRouting(); | ||
app./*MM1*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet(""/"", () => ""Hello World!""); | ||
}); | ||
app./*MM2*/UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet(""/"", () => ""Hello World!""); | ||
}); | ||
"); | ||
//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.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic1.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic1.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic1.GetMessage(CultureInfo.InvariantCulture)); | ||
|
||
Assert.Same(DiagnosticDescriptors.UseTopLevelRouteRegistrationsInsteadOfUseEndpoints, diagnostic2.Descriptor); | ||
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location); | ||
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); | ||
} | ||
} |
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.