From f52c9c0f97fac7d355becd11bfa12413f5505cea Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 4 May 2018 08:45:48 -0700 Subject: [PATCH] Update analyzer tests to use Microsoft.AspNetCore.Analyzer.Testing --- build/dependencies.props | 141 ++++++------ korebuild-lock.txt | 4 +- .../ActionsMustNotBeAsyncVoidFacts.cs | 47 ++-- ...equireExplicitModelValidationCheckFacts.cs | 92 ++++---- .../ApiActionsAreAttributeRoutedFacts.cs | 26 ++- ...ApiActionsShouldUseActionResultOfTFacts.cs | 36 +-- .../Infrastructure/AnalyzerTestBase.cs | 8 +- ...ore.Mvc.Analyzers.Experimental.Test.csproj | 4 +- .../AvoidHtmlPartialAnalyzerTest.cs | 213 +++++------------- .../Infrastructure/Assert.cs | 194 ---------------- .../Infrastructure/DiagnosticResult.cs | 75 ------ .../MvcDiagnosticAnalyzerRunner.cs | 26 +++ .../Infrastructure/MvcTestSource.cs | 34 +++ ...osoft.AspNetCore.Mvc.Analyzers.Test.csproj | 1 + ...gnosticsAreReturned_ForUseOfHtmlPartial.cs | 0 ...Returned_ForUseOfHtmlPartial_InSections.cs | 0 ...eOfHtmlPartial_WithAdditionalParameters.cs | 0 ...osticsAreReturned_ForUseOfRenderPartial.cs | 0 ...turned_ForUseOfRenderPartial_InSections.cs | 0 ...fRenderPartial_WithAdditionalParameters.cs | 0 ...sticsAreReturned_ForNonUseOfHtmlPartial.cs | 0 ...icsAreReturned_ForUseOfHtmlPartialAsync.cs | 0 ...sAreReturned_ForUseOfRenderPartialAsync.cs | 0 23 files changed, 298 insertions(+), 603 deletions(-) rename test/{Microsoft.AspNetCore.Mvc.Analyzers.Test => Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test}/Infrastructure/AnalyzerTestBase.cs (95%) delete mode 100644 test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/Assert.cs delete mode 100644 test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/DiagnosticResult.cs create mode 100644 test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcDiagnosticAnalyzerRunner.cs create mode 100644 test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcTestSource.cs rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfHtmlPartial.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfRenderPartial.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfRenderPartial_InSections.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync.cs (100%) rename test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/{ => AvoidHtmlPartialAnalyzerTest}/NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync.cs (100%) diff --git a/build/dependencies.props b/build/dependencies.props index 92dd9d1ee4..240fdc62c6 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -5,81 +5,82 @@ 0.9.9 0.10.13 - 2.2.0-preview1-17047 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 + 2.2.0-preview1-17048 + 2.2.0-a-preview1-a2-16496 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 5.2.4 - 2.8.0 - 2.8.0 - 2.2.0-preview1-34135 + 2.8.0-beta3 + 2.8.0-beta3 + 2.2.0-preview1-34136 1.7.0 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 2.2.0-preview1-26424-04 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 2.0.0 2.2.0-preview1-26424-04 - 2.2.0-preview1-34135 - 2.2.0-preview1-34135 + 2.2.0-preview1-34136 + 2.2.0-preview1-34136 15.6.1 4.7.49 2.0.3 diff --git a/korebuild-lock.txt b/korebuild-lock.txt index a16d4b9ee4..2573a03995 100644 --- a/korebuild-lock.txt +++ b/korebuild-lock.txt @@ -1,2 +1,2 @@ -version:2.2.0-preview1-17047 -commithash:e1957b52ddc8b62bd39c5c400322fccb5364624c +version:2.2.0-preview1-17048 +commithash:de14a0ee5fb48508ee8a29c14280a2928f8dabf8 diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ActionsMustNotBeAsyncVoidFacts.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ActionsMustNotBeAsyncVoidFacts.cs index 1d536be95a..c7432399f1 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ActionsMustNotBeAsyncVoidFacts.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ActionsMustNotBeAsyncVoidFacts.cs @@ -1,7 +1,9 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; @@ -12,6 +14,8 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers { public class ActionsMustNotBeAsyncVoidFacts : AnalyzerTestBase { + private static DiagnosticDescriptor DiagnosticDescriptor = DiagnosticDescriptors.MVC7003_ActionsMustNotBeAsyncVoid; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ActionsMustNotBeAsyncVoidAnalyzer(); @@ -57,13 +61,7 @@ public class UserViewModel public async Task DiagnosticsAreReturned_WhenMethodIsAControllerAction() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7003", - Message = "Controller actions must not have async void signature.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 7, 18) } - }; + var location = new DiagnosticLocation("Test.cs", 7, 18); var test = @" using Microsoft.AspNetCore.Mvc; @@ -92,7 +90,7 @@ public class HomeController : Controller // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(location, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } @@ -101,13 +99,7 @@ public class HomeController : Controller public async Task DiagnosticsAreReturned_WhenActionMethodIsExpressionBodied() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7003", - Message = "Controller actions must not have async void signature.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 7, 18) } - }; + var location = new DiagnosticLocation("Test.cs", 7, 18); var test = @" using Microsoft.AspNetCore.Mvc; @@ -130,7 +122,7 @@ public class HomeController : Controller // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(location, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } @@ -139,13 +131,7 @@ public class HomeController : Controller public async Task CodeFix_ProducesFullyQualifiedNamespaces() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7003", - Message = "Controller actions must not have async void signature.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 6, 18) } - }; + var location = new DiagnosticLocation("Test.cs", 6, 18); var test = @" using Microsoft.AspNetCore.Mvc; @@ -166,9 +152,22 @@ public class HomeController : Controller // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(location, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } + + private void AssertDiagnostic(DiagnosticLocation expectedLocation, Diagnostic[] actualDiagnostics) + { + // Assert + Assert.Collection( + actualDiagnostics, + diagnostic => + { + Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); + Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); + AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); + }); + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApActionsDoNotRequireExplicitModelValidationCheckFacts.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApActionsDoNotRequireExplicitModelValidationCheckFacts.cs index d74c584349..f2954b3047 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApActionsDoNotRequireExplicitModelValidationCheckFacts.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApActionsDoNotRequireExplicitModelValidationCheckFacts.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; @@ -12,6 +13,8 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers { public class ApiActionsDoNotRequireExplicitModelValidationCheckFacts : AnalyzerTestBase { + private static DiagnosticDescriptor DiagnosticDescriptor = DiagnosticDescriptors.MVC7001_ApiActionsHaveBadModelStateFilter; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ApiActionsDoNotRequireExplicitModelValidationCheckAnalyzer(); @@ -199,48 +202,11 @@ public class PetController : ControllerBase return VerifyAsync(test); } - private async Task VerifyAsync(string test) - { - // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7001", - Message = "Actions on types annotated with ApiControllerAttribute do not require explicit ModelState validity check.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 9, 9) } - }; - var expectedFix = -@" -using Microsoft.AspNetCore.Mvc; - -[ApiController] -public class PetController : ControllerBase -{ - public IActionResult GetPetId() - { - return Ok(); - } -}"; - var project = CreateProject(test); - - // Act & Assert - var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); - var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); - Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); - } - [Fact] public async Task DiagnosticsAndCodeFixes_WhenModelStateIsInElseIf() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7001", - Message = "Actions on types annotated with ApiControllerAttribute do not require explicit ModelState validity check.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 13, 9) } - }; + var expectedLocation = new DiagnosticLocation("Test.cs", 13, 9); var test = @" @@ -284,7 +250,7 @@ public class PetController : ControllerBase // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(expectedLocation, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } @@ -293,13 +259,7 @@ public class PetController : ControllerBase public async Task DiagnosticsAndCodeFixes_WhenModelStateIsInNestedBlock() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7001", - Message = "Actions on types annotated with ApiControllerAttribute do not require explicit ModelState validity check.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 15, 13) } - }; + var expectedLocation = new DiagnosticLocation("Test.cs", 15, 13); var test = @" @@ -353,9 +313,47 @@ public class PetController : ControllerBase // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(expectedLocation, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } + + private async Task VerifyAsync(string test) + { + // Arrange + var expectedLocation = new DiagnosticLocation("Test.cs", 9, 9); + var expectedFix = +@" +using Microsoft.AspNetCore.Mvc; + +[ApiController] +public class PetController : ControllerBase +{ + public IActionResult GetPetId() + { + return Ok(); + } +}"; + var project = CreateProject(test); + + // Act & Assert + var actualDiagnostics = await GetDiagnosticAsync(project); + AssertDiagnostic(expectedLocation, actualDiagnostics); + var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); + Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); + } + + private void AssertDiagnostic(DiagnosticLocation expectedLocation, Diagnostic[] actualDiagnostics) + { + // Assert + Assert.Collection( + actualDiagnostics, + diagnostic => + { + Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); + Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); + AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); + }); + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsAreAttributeRoutedFacts.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsAreAttributeRoutedFacts.cs index c9225692aa..9042db0c0d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsAreAttributeRoutedFacts.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsAreAttributeRoutedFacts.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; @@ -12,6 +13,8 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers { public class ApiActionsAreAttributeRoutedFacts : AnalyzerTestBase { + private static DiagnosticDescriptor DiagnosticDescriptor = DiagnosticDescriptors.MVC7000_ApiActionsMustBeAttributeRouted; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ApiActionsAreAttributeRoutedAnalyzer(); @@ -128,13 +131,7 @@ public class PetController : Controller public async Task DiagnosticsAndCodeFixes_WhenApiControllerActionDoesNotHaveAttribute() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7000", - Message = "Actions on types annotated with ApiControllerAttribute must be attribute routed.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 8, 16) } - }; + var expectedLocation = new DiagnosticLocation("Test.cs", 8, 16); var test = @" using Microsoft.AspNetCore.Mvc; @@ -160,7 +157,7 @@ public class PetController : Controller // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(expectedLocation, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } @@ -290,5 +287,18 @@ public class PetController var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics, codeFixIndex: 3); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } + + private void AssertDiagnostic(DiagnosticLocation expectedLocation, Diagnostic[] actualDiagnostics) + { + // Assert + Assert.Collection( + actualDiagnostics, + diagnostic => + { + Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); + Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); + AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); + }); + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsShouldUseActionResultOfTFacts.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsShouldUseActionResultOfTFacts.cs index 5dc16ac2fe..ef939ffd3c 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsShouldUseActionResultOfTFacts.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/ApiActionsShouldUseActionResultOfTFacts.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; @@ -12,6 +13,8 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers { public class ApiActionsShouldUseActionResultOfTFacts : AnalyzerTestBase { + private static DiagnosticDescriptor DiagnosticDescriptor = DiagnosticDescriptors.MVC7002_ApiActionsShouldReturnActionResultOf; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ApiActionsShouldUseActionResultOfTAnalyzer(); @@ -156,13 +159,7 @@ public class PetController: ControllerBase public async Task DiagnosticsAreReturned_WhenActionsReturnIActionResult() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7002", - Message = "Actions on types annotated with ApiControllerAttribute should return ActionResult.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 9, 12) } - }; + var expectedLocation = new DiagnosticLocation("Test.cs", 9, 12); var test = @" using Microsoft.AspNetCore.Mvc; @@ -195,7 +192,7 @@ public class PetController: ControllerBase // Act var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(expectedLocation, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); @@ -205,13 +202,7 @@ public class PetController: ControllerBase public async Task DiagnosticsAreReturned_WhenActionReturnsAsyncIActionResult() { // Arrange - var expectedDiagnostic = new DiagnosticResult - { - Id = "MVC7002", - Message = "Actions on types annotated with ApiControllerAttribute should return ActionResult.", - Severity = DiagnosticSeverity.Warning, - Locations = new[] { new DiagnosticResultLocation("Test.cs", 8, 18) } - }; + var expectedLocation = new DiagnosticLocation("Test.cs", 8, 18); var test = @" @@ -248,10 +239,23 @@ public class Pet {}"; // Act & Assert var actualDiagnostics = await GetDiagnosticAsync(project); - Assert.DiagnosticsEqual(new[] { expectedDiagnostic }, actualDiagnostics); + AssertDiagnostic(expectedLocation, actualDiagnostics); var actualFix = await ApplyCodeFixAsync(project, actualDiagnostics); Assert.Equal(expectedFix, actualFix, ignoreLineEndingDifferences: true); } + + private void AssertDiagnostic(DiagnosticLocation expectedLocation, Diagnostic[] actualDiagnostics) + { + // Assert + Assert.Collection( + actualDiagnostics, + diagnostic => + { + Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); + Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); + AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); + }); + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/AnalyzerTestBase.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Infrastructure/AnalyzerTestBase.cs similarity index 95% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/AnalyzerTestBase.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Infrastructure/AnalyzerTestBase.cs index 73f2cae3f6..1351b24c94 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/AnalyzerTestBase.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Infrastructure/AnalyzerTestBase.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Testing; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; @@ -17,6 +18,7 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Text; using Microsoft.Extensions.DependencyModel; +using Xunit; namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure { @@ -30,9 +32,9 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure protected virtual CodeFixProvider CodeFixProvider { get; } - public IDictionary MarkerLocations { get; } = new Dictionary(); + public IDictionary MarkerLocations { get; } = new Dictionary(); - public DiagnosticResultLocation? DefaultMarkerLocation { get; private set; } + public DiagnosticLocation DefaultMarkerLocation { get; private set; } protected Project CreateProjectFromFile([CallerMemberName] string fileName = "") { @@ -57,7 +59,7 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure { var markerEndIndex = line.IndexOf(MarkerEnd, markerStartIndex, StringComparison.Ordinal); var markerName = line.Substring(markerStartIndex + 2, markerEndIndex - markerStartIndex - 2); - var resultLocation = new DiagnosticResultLocation(i + 1, markerStartIndex + 1); ; + var resultLocation = new DiagnosticLocation(i + 1, markerStartIndex + 1); ; if (DefaultMarkerLocation == null) { diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test.csproj b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test.csproj index b5d9571c75..86f4228e01 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test.csproj +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test/Microsoft.AspNetCore.Mvc.Analyzers.Experimental.Test.csproj @@ -1,4 +1,4 @@ - + $(StandardTestTfms) @@ -7,13 +7,13 @@ - + diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/AvoidHtmlPartialAnalyzerTest.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/AvoidHtmlPartialAnalyzerTest.cs index 1dc47a4110..b6de06c5fa 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/AvoidHtmlPartialAnalyzerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/AvoidHtmlPartialAnalyzerTest.cs @@ -1,68 +1,77 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Runtime.CompilerServices; using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; using Xunit; namespace Microsoft.AspNetCore.Mvc.Analyzers { - public class AvoidHtmlPartialAnalyzerTest : AnalyzerTestBase + public class AvoidHtmlPartialAnalyzerTest { private static DiagnosticDescriptor DiagnosticDescriptor = DiagnosticDescriptors.MVC1000_HtmlHelperPartialShouldBeAvoided; - protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new AvoidHtmlPartialAnalyzer(); + private MvcDiagnosticAnalyzerRunner Executor { get; } = new MvcDiagnosticAnalyzerRunner(new AvoidHtmlPartialAnalyzer()); [Fact] - public async Task NoDiagnosticsAreReturned_FoEmptyScenarios() - { - // Arrange - var project = CreateProject(source: string.Empty); + public Task NoDiagnosticsAreReturned_FoEmptyScenarios() + => VerifyNoDiagnosticsAreReturned(source: string.Empty); + [Fact] + public Task NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial() + => VerifyNoDiagnosticsAreReturned(ReadTestSource().Source); + + [Fact] + public Task NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync() + => VerifyNoDiagnosticsAreReturned(ReadTestSource().Source); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfHtmlPartial() + => VerifyDefault(ReadTestSource()); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters() + => VerifyDefault(ReadTestSource()); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections() + => VerifyDefault(ReadTestSource()); + + [Fact] + public Task NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync() + => VerifyNoDiagnosticsAreReturned(ReadTestSource().Source); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfRenderPartial() + => VerifyDefault(ReadTestSource()); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters() + => VerifyDefault(ReadTestSource()); + + [Fact] + public Task DiagnosticsAreReturned_ForUseOfRenderPartial_InSections() + => VerifyDefault(ReadTestSource()); + + private async Task VerifyNoDiagnosticsAreReturned(string source) + { // Act - var result = await GetDiagnosticAsync(project); + var result = await Executor.GetDiagnosticsAsync(source); // Assert Assert.Empty(result); } - [Fact] - public async Task NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial() + private async Task VerifyDefault(TestSource testSource) { // Arrange - var project = CreateProjectFromFile(); + var expectedLocation = testSource.DefaultMarkerLocation; // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Empty(result); - } - - [Fact] - public async Task NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync() - { - // Arrange - var project = CreateProjectFromFile(); - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Empty(result); - } - - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfHtmlPartial() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); + var result = await Executor.GetDiagnosticsAsync(testSource.Source); // Assert Assert.Collection( @@ -72,131 +81,11 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); + AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); }); } - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Collection( - result, - diagnostic => - { - - Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); - Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); - }); - } - - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Collection( - result, - diagnostic => - { - - Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); - Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); - }); - } - - [Fact] - public async Task NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync() - { - // Arrange - var project = CreateProjectFromFile(); - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Empty(result); - } - - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfRenderPartial() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Collection( - result, - diagnostic => - { - - Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); - Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); - }); - } - - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Collection( - result, - diagnostic => - { - - Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); - Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); - }); - } - - [Fact] - public async Task DiagnosticsAreReturned_ForUseOfRenderPartial_InSections() - { - // Arrange - var project = CreateProjectFromFile(); - var expectedLocation = DefaultMarkerLocation.Value; - - // Act - var result = await GetDiagnosticAsync(project); - - // Assert - Assert.Collection( - result, - diagnostic => - { - - Assert.Equal(DiagnosticDescriptor.Id, diagnostic.Id); - Assert.Same(DiagnosticDescriptor, diagnostic.Descriptor); - Assert.DiagnosticLocation(expectedLocation, diagnostic.Location); - }); - } + private static TestSource ReadTestSource([CallerMemberName] string testMethod = "") => + MvcTestSource.Read(nameof(AvoidHtmlPartialAnalyzerTest), testMethod); } } diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/Assert.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/Assert.cs deleted file mode 100644 index ea57f2f5b8..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/Assert.cs +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Simplification; - -namespace Microsoft.AspNetCore.Mvc.Analyzers -{ - internal class Assert : Xunit.Assert - { - public static void DiagnosticsEqual(IEnumerable expected, IEnumerable actual) - { - var expectedCount = expected.Count(); - var actualCount = actual.Count(); - - if (expectedCount != actualCount) - { - throw new DiagnosticsAssertException( - expected, - actual, - $"Mismatch between number of diagnostics returned, expected \"{expectedCount}\" actual \"{actualCount}."); - } - - foreach (var (expectedItem, actualItem) in expected.Zip(actual, (a, b) => (a, b))) - { - if (expectedItem.Line == -1 && expectedItem.Column == -1) - { - if (actualItem.Location != Location.None) - { - throw new DiagnosticAssertException( - expectedItem, - actualItem, - $"Expected: A project diagnostic with no location. Actual {actualItem.Location}."); - } - } - else - { - VerifyLocation(expectedItem, actualItem); - } - - if (actualItem.Id != expectedItem.Id) - { - throw new DiagnosticAssertException( - expectedItem, - actualItem, - $"Expected: Expected id: {expectedItem.Id}. Actual id: {actualItem.Id}."); - } - - if (actualItem.Severity != expectedItem.Severity) - { - throw new DiagnosticAssertException( - expectedItem, - actualItem, - $"Expected: Expected severity: {expectedItem.Severity}. Actual severity: {actualItem.Severity}."); - } - - if (actualItem.GetMessage() != expectedItem.Message) - { - throw new DiagnosticAssertException( - expectedItem, - actualItem, - $"Expected: Expected message: {expectedItem.Message}. Actual message: {actualItem.GetMessage()}."); - } - } - } - - private static void VerifyLocation(DiagnosticResult expected, Diagnostic actual) - { - if (expected.Locations.Length == 0) - { - return; - } - - var expectedLocation = expected.Locations[0]; - Assert.DiagnosticLocation(expectedLocation, actual.Location); - - } - - public static void DiagnosticLocation(DiagnosticResultLocation expected, Location actual) - { - var actualSpan = actual.GetLineSpan(); - var actualLinePosition = actualSpan.StartLinePosition; - - // Only check line position if there is an actual line in the real diagnostic - if (actualLinePosition.Line > 0) - { - if (actualLinePosition.Line + 1 != expected.Line) - { - throw new DiagnosticLocationAssertException( - expected, - actual, - $"Expected diagnostic to be on line \"{expected.Line}\" was actually on line \"{actualLinePosition.Line + 1}\""); - } - } - - // Only check column position if there is an actual column position in the real diagnostic - if (actualLinePosition.Character > 0) - { - if (actualLinePosition.Character + 1 != expected.Column) - { - throw new DiagnosticLocationAssertException( - expected, - actual, - $"Expected diagnostic to start at column \"{expected.Column}\" was actually on line \"{actualLinePosition.Character + 1}\""); - } - } - } - - private static string FormatDiagnostics(IEnumerable diagnostics) - { - return string.Join(Environment.NewLine, diagnostics.Select(FormatDiagnostic)); - } - - private static string FormatDiagnostic(Diagnostic diagnostic) - { - var builder = new StringBuilder(); - builder.AppendLine(diagnostic.ToString()); - - var location = diagnostic.Location; - if (location == Location.None) - { - builder.Append($"Location unknown: ({diagnostic.Id})"); - } - else - { - True(location.IsInSource, - $"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostic}"); - - var linePosition = location.GetLineSpan().StartLinePosition; - builder.Append($"({(linePosition.Line + 1)}, {(linePosition.Character + 1)}, {diagnostic.Id})"); - } - - return builder.ToString(); - } - - private static async Task GetStringFromDocumentAsync(Document document) - { - var simplifiedDoc = await Simplifier.ReduceAsync(document, Simplifier.Annotation); - var root = await simplifiedDoc.GetSyntaxRootAsync(); - root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace); - return root.GetText().ToString(); - } - - private class DiagnosticsAssertException : Xunit.Sdk.EqualException - { - public DiagnosticsAssertException( - IEnumerable expected, - IEnumerable actual, - string message) - : base(expected, actual) - { - Message = message + Environment.NewLine + FormatDiagnostics(actual); - } - - public override string Message { get; } - } - - private class DiagnosticAssertException : Xunit.Sdk.EqualException - { - public DiagnosticAssertException( - DiagnosticResult expected, - Diagnostic actual, - string message) - : base(expected, actual) - { - Message = message + Environment.NewLine + FormatDiagnostic(actual); - } - - public override string Message { get; } - } - - private class DiagnosticLocationAssertException : Xunit.Sdk.EqualException - { - public DiagnosticLocationAssertException( - DiagnosticResultLocation expected, - Location actual, - string message) - : base(expected, actual) - { - Message = message; - } - - public override string Message { get; } - } - - } -} diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/DiagnosticResult.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/DiagnosticResult.cs deleted file mode 100644 index c1c32bd025..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/DiagnosticResult.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.CodeAnalysis; - -namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure -{ - /// - /// Location where the diagnostic appears, as determined by path, line number, and column number. - /// - public struct DiagnosticResultLocation - { - public DiagnosticResultLocation(int line, int column) - : this("Test.cs", line, column) - { - } - - public DiagnosticResultLocation(string path, int line, int column) - { - if (line < -1) - { - throw new ArgumentOutOfRangeException(nameof(line), "line must be >= -1"); - } - - if (column < -1) - { - throw new ArgumentOutOfRangeException(nameof(column), "column must be >= -1"); - } - - Path = path; - Line = line; - Column = column; - } - - public string Path { get; } - public int Line { get; } - public int Column { get; } - } - - /// - /// Struct that stores information about a Diagnostic appearing in a source - /// - public struct DiagnosticResult - { - private DiagnosticResultLocation[] _locations; - - public DiagnosticResultLocation[] Locations - { - get - { - if (_locations == null) - { - _locations = new DiagnosticResultLocation[] { }; - } - - return _locations; - } - - set => _locations = value; - } - - public DiagnosticSeverity Severity { get; set; } - - public string Id { get; set; } - - public string Message { get; set; } - - public string Path => Locations.Length > 0 ? Locations[0].Path : ""; - - public int Line => Locations.Length > 0 ? Locations[0].Line : -1; - - public int Column => Locations.Length > 0 ? Locations[0].Column : -1; - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcDiagnosticAnalyzerRunner.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcDiagnosticAnalyzerRunner.cs new file mode 100644 index 0000000000..d3a8d94b73 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcDiagnosticAnalyzerRunner.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Analyzer.Testing; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure +{ + public class MvcDiagnosticAnalyzerRunner : DiagnosticAnalyzerRunner + { + public MvcDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer) + { + Analyzer = analyzer; + } + + public DiagnosticAnalyzer Analyzer { get; } + + public Task GetDiagnosticsAsync(string source) + { + return GetDiagnosticsAsync(sources: new[] { source }, Analyzer, Array.Empty()); + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcTestSource.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcTestSource.cs new file mode 100644 index 0000000000..19c19140d1 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Infrastructure/MvcTestSource.cs @@ -0,0 +1,34 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.IO; +using Microsoft.AspNetCore.Analyzer.Testing; +using Microsoft.AspNetCore.Testing; + +namespace Microsoft.AspNetCore.Mvc.Analyzers.Infrastructure +{ + public static class MvcTestSource + { + private static readonly string ProjectDirectory = GetProjectDirectory(); + + public static TestSource Read(string testClassName, string testMethod) + { + var filePath = Path.Combine(ProjectDirectory, "TestFiles", testClassName, testMethod + ".cs"); + if (!File.Exists(filePath)) + { + throw new FileNotFoundException($"TestFile {testMethod} could not be found at {filePath}.", filePath); + } + + var fileContent = File.ReadAllText(filePath); + return TestSource.Read(fileContent); + } + + private static string GetProjectDirectory() + { + var solutionDirectory = TestPathUtilities.GetSolutionRootDirectory("Mvc"); + var assemblyName = typeof(MvcTestSource).Assembly.GetName().Name; + var projectDirectory = Path.Combine(solutionDirectory, "test", assemblyName); + return projectDirectory; + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Microsoft.AspNetCore.Mvc.Analyzers.Test.csproj b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Microsoft.AspNetCore.Mvc.Analyzers.Test.csproj index 53db6da506..92de24ec50 100644 --- a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Microsoft.AspNetCore.Mvc.Analyzers.Test.csproj +++ b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/Microsoft.AspNetCore.Mvc.Analyzers.Test.csproj @@ -13,6 +13,7 @@ + diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial_InSections.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfHtmlPartial_WithAdditionalParameters.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial_InSections.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial_InSections.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial_InSections.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial_InSections.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/DiagnosticsAreReturned_ForUseOfRenderPartial_WithAdditionalParameters.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForNonUseOfHtmlPartial.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForUseOfHtmlPartialAsync.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync.cs b/test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync.cs rename to test/Microsoft.AspNetCore.Mvc.Analyzers.Test/TestFiles/AvoidHtmlPartialAnalyzerTest/NoDiagnosticsAreReturned_ForUseOfRenderPartialAsync.cs