Skip to content

Commit a470aa5

Browse files
filzrevyufeih
andauthored
chore: Add snapshot tests support that are executed on forked repository (#9759)
* chore: Add snapshot tests support that are executed on forked repository * chore: Add docs for DOCFX_SOURCE_REPOSITORY * chore: change env variable name from DOCFX_SOURCE_REPOSITORY to DOCFX_SOURCE_REPOSITORY_URL --------- Co-authored-by: Yufei Huang <yufeih@live.com>
1 parent aa982be commit a470aa5

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

docs/reference/docfx-environment-variables-reference.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ If set true. Keep following debug info in output HTML.
1414

1515
Used to override git branch name.
1616

17+
## `DOCFX_SOURCE_REPOSITORY_URL`
18+
19+
Used to override git organization and repository names.
20+
It must be defined in the `https://{host_name}/{organization}/{repository_name}` format (e.g.`https://github.com/dotnet/docfx`).
21+
1722
## `DOCFX_NO_CHECK_CERTIFICATE_REVOCATION_LIST`
1823

1924
Used to disable CRL check.

src/Docfx.Common/Git/GitUtility.cs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ record Repo(string path, string url, string branch);
3838
if (repo is null)
3939
return null;
4040

41+
var repoUrl = ResolveDocfxSourceRepoUrl(repo.url);
42+
4143
return new()
4244
{
43-
Repo = repo.url,
45+
Repo = repoUrl,
4446
Branch = repo.branch,
4547
Path = Path.GetRelativePath(repo.path, filePath).Replace('\\', '/'),
4648
};
@@ -49,10 +51,12 @@ record Repo(string path, string url, string branch);
4951
public static string? RawContentUrlToContentUrl(string rawUrl)
5052
{
5153
// GitHub
52-
return Regex.Replace(
54+
var url = Regex.Replace(
5355
rawUrl,
5456
@"^https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/([^/]+)/(.+)$",
5557
string.IsNullOrEmpty(s_branch) ? "https://github.com/$1/$2/blob/$3/$4" : $"https://github.com/$1/$2/blob/{s_branch}/$4");
58+
59+
return ResolveDocfxSourceRepoUrl(url);
5660
}
5761

5862
public static string? GetSourceUrl(GitSource source)
@@ -65,7 +69,7 @@ record Repo(string path, string url, string branch);
6569

6670
var path = source.Path.Replace('\\', '/');
6771

68-
return url.Host switch
72+
var sourceUrl = url.Host switch
6973
{
7074
"github.com" => $"https://github.com{url.AbsolutePath}/blob/{source.Branch}/{path}{(source.Line > 0 ? $"#L{source.Line}" : null)}",
7175
"bitbucket.org" => $"https://bitbucket.org{url.AbsolutePath}/src/{source.Branch}/{path}{(source.Line > 0 ? $"#lines-{source.Line}" : null)}",
@@ -74,6 +78,11 @@ _ when url.Host.EndsWith(".visualstudio.com") || url.Host == "dev.azure.com" =>
7478
_ => null,
7579
};
7680

81+
if (sourceUrl == null)
82+
return null;
83+
84+
return ResolveDocfxSourceRepoUrl(sourceUrl);
85+
7786
static bool IsCommit(string branch)
7887
{
7988
return branch.Length == 40 && branch.All(char.IsLetterOrDigit);
@@ -173,4 +182,46 @@ static string GitUrlToHttps(string url)
173182
}
174183
}
175184
}
185+
186+
/// <summary>
187+
/// Rewrite path if `DOCFX_SOURCE_REPOSITORY_URL` environment variable is specified.
188+
/// </summary>
189+
private static string ResolveDocfxSourceRepoUrl(string originalUrl)
190+
{
191+
var docfxSourceRepoUrl = Environment.GetEnvironmentVariable("DOCFX_SOURCE_REPOSITORY_URL");
192+
if (docfxSourceRepoUrl == null)
193+
return originalUrl;
194+
195+
if (!Uri.TryCreate(originalUrl, UriKind.Absolute, out var parsedOriginalUrl)
196+
|| !Uri.TryCreate(docfxSourceRepoUrl, UriKind.Absolute, out var parsedOverrideUrl)
197+
|| parsedOriginalUrl.Host != parsedOverrideUrl.Host)
198+
{
199+
return originalUrl;
200+
}
201+
202+
// Parse value that defined with `{orgName}/{repoName}` format.
203+
var parts = parsedOverrideUrl.LocalPath.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
204+
if (parts.Length < 2)
205+
return originalUrl;
206+
207+
string orgName = parts[0];
208+
string repoName = parts[1];
209+
210+
switch (parsedOriginalUrl.Host)
211+
{
212+
case "github.com":
213+
case "bitbucket.org":
214+
case "dev.azure.com":
215+
{
216+
// Replace `/{orgName}/{repoName}` and remove `.git` suffix.
217+
var builder = new UriBuilder(parsedOriginalUrl);
218+
builder.Path = Regex.Replace(builder.Path.TrimEnd(".git"), @"^/[^/]+/[^/]+", $"/{orgName}/{repoName}");
219+
return builder.Uri.ToString();
220+
}
221+
222+
// Currently other URL formats are not supported (e.g. visualstudio.com, GitHub Enterprise Server)
223+
default:
224+
return originalUrl;
225+
}
226+
}
176227
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Docfx.Common.Git;
5+
using Xunit;
6+
7+
namespace Docfx.Common.Tests;
8+
9+
[Collection("docfx STA")]
10+
public class GitUtilityWithSourceRepositoryUrlTest : IDisposable
11+
{
12+
private readonly string _originalBranchName;
13+
private readonly string _originalSourceRepoUrl;
14+
15+
private const string ORG_NAME = "dotnet";
16+
private const string REPO_NAME = "docfx";
17+
18+
private const string BRANCH_NAME = "special-branch";
19+
private const string DOCFX_SOURCE_BRANCH_NAME = nameof(DOCFX_SOURCE_BRANCH_NAME);
20+
private const string DOCFX_SOURCE_REPOSITORY_URL = nameof(DOCFX_SOURCE_REPOSITORY_URL);
21+
22+
public GitUtilityWithSourceRepositoryUrlTest()
23+
{
24+
_originalBranchName = Environment.GetEnvironmentVariable(DOCFX_SOURCE_BRANCH_NAME);
25+
_originalSourceRepoUrl = Environment.GetEnvironmentVariable(DOCFX_SOURCE_REPOSITORY_URL);
26+
27+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_BRANCH_NAME, BRANCH_NAME);
28+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_REPOSITORY_URL, $"https://github.com/{ORG_NAME}/{REPO_NAME}");
29+
}
30+
31+
public void Dispose()
32+
{
33+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_BRANCH_NAME, _originalBranchName);
34+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_REPOSITORY_URL, _originalSourceRepoUrl);
35+
}
36+
37+
[Fact]
38+
public void TryGetFileDetailTest()
39+
{
40+
var info = GitUtility.TryGetFileDetail(Directory.GetCurrentDirectory());
41+
Assert.Equal(BRANCH_NAME, info.Branch);
42+
Assert.Equal("https://github.com/dotnet/docfx", info.Repo);
43+
}
44+
45+
[Fact]
46+
public void RawContentUrlToContentUrlTest()
47+
{
48+
string rawUrl = "https://raw.githubusercontent.com/dotnet/docfx/main/README.md";
49+
string expected = "https://github.com/dotnet/docfx/blob/special-branch/README.md";
50+
51+
var result = GitUtility.RawContentUrlToContentUrl(rawUrl);
52+
53+
Assert.Equal(expected, result);
54+
}
55+
56+
[Theory]
57+
[InlineData("https://github.com/user/repo.git", "main", "path/to/file.cs", 0, $"https://github.com/{ORG_NAME}/{REPO_NAME}/blob/main/path/to/file.cs")]
58+
[InlineData("https://github.com/user/repo.git", "main", "path/to/file.cs", 10, $"https://github.com/{ORG_NAME}/{REPO_NAME}/blob/main/path/to/file.cs#L10")]
59+
[InlineData("git@github.com:user/repo.git", "main", "path/to/file.cs", 0, $"https://github.com/{ORG_NAME}/{REPO_NAME}/blob/main/path/to/file.cs")]
60+
public void GetSourceUrlTest_GitHub(string repo, string branch, string path, int line, string result)
61+
{
62+
Assert.Equal(result, GitUtility.GetSourceUrl(new(repo, branch, path, line)));
63+
}
64+
}

test/docfx.Snapshot.Tests/SamplesTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@
88
using System.Text.Json.Nodes;
99
using System.Text.Json.Serialization;
1010
using Docfx.Dotnet;
11+
using Docfx.Plugins;
1112
using UglyToad.PdfPig;
1213
using UglyToad.PdfPig.Actions;
1314
using UglyToad.PdfPig.Annotations;
1415
using UglyToad.PdfPig.Outline;
1516

1617
namespace Docfx.Tests;
1718

18-
public class SamplesTest
19+
[Collection("docfx STA")]
20+
[Trait("Stage", "Snapshot")]
21+
public class SamplesTest : IDisposable
1922
{
2023
private static readonly string s_samplesDir = Path.GetFullPath("../../../../../samples");
2124

25+
private const string DOCFX_SOURCE_REPOSITORY_URL = nameof(DOCFX_SOURCE_REPOSITORY_URL);
26+
27+
public SamplesTest()
28+
{
29+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_REPOSITORY_URL, "https://github.com/dotnet/docfx");
30+
}
31+
32+
public void Dispose()
33+
{
34+
Environment.SetEnvironmentVariable(DOCFX_SOURCE_REPOSITORY_URL, null);
35+
}
36+
2237
private class SamplesFactAttribute : FactAttribute
2338
{
2439
public SamplesFactAttribute()

0 commit comments

Comments
 (0)