From 94f72fa987deed906da6e4e8895c241ba8694023 Mon Sep 17 00:00:00 2001 From: Martin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com> Date: Wed, 22 Feb 2023 08:59:53 +0100 Subject: [PATCH] Add reproducer for #6772 (S4507 FP: Error raised on .NET 7 although the debug feature is deactivated) --- .../AspNetCoreMetadataReference.cs | 1 + .../DeliveringDebugFeaturesInProductionTest.cs | 15 +++++++++++++++ .../DeliveringDebugFeaturesInProduction.Net7.cs | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/DeliveringDebugFeaturesInProduction.Net7.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/MetadataReferences/AspNetCoreMetadataReference.cs b/analyzers/tests/SonarAnalyzer.UnitTest/MetadataReferences/AspNetCoreMetadataReference.cs index 3cd9c1d5b99..5c99dc5abba 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/MetadataReferences/AspNetCoreMetadataReference.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/MetadataReferences/AspNetCoreMetadataReference.cs @@ -38,6 +38,7 @@ internal static class AspNetCoreMetadataReference internal static MetadataReference MicrosoftAspNetCoreMvcCore { get; } = Create(typeof(Microsoft.AspNetCore.Mvc.ControllerBase)); internal static MetadataReference MicrosoftAspNetCoreMvcViewFeatures { get; } = Create(typeof(Microsoft.AspNetCore.Mvc.Controller)); internal static MetadataReference MicrosoftAspNetCoreRazorPages { get; } = Create(typeof(Microsoft.AspNetCore.Mvc.RazorPages.PageModel)); + internal static MetadataReference MicrosoftAspNetCoreRouting { get; } = Create(typeof(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder)); internal static MetadataReference MicrosoftAspNetCoreWebHost { get; } = Create(typeof(Microsoft.AspNetCore.WebHost)); internal static MetadataReference MicrosoftExtensionsHostingAbstractions { get; } = Create(typeof(Microsoft.Extensions.Hosting.IHost)); } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/DeliveringDebugFeaturesInProductionTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/DeliveringDebugFeaturesInProductionTest.cs index 89aa9da92dd..3cbcc476f35 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/DeliveringDebugFeaturesInProductionTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/Hotspots/DeliveringDebugFeaturesInProductionTest.cs @@ -56,6 +56,21 @@ public void DeliveringDebugFeaturesInProduction_NetCore3_VB() => .AddReferences(AdditionalReferencesForAspNetCore3AndLater) .Verify(); + [TestMethod] + public void DeliveringDebugFeaturesInProduction_Net7_CS() => + builderCS.AddPaths("DeliveringDebugFeaturesInProduction.Net7.cs") + .WithTopLevelStatements() + .AddReferences(new[] + { + AspNetCoreMetadataReference.MicrosoftAspNetCoreWebHost, + AspNetCoreMetadataReference.MicrosoftAspNetCoreRouting, + AspNetCoreMetadataReference.MicrosoftAspNetCoreDiagnostics, + AspNetCoreMetadataReference.MicrosoftAspNetCoreHttpAbstractions, + AspNetCoreMetadataReference.MicrosoftAspNetCoreHostingAbstractions, + AspNetCoreMetadataReference.MicrosoftExtensionsHostingAbstractions, + }) + .Verify(); + private static IEnumerable AdditionalReferencesForAspNetCore3AndLater => new[] { diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/DeliveringDebugFeaturesInProduction.Net7.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/DeliveringDebugFeaturesInProduction.Net7.cs new file mode 100644 index 00000000000..534fb49bfbc --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/Hotspots/DeliveringDebugFeaturesInProduction.Net7.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Hosting; + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); // Noncompliant FP https://github.com/SonarSource/sonar-dotnet/issues/6772 +} + +app.Run();