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
8 changes: 6 additions & 2 deletions src/Ramstack.FileSystem.Prefixed/PrefixedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ internal string WrapWithPrefix(string underlyingPath)
{
Debug.Assert(VirtualPath.IsNormalized(path));

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

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

if (path.StartsWith(prefix, StringComparison.Ordinal) && path[prefix.Length] == '/')
return new string(path.AsSpan(prefix.Length));
if ((uint)prefix.Length < (uint)path.Length)
if (path.StartsWith(prefix, StringComparison.Ordinal) && path[prefix.Length] == '/')
return new string(path.AsSpan(prefix.Length));

return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;

using Ramstack.FileSystem.Physical;
using Ramstack.FileSystem.Specification.Tests;
using Ramstack.FileSystem.Specification.Tests.Utilities;
Expand All @@ -7,8 +9,59 @@ namespace Ramstack.FileSystem.Prefixed;
[TestFixture]
public class PrefixedFileSystemTests : VirtualFileSystemSpecificationTests
{
private static readonly Func<string, string, string?> s_unwrapPrefix =
typeof(PrefixedFileSystem)
.GetMethod("TryUnwrapPrefix", BindingFlags.Static | BindingFlags.NonPublic)!
.CreateDelegate<Func<string, string, string?>>();

private readonly TempFileStorage _storage = new TempFileStorage();

[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? UnwrapPrefix(string prefix, string path) =>
s_unwrapPrefix(path, prefix);

[Test]
public async Task File_RootPrefix_DelegatesToInnerProvider()
{
using var fs = new PrefixedFileSystem("/",
new PhysicalFileSystem(_storage.Root));

var file = fs.GetFile("/project/README.md");
Assert.That(await file.ExistsAsync(), Is.True);
}

[Test]
public async Task File_RootPrefix_MissingFile_ReturnsNotFound()
{
using var fs = new PrefixedFileSystem("/",
new PhysicalFileSystem(_storage.Root));

var file = fs.GetFile("/project/nonexistent.txt");
Assert.That(await file.ExistsAsync(), Is.False);
}

[Test]
public async Task Directory_RootPrefix_DelegatesToInnerProvider()
{
using var fs = new PrefixedFileSystem("/",
new PhysicalFileSystem(_storage.Root));

var file = fs.GetDirectory("/project");
Assert.That(await file.ExistsAsync(), Is.True);
}

[Test]
public async Task File_Create_InsideArtificialDirectory_ThrowsException()
{
Expand Down
Loading