1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT license. See License.txt in the project root for license information.
3
3
4
- #nullable disable
5
-
6
4
using System ;
7
5
using System . Collections . Generic ;
8
6
using System . Diagnostics . CodeAnalysis ;
12
10
using Microsoft . AspNetCore . Razor . LanguageServer . ProjectSystem ;
13
11
using Microsoft . AspNetCore . Razor . Test . Common ;
14
12
using Microsoft . AspNetCore . Razor . Test . Common . LanguageServer ;
15
- using Microsoft . CodeAnalysis . Razor ;
13
+ using Microsoft . AspNetCore . Razor . Utilities ;
16
14
using Microsoft . CodeAnalysis . Razor . ProjectSystem ;
17
15
using Moq ;
18
16
using Xunit ;
@@ -39,8 +37,10 @@ public DefaultDocumentContextFactoryTest(ITestOutputHelper testOutput)
39
37
public void TryCreateAsync_CanNotResolveDocument_ReturnsNull ( )
40
38
{
41
39
// 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" ) ) ;
43
42
var uri = new Uri ( filePath ) ;
43
+
44
44
var factory = new DefaultDocumentContextFactory ( _projectSnapshotManagerAccessor , new TestDocumentResolver ( ) , _documentVersionCache , LoggerFactory ) ;
45
45
46
46
// Act
@@ -54,8 +54,10 @@ public void TryCreateAsync_CanNotResolveDocument_ReturnsNull()
54
54
public void TryCreateForOpenDocumentAsync_CanNotResolveDocument_ReturnsNull ( )
55
55
{
56
56
// 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" ) ) ;
58
59
var uri = new Uri ( filePath ) ;
60
+
59
61
var factory = new DefaultDocumentContextFactory ( _projectSnapshotManagerAccessor , new TestDocumentResolver ( ) , _documentVersionCache , LoggerFactory ) ;
60
62
61
63
// Act
@@ -69,9 +71,11 @@ public void TryCreateForOpenDocumentAsync_CanNotResolveDocument_ReturnsNull()
69
71
public void TryCreateForOpenDocumentAsync_CanNotResolveVersion_ReturnsNull ( )
70
72
{
71
73
// 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" ) ) ;
73
76
var uri = new Uri ( filePath ) ;
74
- var documentSnapshot = TestDocumentSnapshot . Create ( uri . GetAbsoluteOrUNCPath ( ) ) ;
77
+
78
+ var documentSnapshot = TestDocumentSnapshot . Create ( filePath ) ;
75
79
var documentResolver = new TestDocumentResolver ( documentSnapshot ) ;
76
80
var factory = new DefaultDocumentContextFactory ( _projectSnapshotManagerAccessor , documentResolver , _documentVersionCache , LoggerFactory ) ;
77
81
@@ -86,9 +90,11 @@ public void TryCreateForOpenDocumentAsync_CanNotResolveVersion_ReturnsNull()
86
90
public void TryCreateAsync_ResolvesContent ( )
87
91
{
88
92
// 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" ) ) ;
90
95
var uri = new Uri ( filePath ) ;
91
- var documentSnapshot = TestDocumentSnapshot . Create ( uri . GetAbsoluteOrUNCPath ( ) ) ;
96
+
97
+ var documentSnapshot = TestDocumentSnapshot . Create ( filePath ) ;
92
98
var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( string . Empty , documentSnapshot . FilePath ) ) ;
93
99
documentSnapshot . With ( codeDocument ) ;
94
100
var documentResolver = new TestDocumentResolver ( documentSnapshot ) ;
@@ -107,20 +113,22 @@ public void TryCreateAsync_ResolvesContent()
107
113
public void TryCreateAsync_WithProjectContext_Resolves ( )
108
114
{
109
115
// 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" ) ;
111
120
var uri = new Uri ( filePath ) ;
112
- var documentSnapshot = TestDocumentSnapshot . Create ( uri . GetAbsoluteOrUNCPath ( ) ) ;
121
+
122
+ var documentSnapshot = TestDocumentSnapshot . Create ( filePath ) ;
113
123
var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( string . Empty , documentSnapshot . FilePath ) ) ;
114
124
documentSnapshot . With ( codeDocument ) ;
115
125
var documentResolver = new TestDocumentResolver ( documentSnapshot ) ;
116
126
var factory = new DefaultDocumentContextFactory ( _projectSnapshotManagerAccessor , documentResolver , _documentVersionCache , LoggerFactory ) ;
117
127
118
- var projectFilePath = PathUtilities . CreateRootedPath ( "goo" ) ;
119
- var intermediateOutputPath = Path . Combine ( projectFilePath , "obj" ) ;
120
128
var hostProject = new HostProject ( projectFilePath , intermediateOutputPath , RazorConfiguration . Default , rootNamespace : null ) ;
121
129
_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 ) ) ;
124
132
125
133
// Act
126
134
var documentContext = factory . TryCreate ( uri , new VisualStudio . LanguageServer . Protocol . VSProjectContext { Id = hostProject . Key . Id } ) ;
@@ -134,20 +142,22 @@ public void TryCreateAsync_WithProjectContext_Resolves()
134
142
public void TryCreateAsync_WithProjectContext_DoesntUseSnapshotResolver ( )
135
143
{
136
144
// 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" ) ;
138
149
var uri = new Uri ( filePath ) ;
139
- var documentSnapshot = TestDocumentSnapshot . Create ( uri . GetAbsoluteOrUNCPath ( ) ) ;
150
+
151
+ var documentSnapshot = TestDocumentSnapshot . Create ( filePath ) ;
140
152
var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( string . Empty , documentSnapshot . FilePath ) ) ;
141
153
documentSnapshot . With ( codeDocument ) ;
142
154
var documentResolverMock = new Mock < ISnapshotResolver > ( MockBehavior . Strict ) ;
143
155
var factory = new DefaultDocumentContextFactory ( _projectSnapshotManagerAccessor , documentResolverMock . Object , _documentVersionCache , LoggerFactory ) ;
144
156
145
- var projectFilePath = PathUtilities . CreateRootedPath ( "goo" ) ;
146
- var intermediateOutputPath = Path . Combine ( projectFilePath , "obj" ) ;
147
157
var hostProject = new HostProject ( projectFilePath , intermediateOutputPath , RazorConfiguration . Default , rootNamespace : null ) ;
148
158
_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 ) ) ;
151
161
152
162
// Act
153
163
var documentContext = factory . TryCreate ( uri , new VisualStudio . LanguageServer . Protocol . VSProjectContext { Id = hostProject . Key . Id } ) ;
@@ -162,9 +172,11 @@ public void TryCreateAsync_WithProjectContext_DoesntUseSnapshotResolver()
162
172
public async Task TryCreateForOpenDocumentAsync_ResolvesContent ( )
163
173
{
164
174
// 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" ) ) ;
166
177
var uri = new Uri ( filePath ) ;
167
- var documentSnapshot = TestDocumentSnapshot . Create ( uri . GetAbsoluteOrUNCPath ( ) ) ;
178
+
179
+ var documentSnapshot = TestDocumentSnapshot . Create ( filePath ) ;
168
180
var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( string . Empty , documentSnapshot . FilePath ) ) ;
169
181
documentSnapshot . With ( codeDocument ) ;
170
182
var documentResolver = new TestDocumentResolver ( documentSnapshot ) ;
@@ -183,7 +195,7 @@ public async Task TryCreateForOpenDocumentAsync_ResolvesContent()
183
195
184
196
private class TestDocumentResolver : ISnapshotResolver
185
197
{
186
- private readonly IDocumentSnapshot _documentSnapshot ;
198
+ private readonly IDocumentSnapshot ? _documentSnapshot ;
187
199
188
200
public TestDocumentResolver ( )
189
201
{
@@ -204,7 +216,7 @@ public IProjectSnapshot GetMiscellaneousProject()
204
216
throw new NotImplementedException ( ) ;
205
217
}
206
218
207
- public bool TryResolveDocumentInAnyProject ( string documentFilePath , [ NotNullWhen ( true ) ] out IDocumentSnapshot documentSnapshot )
219
+ public bool TryResolveDocumentInAnyProject ( string documentFilePath , [ NotNullWhen ( true ) ] out IDocumentSnapshot ? documentSnapshot )
208
220
{
209
221
if ( documentFilePath == _documentSnapshot ? . FilePath )
210
222
{
0 commit comments