Skip to content

Commit 46620d6

Browse files
authored
Merge pull request #45 from rameel/fix-prefix-path-resolution
PrefixedFileProvider: Forward all paths unchanged when prefix is "/"
2 parents 633e675 + da172f8 commit 46620d6

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/Ramstack.FileProviders/PrefixedFileProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public void Dispose() =>
9898
{
9999
Debug.Assert(path == FilePath.Normalize(path));
100100

101+
if (prefix == "/")
102+
return path;
103+
101104
if (path == prefix)
102105
return "/";
103106

tests/Ramstack.FileProviders.Tests/PrefixedFileProviderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public sealed class PrefixedFileProviderTests : AbstractFileProviderTests
1212
.GetMethod("ResolveGlobFilter", BindingFlags.Static | BindingFlags.NonPublic)!
1313
.CreateDelegate<Func<string, string, string?>>();
1414

15+
private static readonly Func<string, string, string?> s_resolvePath =
16+
typeof(PrefixedFileProvider)
17+
.GetMethod("ResolvePath", BindingFlags.Static | BindingFlags.NonPublic)!
18+
.CreateDelegate<Func<string, string, string?>>();
19+
1520
private readonly TempFileStorage _storage = new TempFileStorage();
1621

1722
protected override IFileProvider GetFileProvider() =>
@@ -37,4 +42,52 @@ protected override DirectoryInfo GetDirectoryInfo() =>
3742
[TestCase("/modules/profile/assets", "/*.js", ExpectedResult = null)]
3843
public string? ResolveGlobFilter(string prefix, string filter) =>
3944
s_resolveGlobFilter(prefix, filter);
45+
46+
[TestCase("/", "/", ExpectedResult = "/")]
47+
[TestCase("/", "/foo", ExpectedResult = "/foo")]
48+
[TestCase("/", "/a/b/c", ExpectedResult = "/a/b/c")]
49+
50+
[TestCase("/a/b", "/a/b", ExpectedResult = "/")]
51+
52+
[TestCase("/a/b", "/a/b/c", ExpectedResult = "/c")]
53+
[TestCase("/a/b", "/a/b/c/d", ExpectedResult = "/c/d")]
54+
55+
[TestCase("/a/b", "/a/bc", ExpectedResult = null)]
56+
57+
[TestCase("/a/b", "/a/c", ExpectedResult = null)]
58+
[TestCase("/a/b", "/a", ExpectedResult = null)]
59+
public string? ResolvePath(string prefix, string path) =>
60+
s_resolvePath(prefix, path);
61+
62+
[Test]
63+
public void GetFileInfo_RootPrefix_DelegatesToInnerProvider()
64+
{
65+
using var provider = new PrefixedFileProvider("/",
66+
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));
67+
68+
var file = provider.GetFileInfo("/project/README.md");
69+
Assert.That(file.Exists, Is.True);
70+
Assert.That(file.IsDirectory, Is.False);
71+
}
72+
73+
[Test]
74+
public void GetDirectoryContents_RootPrefix_DelegatesToInnerProvider()
75+
{
76+
using var provider = new PrefixedFileProvider("/",
77+
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));
78+
79+
var contents = provider.GetDirectoryContents("/project/docs");
80+
Assert.That(contents.Exists, Is.True);
81+
Assert.That(contents.Any(), Is.True);
82+
}
83+
84+
[Test]
85+
public void GetFileInfo_RootPrefix_MissingFile_ReturnsNotFound()
86+
{
87+
using var provider = new PrefixedFileProvider("/",
88+
new PhysicalFileProvider(_storage.Root, ExclusionFilters.None));
89+
90+
var file = provider.GetFileInfo("/project/nonexistent.txt");
91+
Assert.That(file.Exists, Is.False);
92+
}
4093
}

0 commit comments

Comments
 (0)