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
3 changes: 3 additions & 0 deletions src/Ramstack.FileProviders/PrefixedFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public void Dispose() =>
{
Debug.Assert(path == FilePath.Normalize(path));

if (prefix == "/")
return path;

if (path == prefix)
return "/";

Expand Down
53 changes: 53 additions & 0 deletions tests/Ramstack.FileProviders.Tests/PrefixedFileProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public sealed class PrefixedFileProviderTests : AbstractFileProviderTests
.GetMethod("ResolveGlobFilter", BindingFlags.Static | BindingFlags.NonPublic)!
.CreateDelegate<Func<string, string, string?>>();

private static readonly Func<string, string, string?> s_resolvePath =
typeof(PrefixedFileProvider)
.GetMethod("ResolvePath", BindingFlags.Static | BindingFlags.NonPublic)!
.CreateDelegate<Func<string, string, string?>>();

private readonly TempFileStorage _storage = new TempFileStorage();

protected override IFileProvider GetFileProvider() =>
Expand All @@ -37,4 +42,52 @@ protected override DirectoryInfo GetDirectoryInfo() =>
[TestCase("/modules/profile/assets", "/*.js", ExpectedResult = null)]
public string? ResolveGlobFilter(string prefix, string filter) =>
s_resolveGlobFilter(prefix, filter);

[TestCase("/", "/", ExpectedResult = "/")]
[TestCase("/", "/foo", ExpectedResult = "/foo")]
[TestCase("/", "/a/b/c", ExpectedResult = "/a/b/c")]

[TestCase("/a/b", "/a/b", ExpectedResult = "/")]

[TestCase("/a/b", "/a/b/c", ExpectedResult = "/c")]
[TestCase("/a/b", "/a/b/c/d", ExpectedResult = "/c/d")]

[TestCase("/a/b", "/a/bc", ExpectedResult = null)]

[TestCase("/a/b", "/a/c", ExpectedResult = null)]
[TestCase("/a/b", "/a", ExpectedResult = null)]
public string? ResolvePath(string prefix, string path) =>
s_resolvePath(prefix, path);

[Test]
public void GetFileInfo_RootPrefix_DelegatesToInnerProvider()
{
using var provider = new PrefixedFileProvider("/",
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));

var file = provider.GetFileInfo("/project/README.md");
Assert.That(file.Exists, Is.True);
Assert.That(file.IsDirectory, Is.False);
}

[Test]
public void GetDirectoryContents_RootPrefix_DelegatesToInnerProvider()
{
using var provider = new PrefixedFileProvider("/",
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));

var contents = provider.GetDirectoryContents("/project/docs");
Assert.That(contents.Exists, Is.True);
Assert.That(contents.Any(), Is.True);
}

[Test]
public void GetFileInfo_RootPrefix_MissingFile_ReturnsNotFound()
{
using var provider = new PrefixedFileProvider("/",
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));

var file = provider.GetFileInfo("/project/nonexistent.txt");
Assert.That(file.Exists, Is.False);
}
}
Loading