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 GitLfsCache.Tests/Tool/StoreFlagTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.GitLfsCache.Tests.Tool;

using ktsu.GitLfsCache.Tool;

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

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

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

Assert.AreEqual(1, exitCode);
}

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

Assert.AreEqual(root, Program.ResolveStoreRoot(root));
}
}
17 changes: 15 additions & 2 deletions GitLfsCache.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/// <summary>The configuration key the <c>--token-key</c> flag overrides.</summary>
private const string TokenKeyKey = "GitLfsCache:TokenKeys:0";

private static async Task<int> Main(string[] args)
internal static async Task<int> Main(string[] args)

Check warning on line 32 in GitLfsCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 32 in GitLfsCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 32 in GitLfsCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 32 in GitLfsCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 32 in GitLfsCache.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
{
Option<int?> port = new("--port", "-p")
{
Expand Down Expand Up @@ -110,7 +110,7 @@

if (parseResult.GetValue(store) is string storeRoot)
{
overrides[StoreRootKey] = storeRoot;
overrides[StoreRootKey] = ResolveStoreRoot(storeRoot);
}

if (parseResult.GetValue(maxSize) is string budget)
Expand Down Expand Up @@ -179,7 +179,7 @@

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

Check warning on line 182 in GitLfsCache.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 182 in GitLfsCache.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 182 in GitLfsCache.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 182 in GitLfsCache.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 182 in GitLfsCache.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.AddGitLfsCache(builder.Configuration);
builder.Services.PostConfigure<GitLfsCacheOptions>(options => ReplaceAllowLists(options, allowLists));
Expand Down Expand Up @@ -285,6 +285,19 @@
return true;
}

/// <summary>
/// Resolves the <c>--store</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.
/// </remarks>
/// <param name="storeRoot">The directory as given on the command line.</param>
/// <returns>The fully qualified directory.</returns>
internal static string ResolveStoreRoot(string storeRoot) => Path.GetFullPath(storeRoot);

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