From 71981dcedefd82236b489fa3eb49a2c3738cb283 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Wed, 9 Oct 2024 14:55:31 -0700 Subject: [PATCH] Remove CreateAndAddProject/Document test extension methods --- ...extDocumentUriPresentationEndpointTests.cs | 92 ++++++++++--------- .../IProjectSnapshotManagerExtensionsTest.cs | 80 +++++++++------- .../WorkspaceDiagnosticRefreshTest.cs | 33 ++++--- .../ProjectSystem/Extensions.cs | 26 ------ .../ProjectSystem/TestHostDocument.cs | 26 ++++++ .../ProjectSystem/TestHostProject.cs | 17 ++++ .../TestProjectSnapshotManager.cs | 14 --- .../TestMocks.cs | 2 - .../CSharpVirtualDocumentFactoryTest.cs | 41 +++++---- 9 files changed, 178 insertions(+), 153 deletions(-) delete mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/Extensions.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostDocument.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostProject.cs diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs index 123764dc62..0e3e8509ef 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs @@ -29,13 +29,16 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp // Arrange var projectManager = CreateProjectSnapshotManager(); - var project = await projectManager.UpdateAsync(updater => - { - return updater.CreateAndAddProject("c:/path/project.csproj"); - }); + var hostProject = TestHostProject.Create("c:/path/project.csproj"); + var hostDocument1 = TestHostDocument.Create(hostProject, "c:/path/index.razor"); + var hostDocument2 = TestHostDocument.Create(hostProject, "c:/path/MyTagHelper.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/index.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/MyTagHelper.razor"); + await projectManager.UpdateAsync(updater => + { + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument1, hostDocument1.CreateEmptyTextLoader()); + updater.DocumentAdded(hostProject.Key, hostDocument2, hostDocument2.CreateEmptyTextLoader()); + }); var droppedUri = new Uri("file:///c:/path/MyTagHelper.razor"); var builder = TagHelperDescriptorBuilder.Create("MyTagHelper", "MyAssembly"); @@ -43,15 +46,14 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp await projectManager.UpdateAsync(updater => { - updater.ProjectWorkspaceStateChanged(project.Key, ProjectWorkspaceState.Create([builder.Build()])); + updater.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Create([builder.Build()])); }); - var razorFilePath = "c:/path/index.razor"; - var uri = new Uri(razorFilePath); + var uri = new Uri(hostDocument1.FilePath); await projectManager.UpdateAsync(updater => { - updater.DocumentOpened(project.Key, razorFilePath, SourceText.From("
")); + updater.DocumentOpened(hostProject.Key, hostDocument1.FilePath, SourceText.From("
")); }); var documentContextFactory = new DocumentContextFactory(projectManager, LoggerFactory); @@ -87,13 +89,16 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp // Arrange var projectManager = CreateProjectSnapshotManager(); - var project = await projectManager.UpdateAsync(updater => - { - return updater.CreateAndAddProject("c:/path/project.csproj"); - }); + var hostProject = TestHostProject.Create("c:/path/project.csproj"); + var hostDocument1 = TestHostDocument.Create(hostProject, "c:/path/index.razor"); + var hostDocument2 = TestHostDocument.Create(hostProject, "c:/path/MyTagHelper.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/index.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/MyTagHelper.razor"); + await projectManager.UpdateAsync(updater => + { + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument1, hostDocument1.CreateEmptyTextLoader()); + updater.DocumentAdded(hostProject.Key, hostDocument2, hostDocument2.CreateEmptyTextLoader()); + }); var droppedUri = new Uri("file:///c:/path/MyTagHelper.razor"); var builder = TagHelperDescriptorBuilder.Create("MyTagHelper", "MyAssembly"); @@ -101,15 +106,14 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp await projectManager.UpdateAsync(updater => { - updater.ProjectWorkspaceStateChanged(project.Key, ProjectWorkspaceState.Create([builder.Build()])); + updater.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Create([builder.Build()])); }); - var razorFilePath = "c:/path/index.razor"; - var uri = new Uri(razorFilePath); + var uri = new Uri(hostDocument1.FilePath); await projectManager.UpdateAsync(updater => { - updater.DocumentOpened(project.Key, razorFilePath, SourceText.From("
")); + updater.DocumentOpened(hostProject.Key, hostDocument1.FilePath, SourceText.From("
")); }); var documentContextFactory = new DocumentContextFactory(projectManager, LoggerFactory); @@ -150,13 +154,16 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp // Arrange var projectManager = CreateProjectSnapshotManager(); - var project = await projectManager.UpdateAsync(updater => - { - return updater.CreateAndAddProject("c:/path/project.csproj"); - }); + var hostProject = TestHostProject.Create("c:/path/project.csproj"); + var hostDocument1 = TestHostDocument.Create(hostProject, "c:/path/index.razor"); + var hostDocument2 = TestHostDocument.Create(hostProject, "c:/path/fetchdata.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/index.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/fetchdata.razor"); + await projectManager.UpdateAsync(updater => + { + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument1, hostDocument1.CreateEmptyTextLoader()); + updater.DocumentAdded(hostProject.Key, hostDocument2, hostDocument2.CreateEmptyTextLoader()); + }); var droppedUri = new Uri("file:///c:/path/fetchdata.razor"); var builder = TagHelperDescriptorBuilder.Create("FetchData", "MyAssembly"); @@ -170,15 +177,14 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp await projectManager.UpdateAsync(updater => { - updater.ProjectWorkspaceStateChanged(project.Key, ProjectWorkspaceState.Create([builder.Build()])); + updater.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Create([builder.Build()])); }); - var razorFilePath = "c:/path/index.razor"; - var uri = new Uri(razorFilePath); + var uri = new Uri(hostDocument1.FilePath); await projectManager.UpdateAsync(updater => { - updater.DocumentOpened(project.Key, razorFilePath, SourceText.From("
")); + updater.DocumentOpened(hostProject.Key, hostDocument1.FilePath, SourceText.From("
")); }); var documentContextFactory = new DocumentContextFactory(projectManager, LoggerFactory); @@ -308,13 +314,16 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp // Arrange var projectManager = CreateProjectSnapshotManager(); - var project = await projectManager.UpdateAsync(updater => - { - return updater.CreateAndAddProject("c:/path/project.csproj"); - }); + var hostProject = TestHostProject.Create("c:/path/project.csproj"); + var hostDocument1 = TestHostDocument.Create(hostProject, "c:/path/index.razor"); + var hostDocument2 = TestHostDocument.Create(hostProject, "c:/path/fetchdata.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/index.razor"); - await projectManager.CreateAndAddDocumentAsync(project, "c:/path/fetchdata.razor"); + await projectManager.UpdateAsync(updater => + { + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument1, hostDocument1.CreateEmptyTextLoader()); + updater.DocumentAdded(hostProject.Key, hostDocument2, hostDocument2.CreateEmptyTextLoader()); + }); var droppedUri1 = new Uri("file:///c:/path/fetchdata.razor.cs"); var droppedUri2 = new Uri("file:///c:/path/fetchdata.razor"); @@ -323,20 +332,19 @@ public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutp await projectManager.UpdateAsync(updater => { - updater.ProjectWorkspaceStateChanged(project.Key, ProjectWorkspaceState.Create([builder.Build()])); + updater.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Create([builder.Build()])); }); - var razorFilePath = "c:/path/index.razor"; - var uri = new Uri(razorFilePath); + var uri = new Uri(hostDocument1.FilePath); await projectManager.UpdateAsync(updater => { - updater.DocumentOpened(project.Key, razorFilePath, SourceText.From("
")); + updater.DocumentOpened(hostProject.Key, hostDocument1.FilePath, SourceText.From("
")); }); var documentSnapshot = projectManager - .GetLoadedProject(project.Key) - .GetDocument(razorFilePath); + .GetLoadedProject(hostProject.Key) + .GetDocument(hostDocument1.FilePath); Assert.NotNull(documentSnapshot); var documentContextFactory = new DocumentContextFactory(projectManager, LoggerFactory); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs index 525614171b..c124083560 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs @@ -39,12 +39,10 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) await projectManager.UpdateAsync(updater => { - var miscProject = projectManager.GetMiscellaneousProject(); - var hostProject = new HostProject(miscProject.FilePath, miscProject.IntermediateOutputPath, FallbackRazorConfiguration.Latest, miscProject.RootNamespace); - updater.DocumentAdded( - hostProject.Key, - new HostDocument(normalizedFilePath, "document.cshtml"), - new EmptyTextLoader(normalizedFilePath)); + var hostProject = MiscFilesHostProject.Instance with { Configuration = FallbackRazorConfiguration.Latest }; + var hostDocument = new HostDocument(normalizedFilePath, targetPath: "document.cshtml"); + + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); }); // Act @@ -110,11 +108,13 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) { // Arrange var documentFilePath = "C:/path/to/document.cshtml"; + var hostProject = TestHostProject.Create("C:/other/path/to/project.csproj"); + var projectManager = CreateProjectSnapshotManager(); await projectManager.UpdateAsync(updater => { - updater.ProjectAdded(TestProjectSnapshot.Create("C:/other/path/to/project.csproj").HostProject); + updater.ProjectAdded(hostProject); }); // Act @@ -125,20 +125,23 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) public async Task TryResolveAllProjects_OwnerProjectWithOthers_ReturnsTrue() { // Arrange - var documentFilePath = "C:/path/to/document.cshtml"; + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + var hostDocument = TestHostDocument.Create(hostProject, "C:/path/to/document.cshtml"); + var otherHostProject = TestHostProject.Create("C:/path/to/other/project.csproj"); + var projectManager = CreateProjectSnapshotManager(); var expectedProject = await projectManager.UpdateAsync(updater => { - var expectedProject = updater.CreateAndAddProject("C:/path/to/project.csproj"); - updater.CreateAndAddProject("C:/path/to/other/project.csproj"); - updater.CreateAndAddDocument(expectedProject, documentFilePath); + updater.ProjectAdded(hostProject); + updater.ProjectAdded(otherHostProject); + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); - return expectedProject; + return updater.GetLoadedProject(hostProject.Key); }); // Act - Assert.True(projectManager.TryResolveAllProjects(documentFilePath, out var projects)); + Assert.True(projectManager.TryResolveAllProjects(hostDocument.FilePath, out var projects)); // Assert var project = Assert.Single(projects); @@ -149,18 +152,21 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) public async Task TryResolveAllProjects_MiscellaneousOwnerProjectWithOthers_ReturnsTrue() { // Arrange - var documentFilePath = Path.Combine(MiscFilesHostProject.Instance.DirectoryPath, "file.cshtml"); + var miscFilesHostProject = MiscFilesHostProject.Instance; + var documentFilePath = Path.Combine(miscFilesHostProject.DirectoryPath, "file.cshtml"); documentFilePath = FilePathNormalizer.Normalize(documentFilePath); + var hostDocument = TestHostDocument.Create(miscFilesHostProject, documentFilePath); + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + var projectManager = CreateProjectSnapshotManager(); var miscProject = await projectManager.UpdateAsync(updater => { - var miscProject = (ProjectSnapshot)projectManager.GetMiscellaneousProject(); - updater.CreateAndAddDocument(miscProject, documentFilePath); - updater.CreateAndAddProject("C:/path/to/project.csproj"); + updater.DocumentAdded(miscFilesHostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); + updater.ProjectAdded(hostProject); - return miscProject; + return updater.GetLoadedProject(miscFilesHostProject.Key); }); // Act @@ -175,19 +181,21 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) public async Task TryResolveAllProjects_OwnerProjectDifferentCasing_ReturnsTrue() { // Arrange - var documentFilePath = "c:/path/to/document.cshtml"; + var hostProject = TestHostProject.Create("C:/Path/To/project.csproj"); + var hostDocument = TestHostDocument.Create(hostProject, "c:/path/to/document.cshtml"); + var projectManager = CreateProjectSnapshotManager(); var ownerProject = await projectManager.UpdateAsync(updater => { - var ownerProject = updater.CreateAndAddProject("C:/Path/To/project.csproj"); - updater.CreateAndAddDocument(ownerProject, documentFilePath); + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); - return ownerProject; + return updater.GetLoadedProject(hostProject.Key); }); // Act - Assert.True(projectManager.TryResolveAllProjects(documentFilePath, out var projects)); + Assert.True(projectManager.TryResolveAllProjects(hostDocument.FilePath, out var projects)); // Assert var project = Assert.Single(projects); @@ -222,32 +230,36 @@ public class IProjectSnapshotManagerExtensionsTest(ITestOutputHelper testOutput) Assert.Equal(MiscFilesHostProject.Instance.FilePath, project.FilePath); } - private async Task CreateProjectManagerAsync(string filePath, bool addToMiscellaneous = false) + private async Task CreateProjectManagerAsync(string documentFilePath, bool addToMiscellaneous = false) { - filePath = FilePathNormalizer.Normalize(filePath); + documentFilePath = FilePathNormalizer.Normalize(documentFilePath); var projectManager = CreateProjectSnapshotManager(); + HostProject hostProject; + if (addToMiscellaneous) { - await projectManager.UpdateAsync(updater => - { - var miscProject = (ProjectSnapshot)projectManager.GetMiscellaneousProject(); - updater.CreateAndAddDocument(miscProject, filePath); - }); + hostProject = MiscFilesHostProject.Instance; } else { - var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(filePath); - var projectSnapshot = TestProjectSnapshot.Create(Path.Combine(projectDirectory, "proj.csproj")); + var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(documentFilePath); + hostProject = TestHostProject.Create(Path.Combine(projectDirectory, "proj.csproj")); await projectManager.UpdateAsync(updater => { - updater.ProjectAdded(projectSnapshot.HostProject); - updater.CreateAndAddDocument(projectSnapshot, filePath); + updater.ProjectAdded(hostProject); }); } + var hostDocument = TestHostDocument.Create(hostProject, documentFilePath); + + await projectManager.UpdateAsync(updater => + { + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); + }); + return projectManager; } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/WorkspaceDiagnosticRefreshTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/WorkspaceDiagnosticRefreshTest.cs index e50e31bc90..f4ea2ed099 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/WorkspaceDiagnosticRefreshTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/WorkspaceDiagnosticRefreshTest.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -10,7 +9,6 @@ using Microsoft.AspNetCore.Razor.LanguageServer.Hosting; using Microsoft.AspNetCore.Razor.Test.Common; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem; -using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.VisualStudio.LanguageServer.Protocol; using Moq; using Xunit; @@ -52,7 +50,8 @@ public class WorkspaceDiagnosticRefreshTest(ITestOutputHelper testOutputHelper) await projectSnapshotManager.UpdateAsync( static updater => { - updater.CreateAndAddProject("C:/path/to/project.csproj"); + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + updater.ProjectAdded(hostProject); }); await testAccessor.WaitForRefreshAsync(); @@ -86,23 +85,25 @@ public class WorkspaceDiagnosticRefreshTest(ITestOutputHelper testOutputHelper) var testAccessor = publisher.GetTestAccessor(); + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + + var directory = Path.GetDirectoryName(hostProject.FilePath); + Assert.NotNull(directory); + + var hostDocument = TestHostDocument.Create(hostProject, Path.Combine(directory, "directory.razor")); + await projectSnapshotManager.UpdateAsync( - static updater => + updater => { - updater.CreateAndAddProject("C:/path/to/project.csproj"); + updater.ProjectAdded(hostProject); }); await testAccessor.WaitForRefreshAsync(); await projectSnapshotManager.UpdateAsync( - static updater => + updater => { - var project = (ProjectSnapshot)updater.GetProjects().First(); - var directory = Path.GetDirectoryName(project.FilePath); - Assert.NotNull(directory); - - var filePath = Path.Combine(directory, "document.razor"); - updater.CreateAndAddDocument(project, filePath); + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); }); await testAccessor.WaitForRefreshAsync(); @@ -136,9 +137,10 @@ public class WorkspaceDiagnosticRefreshTest(ITestOutputHelper testOutputHelper) var testAccessor = publisher.GetTestAccessor(); await projectSnapshotManager.UpdateAsync( - static updater => + updater => { - updater.CreateAndAddProject("C:/path/to/project.csproj"); + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + updater.ProjectAdded(hostProject); }); await testAccessor.WaitForRefreshAsync(); @@ -176,7 +178,8 @@ public class WorkspaceDiagnosticRefreshTest(ITestOutputHelper testOutputHelper) await projectSnapshotManager.UpdateAsync( static updater => { - updater.CreateAndAddProject("C:/path/to/project.csproj"); + var hostProject = TestHostProject.Create("C:/path/to/project.csproj"); + updater.ProjectAdded(hostProject); }); await testAccessor.WaitForRefreshAsync(); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/Extensions.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/Extensions.cs deleted file mode 100644 index 455366453b..0000000000 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/Extensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the MIT license. See License.txt in the project root for license information. - -using Microsoft.AspNetCore.Razor.LanguageServer; -using Microsoft.CodeAnalysis.Razor.ProjectSystem; - -namespace Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem; - -internal static class Extensions -{ - public static TestDocumentSnapshot CreateAndAddDocument(this ProjectSnapshotManager.Updater updater, ProjectSnapshot projectSnapshot, string filePath) - { - var documentSnapshot = TestDocumentSnapshot.Create(projectSnapshot, filePath); - updater.DocumentAdded(projectSnapshot.Key, documentSnapshot.HostDocument, new DocumentSnapshotTextLoader(documentSnapshot)); - - return documentSnapshot; - } - - public static TestProjectSnapshot CreateAndAddProject(this ProjectSnapshotManager.Updater updater, string filePath) - { - var projectSnapshot = TestProjectSnapshot.Create(filePath); - updater.ProjectAdded(projectSnapshot.HostProject); - - return projectSnapshot; - } -} diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostDocument.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostDocument.cs new file mode 100644 index 0000000000..326d4c0d7e --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostDocument.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Razor.Utilities; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Razor.ProjectSystem; + +namespace Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem; + +internal static class TestHostDocument +{ + public static HostDocument Create(HostProject hostProject, string documentFilePath) + { + var targetPath = FilePathNormalizer.Normalize(documentFilePath); + var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(hostProject.FilePath); + if (targetPath.StartsWith(projectDirectory)) + { + targetPath = targetPath[projectDirectory.Length..]; + } + + return new(documentFilePath, targetPath); + } + + public static TextLoader CreateEmptyTextLoader(this HostDocument hostDocument) + => TestMocks.CreateTextLoader(hostDocument.FilePath, string.Empty); +} diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostProject.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostProject.cs new file mode 100644 index 0000000000..0f3ac548c4 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestHostProject.cs @@ -0,0 +1,17 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root for license information. + +using System.IO; +using Microsoft.AspNetCore.Razor.Language; +using Microsoft.CodeAnalysis.Razor.ProjectSystem; + +namespace Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem; + +internal static class TestHostProject +{ + public static HostProject Create(string filePath) + => Create(filePath, intermediateOutputPath: Path.Combine(Path.GetDirectoryName(filePath) ?? @"\\path", "obj")); + + public static HostProject Create(string filePath, string intermediateOutputPath) + => new(filePath, intermediateOutputPath, RazorConfiguration.Default, rootNamespace: "TestRootNamespace"); +} diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestProjectSnapshotManager.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestProjectSnapshotManager.cs index 49491cfbc9..06ae213387 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestProjectSnapshotManager.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestProjectSnapshotManager.cs @@ -4,7 +4,6 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor.LanguageServer; using Microsoft.AspNetCore.Razor.ProjectEngineHost; using Microsoft.CodeAnalysis.Razor.Logging; using Microsoft.CodeAnalysis.Razor.ProjectSystem; @@ -20,19 +19,6 @@ internal partial class TestProjectSnapshotManager( { private readonly CancellationToken _disposalToken = disposalToken; - public Task CreateAndAddDocumentAsync(ProjectSnapshot projectSnapshot, string filePath) - { - return UpdateAsync( - updater => - { - var documentSnapshot = TestDocumentSnapshot.Create(projectSnapshot, filePath); - updater.DocumentAdded(projectSnapshot.Key, documentSnapshot.HostDocument, new DocumentSnapshotTextLoader(documentSnapshot)); - - return documentSnapshot; - }, - _disposalToken); - } - public Listener ListenToNotifications() => new(this); public Task UpdateAsync(Action updater) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestMocks.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestMocks.cs index c8f8e987ec..168a3f3bb9 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestMocks.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestMocks.cs @@ -3,12 +3,10 @@ using System; using System.Threading; -using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.LanguageServer.Hosting; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; using Moq; -using Moq.Language.Flow; namespace Microsoft.AspNetCore.Razor.Test.Common; diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/CSharpVirtualDocumentFactoryTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/CSharpVirtualDocumentFactoryTest.cs index 62ae510977..65ecaca31e 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/CSharpVirtualDocumentFactoryTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/CSharpVirtualDocumentFactoryTest.cs @@ -3,14 +3,12 @@ using System; using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Test.Common; using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem; using Microsoft.AspNetCore.Razor.Test.Common.VisualStudio; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Workspaces; -using Microsoft.VisualStudio.Editor.Razor; using Microsoft.VisualStudio.LanguageServer.ContainedLanguage; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Utilities; @@ -133,10 +131,13 @@ public class CSharpVirtualDocumentFactoryTest : VisualStudioTestBase var projectManager = CreateProjectSnapshotManager(); + var hostProject = TestHostProject.Create(@"C:\path\to\project.csproj"); + var hostDocument = TestHostDocument.Create(hostProject, @"C:\path\to\file.razor"); + await projectManager.UpdateAsync(updater => { - var project = updater.CreateAndAddProject(@"C:\path\to\project.csproj"); - updater.CreateAndAddDocument(project, @"C:\path\to\file.razor"); + updater.ProjectAdded(hostProject); + updater.DocumentAdded(hostProject.Key, hostDocument, hostDocument.CreateEmptyTextLoader()); }); var factory = new CSharpVirtualDocumentFactory( @@ -171,25 +172,25 @@ public class CSharpVirtualDocumentFactoryTest : VisualStudioTestBase var projectManager = CreateProjectSnapshotManager(); + var hostProject1 = TestHostProject.Create( + filePath: @"C:\path\to\project1.csproj", + intermediateOutputPath: @"C:\path\to\obj1"); + + var hostDocument1 = TestHostDocument.Create(hostProject1, @"C:\path\to\file.razor"); + + var hostProject2 = TestHostProject.Create( + filePath: @"C:\path\to\project2.csproj", + intermediateOutputPath: @"C:\path\to\obj2"); + + var hostDocument2 = TestHostDocument.Create(hostProject2, @"C:\path\to\file.razor"); + await projectManager.UpdateAsync(updater => { - var project1 = TestProjectSnapshot.Create( - @"C:\path\to\project1.csproj", - @"C:\path\to\obj1", - documentFilePaths: [], - RazorConfiguration.Default, - projectWorkspaceState: null); - updater.ProjectAdded(project1.HostProject); - updater.CreateAndAddDocument(project1, @"C:\path\to\file.razor"); + updater.ProjectAdded(hostProject1); + updater.DocumentAdded(hostProject1.Key, hostDocument1, hostDocument1.CreateEmptyTextLoader()); - var project2 = TestProjectSnapshot.Create( - @"C:\path\to\project2.csproj", - @"C:\path\to\obj2", - documentFilePaths: [], - RazorConfiguration.Default, - projectWorkspaceState: null); - updater.ProjectAdded(project2.HostProject); - updater.CreateAndAddDocument(project2, @"C:\path\to\file.razor"); + updater.ProjectAdded(hostProject2); + updater.DocumentAdded(hostProject2.Key, hostDocument2, hostDocument2.CreateEmptyTextLoader()); }); var languageServerFeatureOptions = new TestLanguageServerFeatureOptions(includeProjectKeyInGeneratedFilePath: true);