Skip to content

Commit 2fcf4b9

Browse files
Add helper for creating rooted paths to avoid NetFx quirk
1 parent 3ba3fd1 commit 2fcf4b9

File tree

4 files changed

+103
-37
lines changed

4 files changed

+103
-37
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/TagHelperTooltipFactoryBaseTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public void GetAvailableProjects_OneProject_ReturnsNull()
361361
var tagHelpers = ImmutableArray.Create(builder.Build());
362362
var projectWorkspaceState = new ProjectWorkspaceState(tagHelpers, CodeAnalysis.CSharp.LanguageVersion.Default);
363363

364-
var baseDirectory = Path.Combine("C:", "path", "to");
364+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
365365
var razorFilePath = Path.Combine(baseDirectory, "file.razor");
366366
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
367367

@@ -385,7 +385,7 @@ public void GetAvailableProjects_AvailableInAllProjects_ReturnsNull()
385385
var tagHelpers = ImmutableArray.Create(builder.Build());
386386
var projectWorkspaceState = new ProjectWorkspaceState(tagHelpers, CodeAnalysis.CSharp.LanguageVersion.Default);
387387

388-
var baseDirectory = Path.Combine("C:", "path", "to");
388+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
389389
var razorFilePath = Path.Combine(baseDirectory, "file.razor");
390390
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
391391
var baseIntermediateOutputPath = Path.Combine(baseDirectory, "obj");
@@ -422,7 +422,7 @@ public void GetAvailableProjects_NotAvailableInAllProjects_ReturnsText()
422422
var tagHelpers = ImmutableArray.Create(builder.Build());
423423
var projectWorkspaceState = new ProjectWorkspaceState(tagHelpers, CodeAnalysis.CSharp.LanguageVersion.Default);
424424

425-
var baseDirectory = Path.Combine("C:", "path", "to");
425+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
426426
var razorFilePath = Path.Combine(baseDirectory, "file.razor");
427427
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
428428
var baseIntermediateOutputPath = Path.Combine(baseDirectory, "obj");
@@ -458,7 +458,7 @@ public void GetAvailableProjects_NotAvailableInAllProjects_ReturnsText()
458458
[Fact]
459459
public void GetAvailableProjects_NotAvailableInAnyProject_ReturnsText()
460460
{
461-
var baseDirectory = Path.Combine("C:", "path", "to");
461+
var baseDirectory = PathUtilities.CreateRootedPath("path", "to");
462462
var razorFilePath = Path.Combine(baseDirectory, "file.razor");
463463
var projectFilePath = Path.Combine(baseDirectory, "project.csproj");
464464
var baseIntermediateOutputPath = Path.Combine(baseDirectory, "obj");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading.Tasks;
1111
using Microsoft.AspNetCore.Razor.Language;
1212
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
13+
using Microsoft.AspNetCore.Razor.Test.Common;
1314
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
1415
using Microsoft.CodeAnalysis.Razor;
1516
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -109,7 +110,7 @@ public void TryCreateAsync_WithProjectContext_Resolves()
109110
var documentResolver = new TestDocumentResolver(documentSnapshot);
110111
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, documentResolver, _documentVersionCache, LoggerFactory);
111112

112-
var projectFilePath = Path.Combine("C:", "goo");
113+
var projectFilePath = PathUtilities.CreateRootedPath("goo");
113114
var intermediateOutputPath = Path.Combine(projectFilePath, "obj");
114115
var hostProject = new HostProject(projectFilePath, intermediateOutputPath, RazorConfiguration.Default, rootNamespace: null);
115116
_projectSnapshotManagerBase.ProjectAdded(hostProject);
@@ -135,7 +136,7 @@ public void TryCreateAsync_WithProjectContext_DoesntUseSnapshotResolver()
135136
var documentResolverMock = new Mock<ISnapshotResolver>(MockBehavior.Strict);
136137
var factory = new DefaultDocumentContextFactory(_projectSnapshotManagerAccessor, documentResolverMock.Object, _documentVersionCache, LoggerFactory);
137138

138-
var projectFilePath = Path.Combine("C:", "goo");
139+
var projectFilePath = PathUtilities.CreateRootedPath("goo");
139140
var intermediateOutputPath = Path.Combine(projectFilePath, "obj");
140141
var hostProject = new HostProject(projectFilePath, intermediateOutputPath, RazorConfiguration.Default, rootNamespace: null);
141142
_projectSnapshotManagerBase.ProjectAdded(hostProject);

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

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
11+
using Microsoft.AspNetCore.Razor.Test.Common;
1112
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
1213
using Microsoft.AspNetCore.Razor.Test.Common.ProjectSystem;
1314
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
@@ -28,7 +29,8 @@ public class MonitorProjectConfigurationFilePathEndpointTest : LanguageServerTes
2829
public MonitorProjectConfigurationFilePathEndpointTest(ITestOutputHelper testOutput)
2930
: base(testOutput)
3031
{
31-
_directoryPathResolver = Mock.Of<WorkspaceDirectoryPathResolver>(resolver => resolver.Resolve() == "C:/dir", MockBehavior.Strict);
32+
var path = PathUtilities.CreateRootedPath("dir");
33+
_directoryPathResolver = Mock.Of<WorkspaceDirectoryPathResolver>(resolver => resolver.Resolve() == path, MockBehavior.Strict);
3234
}
3335

3436
[Fact]
@@ -45,10 +47,15 @@ public async Task Handle_Disposed_Noops()
4547
TestLanguageServerFeatureOptions.Instance,
4648
LoggerFactory);
4749
configurationFileEndpoint.Dispose();
50+
51+
var debugDirectory = PathUtilities.CreateRootedPath("dir", "obj", "Debug");
52+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
53+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
54+
4855
var request = new MonitorProjectConfigurationFilePathParams()
4956
{
50-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
51-
ConfigurationFilePath = "C:/dir/obj/Debug/project.razor.bin",
57+
ProjectKeyId = projectKey.Id,
58+
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
5259
};
5360
var requestContext = CreateRazorRequestContext(documentContext: null);
5461

@@ -69,9 +76,13 @@ public async Task Handle_ConfigurationFilePath_UntrackedMonitorNoops()
6976
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
7077
TestLanguageServerFeatureOptions.Instance,
7178
LoggerFactory);
79+
80+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
81+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
82+
7283
var request = new MonitorProjectConfigurationFilePathParams()
7384
{
74-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
85+
ProjectKeyId = projectKey.Id,
7586
ConfigurationFilePath = null!,
7687
};
7788
var requestContext = CreateRazorRequestContext(documentContext: null);
@@ -91,16 +102,21 @@ public async Task Handle_ConfigurationFilePath_TrackedMonitor_StopsMonitor()
91102
_directoryPathResolver,
92103
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
93104
LoggerFactory);
105+
106+
var debugDirectory = PathUtilities.CreateRootedPath("externaldir", "obj", "Debug");
107+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
108+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
109+
94110
var startRequest = new MonitorProjectConfigurationFilePathParams()
95111
{
96-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
97-
ConfigurationFilePath = "C:/externaldir/obj/Debug/project.razor.bin",
112+
ProjectKeyId = projectKey.Id,
113+
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
98114
};
99115
var requestContext = CreateRazorRequestContext(documentContext: null);
100116
await configurationFileEndpoint.HandleNotificationAsync(startRequest, requestContext, DisposalToken);
101117
var stopRequest = new MonitorProjectConfigurationFilePathParams()
102118
{
103-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
119+
ProjectKeyId = projectKey.Id,
104120
ConfigurationFilePath = null!,
105121
};
106122

@@ -123,10 +139,15 @@ public async Task Handle_InWorkspaceDirectory_Noops()
123139
_directoryPathResolver,
124140
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
125141
LoggerFactory);
142+
143+
var debugDirectory = PathUtilities.CreateRootedPath("dir", "obj", "Debug");
144+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
145+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
146+
126147
var startRequest = new MonitorProjectConfigurationFilePathParams()
127148
{
128-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
129-
ConfigurationFilePath = "C:/dir/obj/Debug/project.razor.bin",
149+
ProjectKeyId = projectKey.Id,
150+
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
130151
};
131152
var requestContext = CreateRazorRequestContext(documentContext: null);
132153

@@ -149,10 +170,15 @@ public async Task Handle_InWorkspaceDirectory_MonitorsIfLanguageFeatureOptionSet
149170
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
150171
LoggerFactory,
151172
options: new TestLanguageServerFeatureOptions(monitorWorkspaceFolderForConfigurationFiles: false));
173+
174+
var debugDirectory = PathUtilities.CreateRootedPath("dir", "obj", "Debug");
175+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
176+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
177+
152178
var startRequest = new MonitorProjectConfigurationFilePathParams()
153179
{
154-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
155-
ConfigurationFilePath = "C:/dir/obj/Debug/project.razor.bin",
180+
ProjectKeyId = projectKey.Id,
181+
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
156182
};
157183
var requestContext = CreateRazorRequestContext(documentContext: null);
158184

@@ -174,10 +200,15 @@ public async Task Handle_DuplicateMonitors_Noops()
174200
_directoryPathResolver,
175201
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
176202
LoggerFactory);
203+
204+
var debugDirectory = PathUtilities.CreateRootedPath("externaldir", "obj", "Debug");
205+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
206+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
207+
177208
var startRequest = new MonitorProjectConfigurationFilePathParams()
178209
{
179-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
180-
ConfigurationFilePath = "C:/externaldir/obj/Debug/project.razor.bin",
210+
ProjectKeyId = projectKey.Id,
211+
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
181212
};
182213
var requestContext = CreateRazorRequestContext(documentContext: null);
183214

@@ -202,18 +233,20 @@ public async Task Handle_ChangedConfigurationOutputPath_StartsWithNewPath()
202233
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
203234
LoggerFactory);
204235

205-
var debugDirectory = Path.Combine("C:", "externaldir", "obj", "Debug");
206-
var releaseDirectory = Path.Combine("C:", "externaldir", "obj", "Release");
236+
var debugDirectory = PathUtilities.CreateRootedPath("externaldir", "obj", "Debug");
237+
var releaseDirectory = PathUtilities.CreateRootedPath("externaldir", "obj", "Release");
238+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
239+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
207240

208241
var debugOutputPath = new MonitorProjectConfigurationFilePathParams()
209242
{
210-
ProjectKeyId = TestProjectKey.Create("C:/dir/obj").Id,
243+
ProjectKeyId = projectKey.Id,
211244
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
212245
};
213246

214247
var releaseOutputPath = new MonitorProjectConfigurationFilePathParams()
215248
{
216-
ProjectKeyId = debugOutputPath.ProjectKeyId,
249+
ProjectKeyId = projectKey.Id,
217250
ConfigurationFilePath = Path.Combine(releaseDirectory, "project.razor.bin")
218251
};
219252

@@ -240,18 +273,20 @@ public async Task Handle_ChangedConfigurationExternalToInternal_StopsWithoutRest
240273
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
241274
LoggerFactory);
242275

243-
var debugDirectory = Path.Combine("C:", "externaldir", "obj", "Debug");
244-
var releaseDirectory = Path.Combine("C:", "dir", "obj", "Release");
276+
var debugDirectory = PathUtilities.CreateRootedPath("externaldir", "obj", "Debug");
277+
var releaseDirectory = PathUtilities.CreateRootedPath("dir", "obj", "Release");
278+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
279+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
245280

246281
var externalRequest = new MonitorProjectConfigurationFilePathParams()
247282
{
248-
ProjectKeyId = TestProjectKey.Create("C:\\dir\\obj").Id,
283+
ProjectKeyId = projectKey.Id,
249284
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
250285
};
251286

252287
var internalRequest = new MonitorProjectConfigurationFilePathParams()
253288
{
254-
ProjectKeyId = externalRequest.ProjectKeyId,
289+
ProjectKeyId = projectKey.Id,
255290
ConfigurationFilePath = Path.Combine(releaseDirectory, "project.razor.bin")
256291
};
257292

@@ -282,18 +317,20 @@ public async Task Handle_ProjectPublished()
282317
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
283318
LoggerFactory);
284319

285-
var debugDirectory = Path.Combine("C:", "externaldir1", "obj", "Debug");
286-
var releaseDirectory = Path.Combine("C:", "externaldir1", "obj", "Release");
320+
var debugDirectory = PathUtilities.CreateRootedPath("externaldir1", "obj", "Debug");
321+
var releaseDirectory = PathUtilities.CreateRootedPath("externaldir1", "obj", "Release");
322+
var projectKeyDirectory = PathUtilities.CreateRootedPath("dir", "obj");
323+
var projectKey = TestProjectKey.Create(projectKeyDirectory);
287324

288325
var debugOutputPath = new MonitorProjectConfigurationFilePathParams()
289326
{
290-
ProjectKeyId = TestProjectKey.Create("C:\\dir\\obj").Id,
327+
ProjectKeyId = projectKey.Id,
291328
ConfigurationFilePath = Path.Combine(debugDirectory, "project.razor.bin")
292329
};
293330

294331
var releaseOutputPath = new MonitorProjectConfigurationFilePathParams()
295332
{
296-
ProjectKeyId = debugOutputPath.ProjectKeyId,
333+
ProjectKeyId = projectKey.Id,
297334
ConfigurationFilePath = Path.Combine(releaseDirectory, "project.razor.bin")
298335
};
299336

@@ -335,25 +372,29 @@ public async Task Handle_MultipleProjects_StartedAndStopped()
335372
Enumerable.Empty<IProjectConfigurationFileChangeListener>(),
336373
LoggerFactory);
337374

338-
var debugDirectory1 = Path.Combine("C:", "externaldir1", "obj", "Debug");
339-
var releaseDirectory1 = Path.Combine("C:", "externaldir1", "obj", "Release");
340-
var debugDirectory2 = Path.Combine("C:", "externaldir2", "obj", "Debug");
375+
var debugDirectory1 = PathUtilities.CreateRootedPath("externaldir1", "obj", "Debug");
376+
var releaseDirectory1 = PathUtilities.CreateRootedPath("externaldir1", "obj", "Release");
377+
var debugDirectory2 = PathUtilities.CreateRootedPath("externaldir2", "obj", "Debug");
378+
var projectKeyDirectory1 = PathUtilities.CreateRootedPath("dir", "obj");
379+
var projectKey1 = TestProjectKey.Create(projectKeyDirectory1);
380+
var projectKeyDirectory2 = PathUtilities.CreateRootedPath("dir", "obj2");
381+
var projectKey2 = TestProjectKey.Create(projectKeyDirectory2);
341382

342383
var debugOutputPath1 = new MonitorProjectConfigurationFilePathParams()
343384
{
344-
ProjectKeyId = TestProjectKey.Create("C:\\dir\\obj").Id,
385+
ProjectKeyId = projectKey1.Id,
345386
ConfigurationFilePath = Path.Combine(debugDirectory1, "project.razor.bin")
346387
};
347388

348389
var releaseOutputPath1 = new MonitorProjectConfigurationFilePathParams()
349390
{
350-
ProjectKeyId = debugOutputPath1.ProjectKeyId,
391+
ProjectKeyId = projectKey1.Id,
351392
ConfigurationFilePath = Path.Combine(releaseDirectory1, "project.razor.bin")
352393
};
353394

354395
var debugOutputPath2 = new MonitorProjectConfigurationFilePathParams()
355396
{
356-
ProjectKeyId = TestProjectKey.Create("C:\\dir\\obj2").Id,
397+
ProjectKeyId = projectKey2.Id,
357398
ConfigurationFilePath = Path.Combine(debugDirectory2, "project.razor.bin")
358399
};
359400

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.IO;
5+
using System.Runtime.InteropServices;
6+
7+
namespace Microsoft.AspNetCore.Razor.Test.Common;
8+
9+
public static class PathUtilities
10+
{
11+
public static string CreateRootedPath(params string[] parts)
12+
{
13+
var result = Path.Combine(parts);
14+
15+
if (!Path.IsPathRooted(result))
16+
{
17+
result = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
18+
? @"C:\" + result
19+
: "/" + result;
20+
}
21+
22+
return result;
23+
}
24+
}

0 commit comments

Comments
 (0)