Skip to content

Commit

Permalink
chore: Add remote testing settings for WSL environment
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Dec 29, 2024
1 parent 0cceb23 commit f8d9c6d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
12 changes: 11 additions & 1 deletion test/docfx.Snapshot.Tests/SamplesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"]));
Expand Down Expand Up @@ -229,4 +230,13 @@ private string ExtractText(Page page)

return sb.ToString();
}

/// <summary>
/// Returns true if running on WSL and executed on Visual Studio Remote Testing.
/// </summary>
private static bool IsWslRemoteTest([CallerFilePath] string callerFilePath = "")
{
return Environment.GetEnvironmentVariable("WSLENV") != null
&& callerFilePath.Contains('\\', StringComparison.Ordinal); // Contains `\` when build on windows environment.
}
}
51 changes: 33 additions & 18 deletions test/docfx.Tests/Utilities/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace Docfx.Tests;

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");
Expand Down Expand Up @@ -58,31 +52,26 @@ 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);
}

/// <summary>
/// Find TestData from callerFilePath.
/// </summary>
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.
Expand All @@ -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;
}
}
10 changes: 10 additions & 0 deletions testEnvironments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "1",
"environments": [
{
"name": "WSL-Ubuntu",
"type": "wsl",
"wslDistribution": "Ubuntu"
}
]
}

0 comments on commit f8d9c6d

Please sign in to comment.