Skip to content

Commit 46a0223

Browse files
Attempt to fix DefaultDocumentContextFactory for non-Windows
1 parent 102b62d commit 46a0223

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Diagnostics.CodeAnalysis;
@@ -12,7 +10,7 @@
1210
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
1311
using Microsoft.AspNetCore.Razor.Test.Common;
1412
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
15-
using Microsoft.CodeAnalysis.Razor;
13+
using Microsoft.AspNetCore.Razor.Utilities;
1614
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1715
using Moq;
1816
using Xunit;
@@ -39,8 +37,10 @@ public DefaultDocumentContextFactoryTest(ITestOutputHelper testOutput)
3937
public void TryCreateAsync_CanNotResolveDocument_ReturnsNull()
4038
{
4139
// Arrange
42-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
40+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
41+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
4342
var uri = new Uri(filePath);
43+
4444
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, new TestDocumentResolver(), _documentVersionCache, LoggerFactory);
4545

4646
// Act
@@ -54,8 +54,10 @@ public void TryCreateAsync_CanNotResolveDocument_ReturnsNull()
5454
public void TryCreateForOpenDocumentAsync_CanNotResolveDocument_ReturnsNull()
5555
{
5656
// Arrange
57-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
57+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
58+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
5859
var uri = new Uri(filePath);
60+
5961
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, new TestDocumentResolver(), _documentVersionCache, LoggerFactory);
6062

6163
// Act
@@ -69,9 +71,11 @@ public void TryCreateForOpenDocumentAsync_CanNotResolveDocument_ReturnsNull()
6971
public void TryCreateForOpenDocumentAsync_CanNotResolveVersion_ReturnsNull()
7072
{
7173
// Arrange
72-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
74+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
75+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
7376
var uri = new Uri(filePath);
74-
var documentSnapshot = TestDocumentSnapshot.Create(uri.GetAbsoluteOrUNCPath());
77+
78+
var documentSnapshot = TestDocumentSnapshot.Create(filePath);
7579
var documentResolver = new TestDocumentResolver(documentSnapshot);
7680
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, documentResolver, _documentVersionCache, LoggerFactory);
7781

@@ -86,9 +90,11 @@ public void TryCreateForOpenDocumentAsync_CanNotResolveVersion_ReturnsNull()
8690
public void TryCreateAsync_ResolvesContent()
8791
{
8892
// Arrange
89-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
93+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
94+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
9095
var uri = new Uri(filePath);
91-
var documentSnapshot = TestDocumentSnapshot.Create(uri.GetAbsoluteOrUNCPath());
96+
97+
var documentSnapshot = TestDocumentSnapshot.Create(filePath);
9298
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(string.Empty, documentSnapshot.FilePath));
9399
documentSnapshot.With(codeDocument);
94100
var documentResolver = new TestDocumentResolver(documentSnapshot);
@@ -107,20 +113,22 @@ public void TryCreateAsync_ResolvesContent()
107113
public void TryCreateAsync_WithProjectContext_Resolves()
108114
{
109115
// Arrange
110-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
116+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
117+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
118+
var intermediateOutputPath = Path.Combine(baseDirectory, "obj");
119+
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
111120
var uri = new Uri(filePath);
112-
var documentSnapshot = TestDocumentSnapshot.Create(uri.GetAbsoluteOrUNCPath());
121+
122+
var documentSnapshot = TestDocumentSnapshot.Create(filePath);
113123
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(string.Empty, documentSnapshot.FilePath));
114124
documentSnapshot.With(codeDocument);
115125
var documentResolver = new TestDocumentResolver(documentSnapshot);
116126
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, documentResolver, _documentVersionCache, LoggerFactory);
117127

118-
var projectFilePath = PathUtilities.CreateRootedPath("goo");
119-
var intermediateOutputPath = Path.Combine(projectFilePath, "obj");
120128
var hostProject = new HostProject(projectFilePath, intermediateOutputPath, RazorConfiguration.Default, rootNamespace: null);
121129
_projectSnapshotManagerBase.ProjectAdded(hostProject);
122-
var hostDocument = new HostDocument(uri.GetAbsoluteOrUNCPath(), "file.cshtml");
123-
_projectSnapshotManagerBase.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(uri.GetAbsoluteOrUNCPath()));
130+
var hostDocument = new HostDocument(filePath, "file.cshtml");
131+
_projectSnapshotManagerBase.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(filePath));
124132

125133
// Act
126134
var documentContext = factory.TryCreate(uri, new VisualStudio.LanguageServer.Protocol.VSProjectContext { Id = hostProject.Key.Id });
@@ -134,20 +142,22 @@ public void TryCreateAsync_WithProjectContext_Resolves()
134142
public void TryCreateAsync_WithProjectContext_DoesntUseSnapshotResolver()
135143
{
136144
// Arrange
137-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
145+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
146+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
147+
var intermediateOutputPath = Path.Combine(baseDirectory, "obj");
148+
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
138149
var uri = new Uri(filePath);
139-
var documentSnapshot = TestDocumentSnapshot.Create(uri.GetAbsoluteOrUNCPath());
150+
151+
var documentSnapshot = TestDocumentSnapshot.Create(filePath);
140152
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(string.Empty, documentSnapshot.FilePath));
141153
documentSnapshot.With(codeDocument);
142154
var documentResolverMock = new Mock<ISnapshotResolver>(MockBehavior.Strict);
143155
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, documentResolverMock.Object, _documentVersionCache, LoggerFactory);
144156

145-
var projectFilePath = PathUtilities.CreateRootedPath("goo");
146-
var intermediateOutputPath = Path.Combine(projectFilePath, "obj");
147157
var hostProject = new HostProject(projectFilePath, intermediateOutputPath, RazorConfiguration.Default, rootNamespace: null);
148158
_projectSnapshotManagerBase.ProjectAdded(hostProject);
149-
var hostDocument = new HostDocument(uri.GetAbsoluteOrUNCPath(), "file.cshtml");
150-
_projectSnapshotManagerBase.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(uri.GetAbsoluteOrUNCPath()));
159+
var hostDocument = new HostDocument(filePath, "file.cshtml");
160+
_projectSnapshotManagerBase.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(filePath));
151161

152162
// Act
153163
var documentContext = factory.TryCreate(uri, new VisualStudio.LanguageServer.Protocol.VSProjectContext { Id = hostProject.Key.Id });
@@ -162,9 +172,11 @@ public void TryCreateAsync_WithProjectContext_DoesntUseSnapshotResolver()
162172
public async Task TryCreateForOpenDocumentAsync_ResolvesContent()
163173
{
164174
// Arrange
165-
var filePath = PathUtilities.CreateRootedPath("path", "to", "file.cshtml");
175+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
176+
var filePath = FilePathNormalizer.Normalize(Path.Combine(baseDirectory, "file.cshtml"));
166177
var uri = new Uri(filePath);
167-
var documentSnapshot = TestDocumentSnapshot.Create(uri.GetAbsoluteOrUNCPath());
178+
179+
var documentSnapshot = TestDocumentSnapshot.Create(filePath);
168180
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(string.Empty, documentSnapshot.FilePath));
169181
documentSnapshot.With(codeDocument);
170182
var documentResolver = new TestDocumentResolver(documentSnapshot);
@@ -183,7 +195,7 @@ public async Task TryCreateForOpenDocumentAsync_ResolvesContent()
183195

184196
private class TestDocumentResolver : ISnapshotResolver
185197
{
186-
private readonly IDocumentSnapshot _documentSnapshot;
198+
private readonly IDocumentSnapshot? _documentSnapshot;
187199

188200
public TestDocumentResolver()
189201
{
@@ -204,7 +216,7 @@ public IProjectSnapshot GetMiscellaneousProject()
204216
throw new NotImplementedException();
205217
}
206218

207-
public bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot documentSnapshot)
219+
public bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot? documentSnapshot)
208220
{
209221
if (documentFilePath == _documentSnapshot?.FilePath)
210222
{

src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/TestDocumentSnapshot.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Immutable;
6+
using System.IO;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Razor.Language;
89
using Microsoft.AspNetCore.Razor.ProjectSystem;
@@ -33,7 +34,12 @@ public static TestDocumentSnapshot Create(string filePath, string text, VersionS
3334
public static TestDocumentSnapshot Create(string filePath, string text, VersionStamp version, TestProjectSnapshot projectSnapshot)
3435
{
3536
using var testWorkspace = TestWorkspace.Create();
36-
var hostDocument = new HostDocument(filePath, filePath);
37+
38+
var targetPath = Path.GetDirectoryName(projectSnapshot.FilePath) is string projectDirectory && filePath.StartsWith(projectDirectory)
39+
? filePath[projectDirectory.Length..]
40+
: filePath;
41+
42+
var hostDocument = new HostDocument(filePath, targetPath);
3743
var sourceText = SourceText.From(text);
3844
var documentState = new DocumentState(
3945
testWorkspace.Services,

0 commit comments

Comments
 (0)