Skip to content
Merged
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
40 changes: 40 additions & 0 deletions GitBranchStateCache.Tests/Tool/MirrorRootFlagTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.GitBranchStateCache.Tests.Tool;

using ktsu.GitBranchStateCache.Tool;

[TestClass]
public class MirrorRootFlagTests
{
[TestMethod]
public void MirrorRoot_RelativeDirectory_ResolvesAgainstTheWorkingDirectory()
{
// The options validator refuses a root that is not fully qualified, so passing
// `--mirror-root ./mirrors` through verbatim aborted startup with a validation stack trace.
string resolved = Program.ResolveMirrorRoot("./mirrors");

Assert.IsTrue(Path.IsPathFullyQualified(resolved), resolved);
Assert.AreEqual(Path.Combine(Environment.CurrentDirectory, "mirrors"), resolved);
}

[TestMethod]
public async Task MirrorRoot_RelativeDirectory_IsAcceptedByTheCommandLine()
{
// The invalid upstream stops the run after the flags are read and before a server starts, so a
// relative --mirror-root reaches the same one-line failure path as any other flag mistake.
int exitCode = await Program.Main(["--mirror-root", "./mirrors", "--upstream", "not-a-name-url-pair"]);

Assert.AreEqual(1, exitCode);
}

[TestMethod]
public void MirrorRoot_FullyQualifiedDirectory_IsKeptAsGiven()
{
string root = Path.Combine(
Path.GetPathRoot(Path.GetTempPath()) ?? Path.DirectorySeparatorChar.ToString(),
"gitbranchstatecache");

Assert.AreEqual(root, Program.ResolveMirrorRoot(root));
}
}
18 changes: 16 additions & 2 deletions GitBranchStateCache.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/// <summary>The configuration key the <c>--mirror-root</c> flag overrides.</summary>
private const string MirrorRootKey = "GitBranchStateCache:MirrorRoot";

private static async Task<int> Main(string[] args)
internal static async Task<int> Main(string[] args)
{
Option<int?> port = new("--port", "-p")
{
Expand Down Expand Up @@ -94,7 +94,7 @@

if (parseResult.GetValue(mirrorRoot) is string root_)
{
overrides[MirrorRootKey] = root_;
overrides[MirrorRootKey] = ResolveMirrorRoot(root_);
}

if (parseResult.GetValue(gitExecutable) is string git)
Expand Down Expand Up @@ -153,7 +153,7 @@

ApplyDefaults(builder.Configuration, overrides);
builder.Configuration.AddInMemoryCollection(overrides);
builder.Configuration["Kestrel:Endpoints:Http:Url"] = $"http://*:{port}";

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

Check warning on line 156 in GitBranchStateCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Using http protocol is insecure. Use https instead.

builder.Services.AddGitBranchStateCache(builder.Configuration);
builder.Services.PostConfigure<GitBranchStateCacheOptions>(options => ReplaceAllowLists(options, allowLists));
Expand Down Expand Up @@ -266,6 +266,20 @@
return true;
}

/// <summary>
/// Resolves the <c>--mirror-root</c> flag against the working directory.
/// </summary>
/// <remarks>
/// The options validator requires a fully qualified root, which keeps configuration files and
/// environment variables strict for container deployments. A relative directory is what someone
/// running the tool locally types, though, so the flag resolves it the way <c>--config</c> does
/// rather than letting startup fail with a validation stack trace. This is the same behaviour as
/// <c>--store</c> in <c>ktsu.GitLfsCache</c>.
/// </remarks>
/// <param name="mirrorRoot">The directory as given on the command line.</param>
/// <returns>The fully qualified directory.</returns>
internal static string ResolveMirrorRoot(string mirrorRoot) => Path.GetFullPath(mirrorRoot);

/// <summary>
/// Groups every <c>--allow</c> flag by the upstream it names.
/// </summary>
Expand Down
Loading