Skip to content

Commit

Permalink
chore: Add snapshot tests support that are executed on forked repository
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Mar 5, 2024
1 parent f0864a9 commit dd24e9f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/Docfx.Common/Git/GitUtility.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.Collections.Concurrent;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Docfx.Plugins;

Expand Down Expand Up @@ -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('\\', '/'),
};
Expand All @@ -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)
Expand All @@ -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)}",
Expand All @@ -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);
Expand Down Expand Up @@ -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");
}
}
15 changes: 15 additions & 0 deletions src/Docfx.Plugins/EnvironmentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ public static void Clean()
_outputDirectory = null;
FileAbstractLayerImpl = null;
}

/// <summary>
/// Gets whether the current docfx process is running snapshot tests.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool IsSnapshotTestContext
{
get { return _isSnapshotTestContext.Value; }
set { _isSnapshotTestContext = new Lazy<bool>(() => value); }
}

private static Lazy<bool> _isSnapshotTestContext = new Lazy<bool>(() =>
{
return Environment.GetEnvironmentVariable("DOCFX_IS_SNAPSHOT_TEST_CONTEXT") != null;
});
}
19 changes: 18 additions & 1 deletion test/docfx.Snapshot.Tests/SamplesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@
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;
using UglyToad.PdfPig.Outline;

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()
Expand Down
1 change: 1 addition & 0 deletions test/docfx.Tests/Api.verified.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down

0 comments on commit dd24e9f

Please sign in to comment.