|
| 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.Collections.Immutable; |
| 5 | +using Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; |
| 6 | +using Microsoft.AspNetCore.Analyzers.WebApplicationBuilder.Fixers; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Testing; |
| 9 | +using Microsoft.CodeAnalysis.Testing; |
| 10 | +using Microsoft.CodeAnalysis.Testing.Verifiers; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; |
| 13 | + |
| 14 | +public static class CSharpWebApplicationBuilderCodeFixVerifier<TAnalyzer, TCodeFix> |
| 15 | + where TAnalyzer : WebApplicationBuilderAnalyzer, new() |
| 16 | + where TCodeFix : WebApplicationBuilderFixer, new() |
| 17 | +{ |
| 18 | + public static DiagnosticResult Diagnostic(string diagnosticId = null) |
| 19 | + => CSharpCodeFixVerifier<TAnalyzer, TCodeFix, XUnitVerifier>.Diagnostic(diagnosticId); |
| 20 | + |
| 21 | + public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) |
| 22 | + => new DiagnosticResult(descriptor); |
| 23 | + |
| 24 | + public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) |
| 25 | + { |
| 26 | + var test = new CSharpWebApplicationBuilderAnalyzerVerifier<TAnalyzer>.Test { TestCode = source }; |
| 27 | + test.ExpectedDiagnostics.AddRange(expected); |
| 28 | + return test.RunAsync(); |
| 29 | + } |
| 30 | + |
| 31 | + public static Task VerifyCodeFixAsync(string source, string fixedSource) |
| 32 | + => VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); |
| 33 | + |
| 34 | + public static Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) |
| 35 | + => VerifyCodeFixAsync(source, new[] { expected }, fixedSource); |
| 36 | + |
| 37 | + public static Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource) |
| 38 | + => VerifyCodeFixAsync(source, expected, fixedSource, string.Empty); |
| 39 | + |
| 40 | + public static Task VerifyCodeFixAsync(string sources, DiagnosticResult[] expected, string fixedSources, string usageSource = "") |
| 41 | + { |
| 42 | + var test = new WebApplicationBuilderAnalyzerTest |
| 43 | + { |
| 44 | + TestState = |
| 45 | + { |
| 46 | + Sources = { sources, usageSource }, |
| 47 | + // We need to set the output type to an exe to properly |
| 48 | + // support top-level programs in the tests. Otherwise, |
| 49 | + // the test infra will assume we are trying to build a library. |
| 50 | + OutputKind = OutputKind.ConsoleApplication |
| 51 | + }, |
| 52 | + FixedState = |
| 53 | + { |
| 54 | + Sources = { fixedSources, usageSource } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + test.TestState.ExpectedDiagnostics.AddRange(expected); |
| 59 | + return test.RunAsync(); |
| 60 | + } |
| 61 | + |
| 62 | + public class WebApplicationBuilderAnalyzerTest : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier> |
| 63 | + { |
| 64 | + public WebApplicationBuilderAnalyzerTest() |
| 65 | + { |
| 66 | + // We populate the ReferenceAssemblies used in the tests with the locally-built AspNetCore |
| 67 | + // assemblies that are referenced in a minimal app to ensure that there are no reference |
| 68 | + // errors during the build. The value used here should be updated on each TFM change. |
| 69 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net70.AddAssemblies(ImmutableArray.Create( |
| 70 | + TrimAssemblyExtension(typeof(Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions).Assembly.Location), |
| 71 | + TrimAssemblyExtension(typeof(Microsoft.Extensions.Hosting.IHostBuilder).Assembly.Location), |
| 72 | + TrimAssemblyExtension(typeof(Microsoft.Extensions.Configuration.IConfigurationBuilder).Assembly.Location), |
| 73 | + TrimAssemblyExtension(typeof(Microsoft.Extensions.Configuration.JsonConfigurationExtensions).Assembly.Location), |
| 74 | + TrimAssemblyExtension(typeof(Microsoft.Extensions.Configuration.ConfigurationManager).Assembly.Location), |
| 75 | + TrimAssemblyExtension(typeof(Microsoft.Extensions.Hosting.HostingHostBuilderExtensions).Assembly.Location), |
| 76 | + TrimAssemblyExtension(typeof(Microsoft.AspNetCore.Builder.ConfigureHostBuilder).Assembly.Location), |
| 77 | + TrimAssemblyExtension(typeof(Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions).Assembly.Location))); |
| 78 | + |
| 79 | + string TrimAssemblyExtension(string fullPath) => fullPath.Replace(".dll", string.Empty); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments