Move helper method to somewhere central

This commit is contained in:
David Wengier 2024-09-13 14:03:43 +10:00
Родитель b36d507968
Коммит bce408a66d
5 изменённых файлов: 49 добавлений и 41 удалений

Просмотреть файл

@ -43,6 +43,9 @@
<Compile Include="..\..\test\Microsoft.AspNetCore.Razor.Test.Common.Tooling\Workspaces\TestWorkspaceServices.cs">
<Link>TestServices\%(FileName)%(Extension)</Link>
</Compile>
<Compile Include="..\..\test\Microsoft.AspNetCore.Razor.Test.Common.Tooling\Mef\ExportProviderExtensions.cs">
<Link>TestServices\%(FileName)%(Extension)</Link>
</Compile>
<None Remove=".gitignore" />
<None Remove="BenchmarkDotNet.Artifacts\**" />

Просмотреть файл

@ -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<AbstractRazorLanguageServerFactoryWrapper>();
@ -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<AnalyzerFileReference>();
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>();
razorTestAnalyzerLoader.InitializeDiagnosticsServices(workspace);
}
private record CSharpFile(Uri DocumentUri, SourceText CSharpSourceText);
private class EmptyMappingService : IRazorSpanMappingService

Просмотреть файл

@ -89,10 +89,6 @@
<InternalsVisibleTo Include="rzls" Key="$(RazorKey)" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\Benchmarking\" />
</ItemGroup>
<Import Condition="'$(TargetFramework)' != '$(DefaultNetFxTargetFramework)'" Project="..\..\..\Shared\Microsoft.AspNetCore.Razor.Serialization.Json\Microsoft.AspNetCore.Razor.Serialization.Json.projitems" Label="Shared" />
</Project>

Просмотреть файл

@ -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<AdhocWorkspace>? 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<AdhocWorkspace>? 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<AnalyzerFileReference>();
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>();
razorTestAnalyzerLoader.InitializeDiagnosticsServices(workspace);
}
}

Просмотреть файл

@ -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);