|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System; |
5 |
| -using System.Collections.Generic; |
6 |
| -using System.Linq; |
7 |
| -using System.Text; |
8 |
| -using System.Threading.Tasks; |
9 |
| -using Microsoft.CodeAnalysis.Testing; |
10 |
| -using VerifyCS = Microsoft.AspNetCore.Analyzers.RouteHandlers.CSharpRouteHandlerCodeFixVerifier< |
11 |
| - Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer, |
12 |
| - Microsoft.AspNetCore.Analyzers.RouteHandlers.Fixers.DetectMismatchedParameterOptionalityFixer>; |
| 4 | +using System.Globalization; |
| 5 | +using Microsoft.AspNetCore.Analyzer.Testing; |
13 | 6 |
|
14 | 7 | namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder;
|
15 | 8 | public partial class DisallowConfigureAppConfigureHostBuilderTest
|
16 | 9 | {
|
17 | 10 | private TestDiagnosticAnalyzerRunner Runner { get; } = new(new WebApplicationBuilderAnalyzer());
|
18 |
| - /** |
19 |
| - * Verify that the correct code produces no diagnostic |
20 |
| - */ |
| 11 | + |
21 | 12 | [Fact]
|
22 | 13 | public async Task ConfigurationBuilderRunsWithoutDiagnostic()
|
23 | 14 | {
|
24 | 15 | // Arrange
|
25 | 16 | var source = @"
|
26 | 17 | using Microsoft.AspNetCore.Builder;
|
27 |
| -using Microsoft.Extensions.Hosting; |
| 18 | +using Microsoft.Extensions.Configuration; |
28 | 19 | var builder = WebApplication.CreateBuilder(args);
|
29 |
| -builder.Configuration.AddJSonFile(fileName, optional: true); |
| 20 | +builder.Configuration.AddJsonFile(""foo.json"", optional: true); |
30 | 21 | ";
|
31 | 22 | // Act
|
32 | 23 | var diagnostic = await Runner.GetDiagnosticsAsync(source);
|
33 | 24 |
|
34 | 25 | // Assert
|
35 | 26 | Assert.Empty(diagnostic);
|
36 | 27 | }
|
37 |
| - /** |
38 |
| - * Verify the fixed code and the diagnostic for builder.Host.ConfigureAppConfiguration |
39 |
| - */ |
| 28 | + |
40 | 29 | [Fact]
|
41 | 30 | public async Task ConfigureAppHostBuilderProducesDiagnostic()
|
42 | 31 | {
|
43 | 32 | // Arrange
|
44 |
| - var source = @" |
| 33 | + var source = TestSource.Read(@" |
45 | 34 | using Microsoft.AspNetCore.Builder;
|
46 | 35 | using Microsoft.Extensions.Hosting;
|
| 36 | +using Microsoft.Extensions.Configuration; |
47 | 37 | var builder = WebApplication.CreateBuilder(args);
|
48 |
| -builder.Host.ConfigureAppConfiguration(builder => |
| 38 | +builder.Host./*MM*/ConfigureAppConfiguration(builder => |
49 | 39 | {
|
50 |
| - builder.AddJsonFile(fileName, optional: true); |
| 40 | + builder.AddJsonFile(""foo.json"", optional: true); |
51 | 41 | });
|
52 |
| -"; |
53 |
| - var fixedSource = @" |
54 |
| -using Microsoft.AspNetCore.Builder; |
55 |
| -using Microsoft.Extensions.Hosting; |
56 |
| -var builder = WebApplication.CreateBuilder(args); |
57 |
| -builder.Configuration.AddJsonFile(fileName, optional: true); |
58 |
| -"; |
| 42 | +"); |
| 43 | + |
59 | 44 | // Act
|
60 |
| - var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder); |
| 45 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
61 | 46 |
|
62 | 47 | // Assert
|
63 |
| - await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource); |
| 48 | + var diagnostic = Assert.Single(diagnostics); |
| 49 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 50 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 51 | + Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
64 | 52 | }
|
65 | 53 |
|
66 |
| - /** |
67 |
| - * Verify the fixed code and the diagnostic for builder.Host.ConfigureHostConfiguration |
68 |
| - */ |
69 | 54 | [Fact]
|
70 | 55 | public async Task ConfigureHostHostBuilderProducesDiagnostic()
|
71 | 56 | {
|
72 | 57 | // Arrange
|
73 |
| - var source = @" |
| 58 | + var source = TestSource.Read(@" |
74 | 59 | using Microsoft.AspNetCore.Builder;
|
75 |
| -using Microsoft.Extensions.Hosting; |
| 60 | +using Microsoft.Extensions.Configuration; |
76 | 61 | var builder = WebApplication.CreateBuilder(args);
|
77 |
| -builder.Host.ConfigureHostConfiguration(builder => |
| 62 | +builder.Host./*MM*/ConfigureHostConfiguration(builder => |
78 | 63 | {
|
79 |
| - builder.AddJsonFile(fileName, optional: true); |
| 64 | + builder.AddJsonFile(""foo.json"", optional: true); |
80 | 65 | });
|
81 |
| -"; |
82 |
| - var fixedSource = @" |
83 |
| -using Microsoft.AspNetCore.Builder; |
84 |
| -using Microsoft.Extensions.Hosting; |
85 |
| -var builder = WebApplication.CreateBuilder(args); |
86 |
| -builder.Configuration.AddJsonFile(fileName, optional: true); |
87 |
| -"; |
| 66 | +"); |
| 67 | + |
88 | 68 | // Act
|
89 |
| - var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder); |
| 69 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
90 | 70 |
|
91 | 71 | // Assert
|
92 |
| - await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource); |
| 72 | + var diagnostic = Assert.Single(diagnostics); |
| 73 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic.Descriptor); |
| 74 | + AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location); |
| 75 | + Assert.Equal("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
93 | 76 | }
|
94 | 77 |
|
95 |
| - /** |
96 |
| - * Verify the fixed code and the diagnostic for builder.WebHost.ConfigureAppConfiguration |
97 |
| - */ |
98 | 78 | [Fact]
|
99 | 79 | public async Task ConfigureAppWebHostBuilderProducesDiagnostic()
|
100 | 80 | {
|
101 | 81 | // Arrange
|
102 |
| - var source = @" |
| 82 | + var source = TestSource.Read(@" |
103 | 83 | using Microsoft.AspNetCore.Builder;
|
104 |
| -using Microsoft.Extensions.Hosting; |
105 | 84 | using Microsoft.AspNetCore.Hosting;
|
106 |
| -using Microsoft.Extensions.DependencyInjection; |
| 85 | +using Microsoft.Extensions.Configuration; |
107 | 86 | var builder = WebApplication.CreateBuilder(args);
|
108 |
| -builder.WebHost.ConfigureAppConfiguration(builder => |
| 87 | +builder.WebHost./*MM*/ConfigureAppConfiguration(builder => |
109 | 88 | {
|
110 |
| - builder.AddJsonFile(fileName, optional: true); |
| 89 | + builder.AddJsonFile(""foo.json"", optional: true); |
111 | 90 | });
|
112 |
| -"; |
113 |
| - var fixedSource = @" |
| 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("Replace with builder.Configuration", diagnostic.GetMessage(CultureInfo.InvariantCulture)); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public async Task ConfigureAppWebHostBuilderWithContextProducesDiagnostic() |
| 105 | + { |
| 106 | + // Arrange |
| 107 | + var source = TestSource.Read(@" |
114 | 108 | using Microsoft.AspNetCore.Builder;
|
115 |
| -using Microsoft.Extensions.Hosting; |
116 | 109 | using Microsoft.AspNetCore.Hosting;
|
117 |
| -using Microsoft.Extensions.DependencyInjection; |
| 110 | +using Microsoft.Extensions.Configuration; |
118 | 111 | var builder = WebApplication.CreateBuilder(args);
|
119 |
| -builder.Configuration.AddJsonFile(fileName, optional: true); |
120 |
| -"; |
| 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("Replace with builder.Configuration", 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("Replace with builder.Configuration", 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("Replace with builder.Configuration", 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 | + |
121 | 196 | // Act
|
122 |
| - var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder); |
| 197 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
123 | 198 |
|
124 | 199 | // Assert
|
125 |
| - await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource); |
| 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("Replace with builder.Configuration", diagnostic1.GetMessage(CultureInfo.InvariantCulture)); |
| 206 | + Assert.Same(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder, diagnostic2.Descriptor); |
| 207 | + AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location); |
| 208 | + Assert.Equal("Replace with builder.Configuration", diagnostic2.GetMessage(CultureInfo.InvariantCulture)); |
126 | 209 | }
|
127 | 210 | }
|
0 commit comments