Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add remote testing settings for WSL environment #10485

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/Docfx.Build.Tests/XRefMapSerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Text;
using Docfx.Common;
using Docfx.Plugins;
using FluentAssertions;
using Xunit;

Expand Down
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.
}
}
49 changes: 32 additions & 17 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,7 +52,7 @@ 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}");
}
Expand All @@ -71,18 +65,13 @@ public static string ResolveTestDataPath(string path = "", [CallerFilePath] stri
/// </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"
}
]
}
Loading