Skip to content

Commit

Permalink
Preserve trailing slash in file spec for directory
Browse files Browse the repository at this point in the history
This enhances consistency with the `/` meaning "start relative path from here on", which can now be used consistently in both the path and url.
  • Loading branch information
kzu committed Oct 19, 2024
1 parent dd8024d commit af111fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/File/FileSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,22 @@ public FileSpec(string path, Uri? uri = null, string? etag = null, string? sha =
Sha = sha;
NewSha = sha;

if (!finalPath && uri != null &&
if (!finalPath && uri != null && !uri.AbsolutePath.EndsWith('/') &&
(path.EndsWith('\\') || path.EndsWith('/')))
{
path = System.IO.Path.Combine(path, WithDefaultPath(uri!).Path);
}

// This will also normalize double slashes.
var parts = path.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
var parts = path.Split(['\\', '/'], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0 && parts[0] == ".")
Path = string.Join('/', parts.Skip(1));
else
Path = string.Join('/', parts);

// Preserve trailing slash if present, for consistency with url ending in /.
if (path.EndsWith('/') || path.EndsWith("\\"))
Path += "/";
}

public string Path { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/File/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static async Task<int> Main(string[] args)
}

// Try to pair Uri+File to allow intuitive download>path mapping, such as
// https://gitub.com/org/repo/docs/file.md docs/file.md
// https://gitub.com/org/repo/docs/file.md > docs/file.md
if (Uri.TryCreate(extraArgs[i], UriKind.Absolute, out var uri))
{
var next = i + 1;
Expand Down
11 changes: 10 additions & 1 deletion src/Tests/FileSpecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ public void WhenPathIsDirAppendsDefaultPath(string url, string path, string expe
[Fact]
public void WhenSourceUriEndsInSlathThenTargetPathIsBaseDir()
{
var spec = new FileSpec("docs/", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));
var spec = new FileSpec("docs", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));

// NOTE: the relative `src/api/documentation` is not appended to the file path.
Assert.Equal("docs", spec.Path);
}

[Fact]
public void WhenSpecAndSourceUriEndInSlathThenPreservesBaseDirSlash()
{
var spec = new FileSpec("docs/", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));

// NOTE: the relative `src/api/documentation` is not appended to the file path.
Assert.Equal("docs/", spec.Path);
}

[Theory]
[InlineData("https://github.com/devlooped/dotnet-file/blob/main/src/Foo.cs", ".", "Foo.cs")]
[InlineData("https://github.com/devlooped/dotnet-file/blob/main/src/Foo.cs", "src/External/.", "src/External/Foo.cs")]
Expand Down

0 comments on commit af111fb

Please sign in to comment.