From f8d9c6dcddfafa1decfc9085e00be762d6dacad0 Mon Sep 17 00:00:00 2001
From: filzrev <103790468+filzrev@users.noreply.github.com>
Date: Mon, 30 Dec 2024 08:37:34 +0900
Subject: [PATCH] chore: Add remote testing settings for WSL environment
---
test/docfx.Snapshot.Tests/SamplesTest.cs | 12 +++++-
test/docfx.Tests/Utilities/PathHelper.cs | 51 +++++++++++++++---------
testEnvironments.json | 10 +++++
3 files changed, 54 insertions(+), 19 deletions(-)
create mode 100644 testEnvironments.json
diff --git a/test/docfx.Snapshot.Tests/SamplesTest.cs b/test/docfx.Snapshot.Tests/SamplesTest.cs
index 1e3d32aefc3..da72625b456 100644
--- a/test/docfx.Snapshot.Tests/SamplesTest.cs
+++ b/test/docfx.Snapshot.Tests/SamplesTest.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
+using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
@@ -56,7 +57,7 @@ public async Task Seed()
using var process = Process.Start("dotnet", $"build \"{s_samplesDir}/seed/dotnet/assembly/BuildFromAssembly.csproj\"");
await process.WaitForExitAsync();
- if (Debugger.IsAttached)
+ if (Debugger.IsAttached || IsWslRemoteTest())
{
Environment.SetEnvironmentVariable("DOCFX_SOURCE_BRANCH_NAME", "main");
Assert.Equal(0, Program.Main([$"{samplePath}/docfx.json"]));
@@ -229,4 +230,13 @@ private string ExtractText(Page page)
return sb.ToString();
}
+
+ ///
+ /// Returns true if running on WSL and executed on Visual Studio Remote Testing.
+ ///
+ private static bool IsWslRemoteTest([CallerFilePath] string callerFilePath = "")
+ {
+ return Environment.GetEnvironmentVariable("WSLENV") != null
+ && callerFilePath.Contains('\\', StringComparison.Ordinal); // Contains `\` when build on windows environment.
+ }
}
diff --git a/test/docfx.Tests/Utilities/PathHelper.cs b/test/docfx.Tests/Utilities/PathHelper.cs
index 05fd58838b0..97d95554faa 100644
--- a/test/docfx.Tests/Utilities/PathHelper.cs
+++ b/test/docfx.Tests/Utilities/PathHelper.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.CompilerServices;
+using System.Text.RegularExpressions;
namespace Docfx.Tests;
@@ -9,20 +10,13 @@ internal class PathHelper
{
public static string GetSolutionFolder([CallerFilePath] string callerFilePath = "")
{
- if (callerFilePath.StartsWith("/_/"))
- {
- // PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
- // So try to get workspace folder from GitHub Action environment variable.
- var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
- if (workspace != null)
- return workspace;
- }
+ callerFilePath = NormalizeCallerFilePath(callerFilePath);
if (!File.Exists(callerFilePath))
{
// CallerFilePath is resolved at build timing.
// If build/test is executed on separated machine. It failed to find file.
- throw new FileNotFoundException($"File is not found. callerFilePath: {callerFilePath}");
+ throw new FileNotFoundException($"File is not found. path: {callerFilePath}");
}
return FindSolutionFolder(callerFilePath, "docfx");
@@ -58,12 +52,12 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
var dir = GetTestDataDirectory(callerFilePath);
var resultPath = Path.Combine(dir, path);
- if (!File.Exists(callerFilePath) && !Directory.Exists(callerFilePath))
+ if (!File.Exists(resultPath) && !Directory.Exists(resultPath))
{
throw new FileNotFoundException($"Specified TestData file/directory is not found. path: {resultPath}");
}
- return Path.GetFullPath(resultPath);
+ return Path.GetFullPath(path);
}
///
@@ -71,18 +65,13 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
///
public static string GetTestDataDirectory([CallerFilePath] string callerFilePath = "")
{
- if (callerFilePath.StartsWith("/_/"))
- {
- var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
- if (workspace != null)
- callerFilePath = callerFilePath.Replace("/_/", workspace);
- }
+ callerFilePath = NormalizeCallerFilePath(callerFilePath);
if (!File.Exists(callerFilePath))
{
// CallerFilePath is resolved at build timing.
// If build/test is executed on separated machine. It failed to find file.
- throw new FileNotFoundException($"File is not found. callerFilePath: {callerFilePath}");
+ throw new FileNotFoundException($"File is not found. path: {callerFilePath}");
}
// Find closest `TestData` directory.
@@ -105,4 +94,30 @@ public static string GetTestDataDirectory([CallerFilePath] string callerFilePath
return dir.FullName;
}
+
+ private static string NormalizeCallerFilePath(string callerFilePath)
+ {
+ // PathMap is rewritten on CI environment (`ContinuousIntegrationBuild=true`).
+ // So try to get workspace folder from GitHub Action environment variable.
+ if (callerFilePath.StartsWith("/_/"))
+ {
+ var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
+ if (workspace != null)
+ return workspace;
+ }
+
+ // Rewrite path when test runnign on WSL environment that are executed by Visual Studio Remote Testing.
+ if (Environment.GetEnvironmentVariable("WSLENV") != null && callerFilePath.Contains('\\'))
+ {
+ var match = Regex.Match(callerFilePath, @"^([a-zA-Z]):\\(.+)$");
+ if (match.Success)
+ {
+ var driveLetter = match.Groups[1].Value.ToLowerInvariant();
+ var path = match.Groups[2].Value.Replace('\\', '/');
+ return $"/mnt/{driveLetter}/{path}";
+ }
+ }
+
+ return callerFilePath;
+ }
}
diff --git a/testEnvironments.json b/testEnvironments.json
new file mode 100644
index 00000000000..18076edf10c
--- /dev/null
+++ b/testEnvironments.json
@@ -0,0 +1,10 @@
+{
+ "version": "1",
+ "environments": [
+ {
+ "name": "WSL-Ubuntu",
+ "type": "wsl",
+ "wslDistribution": "Ubuntu"
+ }
+ ]
+}