Skip to content

Commit 974588e

Browse files
committed
chore: Add remote testing settings for WSL environment
1 parent 0cceb23 commit 974588e

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

test/docfx.Snapshot.Tests/SamplesTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Runtime.CompilerServices;
56
using System.Text;
67
using System.Text.Encodings.Web;
78
using System.Text.Json;
@@ -56,7 +57,7 @@ public async Task Seed()
5657
using var process = Process.Start("dotnet", $"build \"{s_samplesDir}/seed/dotnet/assembly/BuildFromAssembly.csproj\"");
5758
await process.WaitForExitAsync();
5859

59-
if (Debugger.IsAttached)
60+
if (Debugger.IsAttached || IsWslRemoteTest())
6061
{
6162
Environment.SetEnvironmentVariable("DOCFX_SOURCE_BRANCH_NAME", "main");
6263
Assert.Equal(0, Program.Main([$"{samplePath}/docfx.json"]));
@@ -229,4 +230,13 @@ private string ExtractText(Page page)
229230

230231
return sb.ToString();
231232
}
233+
234+
/// <summary>
235+
/// Returns true if running on WSL and executed on Visual Studio Remote Testing.
236+
/// </summary>
237+
private static bool IsWslRemoteTest([CallerFilePath] string callerFilePath = "")
238+
{
239+
return Environment.GetEnvironmentVariable("WSLENV") != null
240+
&& callerFilePath.Contains('\\', StringComparison.Ordinal); // Contains `\` when build on windows environment.
241+
}
232242
}

test/docfx.Tests/Utilities/PathHelper.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.CompilerServices;
5+
using System.Text.RegularExpressions;
56

67
namespace Docfx.Tests;
78

89
internal class PathHelper
910
{
1011
public static string GetSolutionFolder([CallerFilePath] string callerFilePath = "")
1112
{
12-
if (callerFilePath.StartsWith("/_/"))
13-
{
14-
// PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
15-
// So try to get workspace folder from GitHub Action environment variable.
16-
var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
17-
if (workspace != null)
18-
return workspace;
19-
}
13+
callerFilePath = NormalizeCallerFilePath(callerFilePath);
2014

2115
if (!File.Exists(callerFilePath))
2216
{
2317
// CallerFilePath is resolved at build timing.
2418
// If build/test is executed on separated machine. It failed to find file.
25-
throw new FileNotFoundException($"File is not found. callerFilePath: {callerFilePath}");
19+
throw new FileNotFoundException($"File is not found. path: {path}");
2620
}
2721

2822
return FindSolutionFolder(callerFilePath, "docfx");
@@ -58,31 +52,26 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
5852
var dir = GetTestDataDirectory(callerFilePath);
5953

6054
var resultPath = Path.Combine(dir, path);
61-
if (!File.Exists(callerFilePath) && !Directory.Exists(callerFilePath))
55+
if (!File.Exists(resultPath) && !Directory.Exists(resultPath))
6256
{
6357
throw new FileNotFoundException($"Specified TestData file/directory is not found. path: {resultPath}");
6458
}
6559

66-
return Path.GetFullPath(resultPath);
60+
return Path.GetFullPath(path);
6761
}
6862

6963
/// <summary>
7064
/// Find TestData from callerFilePath.
7165
/// </summary>
7266
public static string GetTestDataDirectory([CallerFilePath] string callerFilePath = "")
7367
{
74-
if (callerFilePath.StartsWith("/_/"))
75-
{
76-
var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
77-
if (workspace != null)
78-
callerFilePath = callerFilePath.Replace("/_/", workspace);
79-
}
68+
callerFilePath = NormalizeCallerFilePath(callerFilePath);
8069

8170
if (!File.Exists(callerFilePath))
8271
{
8372
// CallerFilePath is resolved at build timing.
8473
// If build/test is executed on separated machine. It failed to find file.
85-
throw new FileNotFoundException($"File is not found. callerFilePath: {callerFilePath}");
74+
throw new FileNotFoundException($"File is not found. path: {callerFilePath}");
8675
}
8776

8877
// Find closest `TestData` directory.
@@ -105,4 +94,30 @@ public static string GetTestDataDirectory([CallerFilePath] string callerFilePath
10594

10695
return dir.FullName;
10796
}
97+
98+
private static string NormalizeCallerFilePath(string callerFilePath)
99+
{
100+
// PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
101+
// So try to get workspace folder from GitHub Action environment variable.
102+
if (callerFilePath.StartsWith("/_/"))
103+
{
104+
var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
105+
if (workspace != null)
106+
return workspace;
107+
}
108+
109+
// Rewrite path when test runnign on WSL environment that are executed by Visual Studio Remote Testing.
110+
if (Environment.GetEnvironmentVariable("WSLENV") != null && callerFilePath.Contains('\\'))
111+
{
112+
var match = Regex.Match(callerFilePath, @"^([a-zA-Z]):\\(.+)$");
113+
if (match.Success)
114+
{
115+
var driveLetter = match.Groups[1].Value.ToLowerInvariant();
116+
var path = match.Groups[2].Value.Replace('\\', '/');
117+
return $"/mnt/{driveLetter}/{path}";
118+
}
119+
}
120+
121+
return callerFilePath;
122+
}
108123
}

testEnvironments.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "1",
3+
"environments": [
4+
{
5+
"name": "WSL-Ubuntu",
6+
"type": "wsl",
7+
"wslDistribution": "Ubuntu"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)