|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using Microsoft.AspNetCore.Analyzer.Testing; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; |
| 8 | +public partial class DisallowConfigureAppConfigureHostBuilderTest |
| 9 | +{ |
| 10 | + private TestDiagnosticAnalyzerRunner Runner { get; } = new(new WebApplicationBuilderAnalyzer()); |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task ConfigurationBuilderRunsWithoutDiagnostic() |
| 14 | + { |
| 15 | + // Arrange |
| 16 | + var source = @" |
| 17 | +using Microsoft.AspNetCore.Builder; |
| 18 | +using Microsoft.Extensions.Configuration; |
| 19 | +var builder = WebApplication.CreateBuilder(args); |
| 20 | +builder.Configuration.AddJsonFile(""foo.json"", optional: true); |
| 21 | +"; |
| 22 | + // Act |
| 23 | + var diagnostic = await Runner.GetDiagnosticsAsync(source); |
| 24 | + |
| 25 | + // Assert |
| 26 | + Assert.Empty(diagnostic); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task ConfigureAppHostBuilderProducesDiagnostic() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var source = TestSource.Read(@" |
| 34 | +using Microsoft.AspNetCore.Builder; |
| 35 | +using Microsoft.Extensions.Hosting; |
| 36 | +using Microsoft.Extensions.Configuration; |
| 37 | +var builder = WebApplication.CreateBuilder(args); |
| 38 | +builder.Host./*MM*/ConfigureAppConfiguration(builder => |
| 39 | +{ |
| 40 | + builder.AddJsonFile(""foo.json"", optional: true); |
| 41 | +}); |
| 42 | +"); |
| 43 | + |
| 44 | + // Act |
| 45 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 46 | + |
| 47 | + // Assert |
| 48 | + var diagnostic = Assert.Single(diagnostics); |
| 49 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 50 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 51 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task ConfigureHostHostBuilderProducesDiagnostic() |
| 56 | + { |
| 57 | + // Arrange |
| 58 | + var source = TestSource.Read(@" |
| 59 | +using Microsoft.AspNetCore.Builder; |
| 60 | +using Microsoft.Extensions.Configuration; |
| 61 | +var builder = WebApplication.CreateBuilder(args); |
| 62 | +builder.Host./*MM*/ConfigureHostConfiguration(builder => |
| 63 | +{ |
| 64 | + builder.AddJsonFile(""foo.json"", optional: true); |
| 65 | +}); |
| 66 | +"); |
| 67 | + |
| 68 | + // Act |
| 69 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 70 | + |
| 71 | + // Assert |
| 72 | + var diagnostic = Assert.Single(diagnostics); |
| 73 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 74 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 75 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureHostConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task ConfigureAppWebHostBuilderProducesDiagnostic() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + var source = TestSource.Read(@" |
| 83 | +using Microsoft.AspNetCore.Builder; |
| 84 | +using Microsoft.AspNetCore.Hosting; |
| 85 | +using Microsoft.Extensions.Configuration; |
| 86 | +var builder = WebApplication.CreateBuilder(args); |
| 87 | +builder.WebHost./*MM*/ConfigureAppConfiguration(builder => |
| 88 | +{ |
| 89 | + builder.AddJsonFile(""foo.json"", optional: true); |
| 90 | +}); |
| 91 | +"); |
| 92 | + |
| 93 | + // Act |
| 94 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 95 | + |
| 96 | + // Assert |
| 97 | + var diagnostic = Assert.Single(diagnostics); |
| 98 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 99 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 100 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public async Task ConfigureAppWebHostBuilderWithContextProducesDiagnostic() |
| 105 | + { |
| 106 | + // Arrange |
| 107 | + var source = TestSource.Read(@" |
| 108 | +using Microsoft.AspNetCore.Builder; |
| 109 | +using Microsoft.AspNetCore.Hosting; |
| 110 | +using Microsoft.Extensions.Configuration; |
| 111 | +var builder = WebApplication.CreateBuilder(args); |
| 112 | +builder.WebHost./*MM*/ConfigureAppConfiguration((context, webHostBuilder) => { }); |
| 113 | +"); |
| 114 | + |
| 115 | + // Act |
| 116 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 117 | + |
| 118 | + // Assert |
| 119 | + var diagnostic = Assert.Single(diagnostics); |
| 120 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 121 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 122 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 123 | + } |
| 124 | + [Fact] |
| 125 | + public async Task ConfigureAppWebHostBuilderProducesDiagnosticInMain() |
| 126 | + { |
| 127 | + // Arrange |
| 128 | + var source = TestSource.Read(@" |
| 129 | +using Microsoft.AspNetCore.Builder; |
| 130 | +using Microsoft.AspNetCore.Hosting; |
| 131 | +using Microsoft.Extensions.Configuration; |
| 132 | +public static class Test |
| 133 | +{ |
| 134 | + public static void Main(string[]args) { |
| 135 | + var builder = WebApplication.CreateBuilder(args); |
| 136 | + builder.WebHost./*MM*/ConfigureAppConfiguration(builder => { }); |
| 137 | +} |
| 138 | +} |
| 139 | +"); |
| 140 | + |
| 141 | + // Act |
| 142 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 143 | + |
| 144 | + // Assert |
| 145 | + var diagnostic = Assert.Single(diagnostics); |
| 146 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 147 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 148 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 149 | + } |
| 150 | + [Fact] |
| 151 | + public async Task ConfigureAppWebHostOnBuilderProducesDiagnosticInMain() |
| 152 | + { |
| 153 | + // Arrange |
| 154 | + var source = TestSource.Read(@" |
| 155 | +using Microsoft.AspNetCore.Builder; |
| 156 | +using Microsoft.AspNetCore.Hosting; |
| 157 | +using Microsoft.Extensions.Configuration; |
| 158 | +public static class Test |
| 159 | +{ |
| 160 | + public static void Main(string[]args) { |
| 161 | + var builder = WebApplication.CreateBuilder(args); |
| 162 | + var webhost = builder.WebHost; |
| 163 | + webhost./*MM*/ConfigureAppConfiguration(builder => { }); |
| 164 | +} |
| 165 | +} |
| 166 | +"); |
| 167 | + |
| 168 | + // Act |
| 169 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 170 | + |
| 171 | + // Assert |
| 172 | + var diagnostic = Assert.Single(diagnostics); |
| 173 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 174 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 175 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 176 | + } |
| 177 | + [Fact] |
| 178 | + public async Task TwoInvocationsProduceTwoDiagnostic() |
| 179 | + { |
| 180 | + // Arrange |
| 181 | + var source = TestSource.Read(@" |
| 182 | +using Microsoft.AspNetCore.Builder; |
| 183 | +using Microsoft.AspNetCore.Hosting; |
| 184 | +using Microsoft.Extensions.Configuration; |
| 185 | +var builder = WebApplication.CreateBuilder(args); |
| 186 | +builder.Host./*MM1*/ConfigureHostConfiguration(builder => |
| 187 | +{ |
| 188 | + builder.AddJsonFile(""foo.json"", optional: true); |
| 189 | +}); |
| 190 | +builder.WebHost./*MM2*/ConfigureAppConfiguration(builder => |
| 191 | +{ |
| 192 | + builder.AddJsonFile(""foo.json"", optional: true); |
| 193 | +}); |
| 194 | +"); |
| 195 | + |
| 196 | + // Act |
| 197 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 198 | + |
| 199 | + // Assert |
| 200 | + Assert.Equal(2, diagnostics.Length); |
| 201 | + var diagnostic1 = diagnostics[0]; |
| 202 | + var diagnostic2 = diagnostics[1]; |
| 203 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic1.Descriptor); |
| 204 | + AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic1.Location); |
| 205 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureHostConfiguration", diagnostic1.GetMessage(CultureInfo.InvariantCulture)); |
| 206 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic2.Descriptor); |
| 207 | + AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location); |
| 208 | + Assert.Equal("Suggest using WebApplicationBuilder.Configuration instead of ConfigureAppConfiguration", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); |
| 209 | + } |
| 210 | +} |
0 commit comments