Skip to content

Commit 4238928

Browse files
committed
Restore functionality in tests
Sadly most of the usage of the AddDocument method, that took advantage of the fact that it would try to find a real project, were tests.
1 parent 31c9282 commit 4238928

File tree

4 files changed

+90
-32
lines changed

4 files changed

+90
-32
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorComponentSearchEngineTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected override async Task InitializeAsync()
6262
return textLoaderMock.Object;
6363
});
6464

65-
var projectService = new RazorProjectService(
65+
var projectService = new TestRazorProjectService(
6666
remoteTextLoaderFactoryMock.Object,
6767
snapshotResolver,
6868
documentVersionCache,
@@ -77,10 +77,10 @@ await projectService.AddProjectAsync(
7777
displayName: "",
7878
DisposalToken);
7979

80-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath1, DisposalToken);
80+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath1, DisposalToken);
8181
await projectService.UpdateDocumentAsync(s_componentFilePath1, SourceText.From(""), version: 1, DisposalToken);
8282

83-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath2, DisposalToken);
83+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath2, DisposalToken);
8484
await projectService.UpdateDocumentAsync(s_componentFilePath2, SourceText.From("@namespace Test"), version: 1, DisposalToken);
8585

8686
await projectService.AddProjectAsync(
@@ -91,7 +91,7 @@ await projectService.AddProjectAsync(
9191
displayName: "",
9292
DisposalToken);
9393

94-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath3, DisposalToken);
94+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath3, DisposalToken);
9595
await projectService.UpdateDocumentAsync(s_componentFilePath3, SourceText.From(""), version: 1, DisposalToken);
9696
}
9797

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RazorProjectServiceTest : LanguageServerTestBase
3131
private readonly TestProjectSnapshotManager _projectManager;
3232
private readonly SnapshotResolver _snapshotResolver;
3333
private readonly DocumentVersionCache _documentVersionCache;
34-
private readonly RazorProjectService _projectService;
34+
private readonly TestRazorProjectService _projectService;
3535

3636
public RazorProjectServiceTest(ITestOutputHelper testOutput)
3737
: base(testOutput)
@@ -45,7 +45,7 @@ public RazorProjectServiceTest(ITestOutputHelper testOutput)
4545
.Setup(x => x.Create(It.IsAny<string>()))
4646
.Returns(CreateEmptyTextLoader());
4747

48-
_projectService = new RazorProjectService(
48+
_projectService = new TestRazorProjectService(
4949
remoteTextLoaderFactoryMock.Object,
5050
_snapshotResolver,
5151
_documentVersionCache,
@@ -439,7 +439,7 @@ public async Task CloseDocument_ClosesDocumentInOwnerProject()
439439

440440
var ownerProjectKey = await _projectService.AddProjectAsync(
441441
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
442-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
442+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
443443
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
444444

445445
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
@@ -472,7 +472,7 @@ public async Task CloseDocument_ClosesDocumentInAllOwnerProjects()
472472
ProjectFilePath, IntermediateOutputPath1, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
473473
var ownerProjectKey2 = await _projectService.AddProjectAsync(
474474
ProjectFilePath, IntermediateOutputPath2, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
475-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
475+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
476476
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
477477

478478
var ownerProject1 = _projectManager.GetLoadedProject(ownerProjectKey1);
@@ -499,7 +499,7 @@ public async Task CloseDocument_ClosesDocumentInMiscellaneousProject()
499499
// Arrange
500500
const string DocumentFilePath = "document.cshtml";
501501

502-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
502+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
503503
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
504504

505505
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
@@ -529,7 +529,7 @@ public async Task OpenDocument_OpensAlreadyAddedDocumentInOwnerProject()
529529

530530
var ownerProjectKey = await _projectService.AddProjectAsync(
531531
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
532-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
532+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
533533

534534
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
535535

@@ -561,7 +561,7 @@ public async Task OpenDocument_OpensAlreadyAddedDocumentInAllOwnerProjects()
561561
ProjectFilePath, IntermediateOutputPath1, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
562562
var ownerProjectKey2 = await _projectService.AddProjectAsync(
563563
ProjectFilePath, IntermediateOutputPath2, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
564-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
564+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
565565

566566
var ownerProject1 = _projectManager.GetLoadedProject(ownerProjectKey1);
567567
var ownerProject2 = _projectManager.GetLoadedProject(ownerProjectKey2);
@@ -638,14 +638,14 @@ public async Task AddDocument_NoopsIfDocumentIsAlreadyAdded()
638638
// Arrange
639639
const string DocumentFilePath = "document.cshtml";
640640

641-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
641+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
642642

643643
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
644644

645645
using var listener = _projectManager.ListenToNotifications();
646646

647647
// Act
648-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
648+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
649649

650650
// Assert
651651
listener.AssertNoNotifications();
@@ -668,7 +668,7 @@ public async Task AddDocument_AddsDocumentToOwnerProject()
668668
using var listener = _projectManager.ListenToNotifications();
669669

670670
// Act
671-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
671+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
672672

673673
// Assert
674674
listener.AssertNotifications(
@@ -688,7 +688,7 @@ public async Task AddDocument_AddsDocumentToMiscellaneousProject()
688688
using var listener = _projectManager.ListenToNotifications();
689689

690690
// Act
691-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
691+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
692692

693693
// Assert
694694
listener.AssertNotifications(
@@ -708,7 +708,7 @@ public async Task RemoveDocument_RemovesDocumentFromOwnerProject()
708708

709709
var ownerProjectKey = await _projectService.AddProjectAsync(
710710
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
711-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
711+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
712712

713713
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
714714

@@ -738,7 +738,7 @@ public async Task RemoveDocument_RemovesDocumentFromAllOwnerProjects()
738738
ProjectFilePath, IntermediateOutputPath1, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
739739
var ownerProjectKey2 = await _projectService.AddProjectAsync(
740740
ProjectFilePath, IntermediateOutputPath2, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
741-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
741+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
742742

743743
var ownerProject1 = _projectManager.GetLoadedProject(ownerProjectKey1);
744744
var ownerProject2 = _projectManager.GetLoadedProject(ownerProjectKey2);
@@ -767,7 +767,7 @@ public async Task RemoveOpenDocument_RemovesDocumentFromOwnerProject_MovesToMisc
767767

768768
var ownerProjectKey = await _projectService.AddProjectAsync(
769769
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
770-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
770+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
771771
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
772772

773773
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
@@ -863,7 +863,7 @@ public async Task UpdateDocument_ChangesDocumentInOwnerProject()
863863

864864
var ownerProjectKey = await _projectService.AddProjectAsync(
865865
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
866-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
866+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
867867
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
868868

869869
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
@@ -896,7 +896,7 @@ public async Task UpdateDocument_ChangesDocumentInAllOwnerProjects()
896896
ProjectFilePath, IntermediateOutputPath1, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
897897
var ownerProjectKey2 = await _projectService.AddProjectAsync(
898898
ProjectFilePath, IntermediateOutputPath2, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
899-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
899+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
900900
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
901901

902902
var ownerProject1 = _projectManager.GetLoadedProject(ownerProjectKey1);
@@ -923,7 +923,7 @@ public async Task UpdateDocument_ChangesDocumentInMiscProject()
923923
// Arrange
924924
const string DocumentFilePath = "document.cshtml";
925925

926-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
926+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
927927
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
928928

929929
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
@@ -953,7 +953,7 @@ public async Task UpdateDocument_TracksKnownDocumentVersion()
953953

954954
var ownerProjectKey = await _projectService.AddProjectAsync(
955955
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
956-
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);
956+
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);
957957

958958
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
959959

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Refactoring/RenameEndpointTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public async Task Handle_Rename_SingleServer_DoesNotDelegateForRazor()
629629
return textLoaderMock.Object;
630630
});
631631

632-
var projectService = new RazorProjectService(
632+
var projectService = new TestRazorProjectService(
633633
remoteTextLoaderFactoryMock.Object,
634634
snapshotResolver,
635635
documentVersionCache,
@@ -644,12 +644,12 @@ await projectManager.UpdateAsync(updater =>
644644
updater.ProjectWorkspaceStateChanged(projectKey1, ProjectWorkspaceState.Create(tagHelpers));
645645
});
646646

647-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath1, DisposalToken);
648-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath2, DisposalToken);
649-
await projectService.AddDocumentToMiscProjectAsync(s_directoryFilePath1, DisposalToken);
650-
await projectService.AddDocumentToMiscProjectAsync(s_directoryFilePath2, DisposalToken);
651-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath1337, DisposalToken);
652-
await projectService.AddDocumentToMiscProjectAsync(s_indexFilePath1, DisposalToken);
647+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath1, DisposalToken);
648+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath2, DisposalToken);
649+
await projectService.AddDocumentToPotentialProjectsAsync(s_directoryFilePath1, DisposalToken);
650+
await projectService.AddDocumentToPotentialProjectsAsync(s_directoryFilePath2, DisposalToken);
651+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath1337, DisposalToken);
652+
await projectService.AddDocumentToPotentialProjectsAsync(s_indexFilePath1, DisposalToken);
653653

654654
await projectService.UpdateDocumentAsync(s_componentFilePath1, SourceText.From(ComponentText1), version: 42, DisposalToken);
655655
await projectService.UpdateDocumentAsync(s_componentFilePath2, SourceText.From(ComponentText2), version: 42, DisposalToken);
@@ -666,9 +666,9 @@ await projectManager.UpdateAsync(updater =>
666666
updater.ProjectWorkspaceStateChanged(projectKey2, ProjectWorkspaceState.Create(tagHelpers));
667667
});
668668

669-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath3, DisposalToken);
670-
await projectService.AddDocumentToMiscProjectAsync(s_componentFilePath4, DisposalToken);
671-
await projectService.AddDocumentToMiscProjectAsync(s_componentWithParamFilePath, DisposalToken);
669+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath3, DisposalToken);
670+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath4, DisposalToken);
671+
await projectService.AddDocumentToPotentialProjectsAsync(s_componentWithParamFilePath, DisposalToken);
672672

673673
await projectService.UpdateDocumentAsync(s_componentFilePath3, SourceText.From(ComponentText3), version: 42, DisposalToken);
674674
await projectService.UpdateDocumentAsync(s_componentFilePath4, SourceText.From(ComponentText4), version: 42, DisposalToken);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using System.Collections.Immutable;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Razor.Language;
9+
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
10+
using Microsoft.AspNetCore.Razor.Serialization;
11+
using Microsoft.AspNetCore.Razor.Utilities;
12+
using Microsoft.CodeAnalysis.Razor;
13+
using Microsoft.CodeAnalysis.Razor.Logging;
14+
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
15+
16+
namespace Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
17+
18+
internal class TestRazorProjectService(
19+
RemoteTextLoaderFactory remoteTextLoaderFactory,
20+
ISnapshotResolver snapshotResolver,
21+
IDocumentVersionCache documentVersionCache,
22+
IProjectSnapshotManager projectManager,
23+
ILoggerFactory loggerFactory)
24+
: RazorProjectService(remoteTextLoaderFactory, snapshotResolver, documentVersionCache, projectManager, loggerFactory)
25+
{
26+
private readonly ISnapshotResolver _snapshotResolver = snapshotResolver;
27+
28+
public async Task AddDocumentToPotentialProjectsAsync(string textDocumentPath, CancellationToken cancellationToken)
29+
{
30+
foreach (var projectSnapshot in _snapshotResolver.FindPotentialProjects(textDocumentPath))
31+
{
32+
var normalizedProjectPath = FilePathNormalizer.NormalizeDirectory(projectSnapshot.FilePath);
33+
var documents = ImmutableArray
34+
.CreateRange(projectSnapshot.DocumentFilePaths)
35+
.Add(textDocumentPath)
36+
.Select(d => new DocumentSnapshotHandle(d, d, FileKinds.GetFileKindFromFilePath(d)))
37+
.ToImmutableArray();
38+
39+
await this.UpdateProjectAsync(projectSnapshot.Key, projectSnapshot.Configuration, projectSnapshot.RootNamespace, projectSnapshot.DisplayName, projectSnapshot.ProjectWorkspaceState,
40+
documents, cancellationToken).ConfigureAwait(false);
41+
}
42+
}
43+
44+
private static string GetTargetPath(string documentFilePath, string normalizedProjectPath)
45+
{
46+
var targetFilePath = FilePathNormalizer.Normalize(documentFilePath);
47+
if (targetFilePath.StartsWith(normalizedProjectPath, FilePathComparison.Instance))
48+
{
49+
// Make relative
50+
targetFilePath = documentFilePath[normalizedProjectPath.Length..];
51+
}
52+
53+
// Representing all of our host documents with a re-normalized target path to workaround GetRelatedDocument limitations.
54+
var normalizedTargetFilePath = targetFilePath.Replace('/', '\\').TrimStart('\\');
55+
56+
return normalizedTargetFilePath;
57+
}
58+
}

0 commit comments

Comments
 (0)