From dd24e9fa084b69c15d50adb935a0f9c09c946aa1 Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Tue, 5 Mar 2024 21:39:28 +0900 Subject: [PATCH] chore: Add snapshot tests support that are executed on forked repository --- src/Docfx.Common/Git/GitUtility.cs | 33 +++++++++++++++++++++--- src/Docfx.Plugins/EnvironmentContext.cs | 15 +++++++++++ test/docfx.Snapshot.Tests/SamplesTest.cs | 19 +++++++++++++- test/docfx.Tests/Api.verified.cs | 1 + 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Docfx.Common/Git/GitUtility.cs b/src/Docfx.Common/Git/GitUtility.cs index ca3ed56ddb3..43a3ef89a76 100644 --- a/src/Docfx.Common/Git/GitUtility.cs +++ b/src/Docfx.Common/Git/GitUtility.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Concurrent; +using System.Diagnostics; using System.Text.RegularExpressions; using Docfx.Plugins; @@ -38,9 +39,13 @@ record Repo(string path, string url, string branch); if (repo is null) return null; + var repoUrl = EnvironmentContext.IsSnapshotTestContext + ? RewritePathForSnapshotTest(repo.url) + : repo.url; + return new() { - Repo = repo.url, + Repo = repoUrl, Branch = repo.branch, Path = Path.GetRelativePath(repo.path, filePath).Replace('\\', '/'), }; @@ -49,10 +54,14 @@ record Repo(string path, string url, string branch); public static string? RawContentUrlToContentUrl(string rawUrl) { // GitHub - return Regex.Replace( + var url = Regex.Replace( rawUrl, @"^https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/([^/]+)/(.+)$", string.IsNullOrEmpty(s_branch) ? "/~https://github.com/$1/$2/blob/$3/$4" : $"/~https://github.com/$1/$2/blob/{s_branch}/$4"); + + return EnvironmentContext.IsSnapshotTestContext + ? RewritePathForSnapshotTest(url) + : url; } public static string? GetSourceUrl(GitSource source) @@ -65,7 +74,7 @@ record Repo(string path, string url, string branch); var path = source.Path.Replace('\\', '/'); - return url.Host switch + var sourceUrl = url.Host switch { "github.com" => $"https://github.com{url.AbsolutePath}/blob/{source.Branch}/{path}{(source.Line > 0 ? $"#L{source.Line}" : null)}", "bitbucket.org" => $"https://bitbucket.org{url.AbsolutePath}/src/{source.Branch}/{path}{(source.Line > 0 ? $"#lines-{source.Line}" : null)}", @@ -74,6 +83,13 @@ _ when url.Host.EndsWith(".visualstudio.com") || url.Host == "dev.azure.com" => _ => null, }; + if (sourceUrl == null) + return null; + + return EnvironmentContext.IsSnapshotTestContext + ? RewritePathForSnapshotTest(sourceUrl) + : sourceUrl; + static bool IsCommit(string branch) { return branch.Length == 40 && branch.All(char.IsLetterOrDigit); @@ -173,4 +189,15 @@ static string GitUrlToHttps(string url) } } } + + private static string RewritePathForSnapshotTest(string path) + { + Debug.Assert(EnvironmentContext.IsSnapshotTestContext); + + // Remove trailing `.git`. + path = path.TrimEnd(".git"); + + // Replace `/{organization}/docfx` path to `/dotnet/docfx`. + return Regex.Replace(path, @"/[^/]+/docfx", $"/dotnet/docfx"); + } } diff --git a/src/Docfx.Plugins/EnvironmentContext.cs b/src/Docfx.Plugins/EnvironmentContext.cs index b6d1a9de8dc..3bf710e329f 100644 --- a/src/Docfx.Plugins/EnvironmentContext.cs +++ b/src/Docfx.Plugins/EnvironmentContext.cs @@ -56,4 +56,19 @@ public static void Clean() _outputDirectory = null; FileAbstractLayerImpl = null; } + + /// + /// Gets whether the current docfx process is running snapshot tests. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static bool IsSnapshotTestContext + { + get { return _isSnapshotTestContext.Value; } + set { _isSnapshotTestContext = new Lazy(() => value); } + } + + private static Lazy _isSnapshotTestContext = new Lazy(() => + { + return Environment.GetEnvironmentVariable("DOCFX_IS_SNAPSHOT_TEST_CONTEXT") != null; + }); } diff --git a/test/docfx.Snapshot.Tests/SamplesTest.cs b/test/docfx.Snapshot.Tests/SamplesTest.cs index f0e0f925195..21f9975c01f 100644 --- a/test/docfx.Snapshot.Tests/SamplesTest.cs +++ b/test/docfx.Snapshot.Tests/SamplesTest.cs @@ -8,6 +8,7 @@ using System.Text.Json.Nodes; using System.Text.Json.Serialization; using Docfx.Dotnet; +using Docfx.Plugins; using UglyToad.PdfPig; using UglyToad.PdfPig.Actions; using UglyToad.PdfPig.Annotations; @@ -15,10 +16,26 @@ namespace Docfx.Tests; -public class SamplesTest +[Collection("docfx STA")] +[Trait("Stage", "Snapshot")] +public class SamplesTest : IDisposable { private static readonly string s_samplesDir = Path.GetFullPath("../../../../../samples"); + private const string DOCFX_IS_SNAPSHOT_TEST_CONTEXT = nameof(DOCFX_IS_SNAPSHOT_TEST_CONTEXT); + + public SamplesTest() + { + EnvironmentContext.IsSnapshotTestContext = true; // Used for tests that are executed in-process. + Environment.SetEnvironmentVariable(DOCFX_IS_SNAPSHOT_TEST_CONTEXT, "true"); // Used for tests that launch child process. + } + + public void Dispose() + { + EnvironmentContext.IsSnapshotTestContext = false; + Environment.SetEnvironmentVariable(DOCFX_IS_SNAPSHOT_TEST_CONTEXT, null); + } + private class SamplesFactAttribute : FactAttribute { public SamplesFactAttribute() diff --git a/test/docfx.Tests/Api.verified.cs b/test/docfx.Tests/Api.verified.cs index 0933d8366bb..62caef1305d 100644 --- a/test/docfx.Tests/Api.verified.cs +++ b/test/docfx.Tests/Api.verified.cs @@ -3950,6 +3950,7 @@ public static class EnvironmentContext public static bool GitFeaturesDisabled { get; } public static string OutputDirectory { get; } public static Docfx.Plugins.IFileAbstractLayer FileAbstractLayerImpl { get; set; } + public static bool IsSnapshotTestContext { get; set; } public static void Clean() { } public static void SetBaseDirectory(string dir) { } public static void SetGitFeaturesDisabled(bool disabled) { }