From bce408a66db744d0c2c8064195d474750d55e108 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 13 Sep 2024 14:03:43 +1000 Subject: [PATCH] Move helper method to somewhere central --- ...ft.AspNetCore.Razor.Microbenchmarks.csproj | 3 ++ .../CSharpTestLspServerHelpers.cs | 32 +------------- ...spNetCore.Razor.Test.Common.Tooling.csproj | 4 -- .../Workspaces/TestWorkspace.cs | 44 +++++++++++++++++++ .../Cohost/CohostEndpointTestBase.cs | 7 +-- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks.csproj b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks.csproj index 1010350dd2..d317968768 100644 --- a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks.csproj +++ b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks.csproj @@ -43,6 +43,9 @@ TestServices\%(FileName)%(Extension) + + TestServices\%(FileName)%(Extension) + diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/CSharpTestLspServerHelpers.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/CSharpTestLspServerHelpers.cs index 83a05dc1ab..52a5b14612 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/CSharpTestLspServerHelpers.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/CSharpTestLspServerHelpers.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -14,9 +13,7 @@ using Microsoft.AspNetCore.Razor.Test.Common.Mef; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; using Microsoft.AspNetCore.Razor.Threading; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.ExternalAccess.Razor; -using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis.Text; @@ -109,8 +106,7 @@ internal static class CSharpTestLspServerHelpers IRazorSpanMappingService razorSpanMappingService, bool multiTargetProject) { - var hostServices = MefHostServices.Create(exportProvider.AsCompositionContext()); - var workspace = TestWorkspace.Create(hostServices); + var workspace = TestWorkspace.CreateWithDiagnosticAnalyzers(exportProvider); // Add project and solution to workspace var projectInfoNet60 = ProjectInfo.Create( @@ -142,8 +138,6 @@ internal static class CSharpTestLspServerHelpers workspace.AddSolution(solutionInfo); - AddAnalyzersToWorkspace(workspace, exportProvider); - // Add document to workspace. We use an IVT method to create the DocumentInfo variable because there's // a special constructor in Roslyn that will help identify the document as belonging to Razor. var languageServerFactory = exportProvider.GetExportedValue(); @@ -172,30 +166,6 @@ internal static class CSharpTestLspServerHelpers return workspace; } - public static void AddAnalyzersToWorkspace(Workspace workspace, ExportProvider exportProvider) - { - var analyzerLoader = RazorTestAnalyzerLoader.CreateAnalyzerAssemblyLoader(); - - var analyzerPaths = new DirectoryInfo(AppContext.BaseDirectory).GetFiles("*.dll") - .Where(f => f.Name.StartsWith("Microsoft.CodeAnalysis.", StringComparison.Ordinal) && !f.Name.Contains("LanguageServer") && !f.Name.Contains("Test.Utilities")) - .Select(f => f.FullName) - .ToImmutableArray(); - var references = new List(); - foreach (var analyzerPath in analyzerPaths) - { - if (File.Exists(analyzerPath)) - { - references.Add(new AnalyzerFileReference(analyzerPath, analyzerLoader)); - } - } - - workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(references)); - - // Make sure Roslyn is producing diagnostics for our workspace - var razorTestAnalyzerLoader = exportProvider.GetExportedValue(); - razorTestAnalyzerLoader.InitializeDiagnosticsServices(workspace); - } - private record CSharpFile(Uri DocumentUri, SourceText CSharpSourceText); private class EmptyMappingService : IRazorSpanMappingService diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Microsoft.AspNetCore.Razor.Test.Common.Tooling.csproj b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Microsoft.AspNetCore.Razor.Test.Common.Tooling.csproj index 89019499fa..6683bf95cd 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Microsoft.AspNetCore.Razor.Test.Common.Tooling.csproj +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Microsoft.AspNetCore.Razor.Test.Common.Tooling.csproj @@ -89,10 +89,6 @@ - - - - diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Workspaces/TestWorkspace.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Workspaces/TestWorkspace.cs index cae18632e5..ffb3c8de50 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Workspaces/TestWorkspace.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Workspaces/TestWorkspace.cs @@ -2,8 +2,17 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.IO; +using System.Linq; +using Microsoft.AspNetCore.Razor.Test.Common.Mef; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.ExternalAccess.Razor; using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.VisualStudio.Composition; namespace Microsoft.AspNetCore.Razor.Test.Common.Workspaces; @@ -14,6 +23,17 @@ public static class TestWorkspace public static Workspace Create(Action? configure = null) => Create(services: null, configure: configure); + public static AdhocWorkspace CreateWithDiagnosticAnalyzers(ExportProvider exportProvider) + { + var hostServices = MefHostServices.Create(exportProvider.AsCompositionContext()); + + var workspace = Create(hostServices); + + AddAnalyzersToWorkspace(workspace, exportProvider); + + return workspace; + } + public static AdhocWorkspace Create(HostServices? services, Action? configure = null) { lock (s_workspaceLock) @@ -27,4 +47,28 @@ public static class TestWorkspace return workspace; } } + + private static void AddAnalyzersToWorkspace(Workspace workspace, ExportProvider exportProvider) + { + var analyzerLoader = RazorTestAnalyzerLoader.CreateAnalyzerAssemblyLoader(); + + var analyzerPaths = new DirectoryInfo(AppContext.BaseDirectory).GetFiles("*.dll") + .Where(f => f.Name.StartsWith("Microsoft.CodeAnalysis.", StringComparison.Ordinal) && !f.Name.Contains("LanguageServer") && !f.Name.Contains("Test.Utilities")) + .Select(f => f.FullName) + .ToImmutableArray(); + var references = new List(); + foreach (var analyzerPath in analyzerPaths) + { + if (File.Exists(analyzerPath)) + { + references.Add(new AnalyzerFileReference(analyzerPath, analyzerLoader)); + } + } + + workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(references)); + + // Make sure Roslyn is producing diagnostics for our workspace + var razorTestAnalyzerLoader = exportProvider.GetExportedValue(); + razorTestAnalyzerLoader.InitializeDiagnosticsServices(workspace); + } } diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostEndpointTestBase.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostEndpointTestBase.cs index abd502ca20..bb50863d64 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostEndpointTestBase.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostEndpointTestBase.cs @@ -9,11 +9,9 @@ using Basic.Reference.Assemblies; using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Test.Common; -using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; using Microsoft.AspNetCore.Razor.Test.Common.Mef; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Razor.Remote; using Microsoft.CodeAnalysis.Razor.Workspaces; using Microsoft.CodeAnalysis.Remote.Razor; @@ -110,11 +108,8 @@ public abstract class CohostEndpointTestBase(ITestOutputHelper testOutputHelper) { var exportProvider = TestComposition.Roslyn.ExportProviderFactory.CreateExportProvider(); AddDisposable(exportProvider); - var hostServices = MefHostServices.Create(exportProvider.AsCompositionContext()); - var workspace = TestWorkspace.Create(hostServices); + var workspace = TestWorkspace.CreateWithDiagnosticAnalyzers(exportProvider); AddDisposable(workspace); - // Adding analyzers modifies the workspace, so important to do it before creating the first project - CSharpTestLspServerHelpers.AddAnalyzersToWorkspace(workspace, exportProvider); var razorDocument = CreateProjectAndRazorDocument(workspace, projectId, projectName, documentId, documentFilePath, contents, additionalFiles);