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
6 changes: 3 additions & 3 deletions src/Ramstack.FileSystem.Abstractions/VirtualPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ public static string Normalize(string path)
// Unwind back to the last separator
index = buffer[..index].LastIndexOf('/');

// Path.GetFullPath in this case does not throw an exception,
// it simply clears out the buffer.
// If no separator is found, reset to start
// (mimics Path.GetFullPath behavior)
if (index < 0)
Error_InvalidPath();
index = 0;
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Ramstack.FileSystem.Abstractions.Tests/VirtualPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ public bool IsNormalized(string path) =>
[TestCase("/home/../home/user//documents", "/home/user/documents")]
[TestCase("/home/../home/user/../../home/config/documents", "/home/config/documents")]
[TestCase("/home/../home/user/./.././.././home/config/documents", "/home/config/documents")]
[TestCase("..", "/")]
[TestCase("../..", "/")]
[TestCase("../../../", "/")]
[TestCase("/home/../..", "/")]
[TestCase("/home/../../..", "/")]
public void Normalize(string path, string expected)
{
foreach (var p in GetPathVariations(path))
Assert.That(VirtualPath.Normalize(p),Is.EqualTo(expected));
}

[TestCase("..")]
[TestCase("/home/../..")]
public void Normalize_Error(string path) =>
Assert.Throws<ArgumentException>(() => VirtualPath.Normalize(path));

private static string[] GetPathVariations(string path) =>
[path, path.Replace('/', '\\')];
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,39 +79,6 @@ await fs.GetFilesAsync("/", "**").CountAsync(),
}
}

[Test]
[Order(-1001)]
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
public async Task FileTree_NavigateAboveRoot_ThrowsException()
{
using var fs = GetFileSystem();

Assert.Throws<ArgumentException>(() => fs.GetDirectory(".."));
Assert.Throws<ArgumentException>(() => fs.GetDirectory("/.."));
Assert.Throws<ArgumentException>(() => fs.GetDirectory("/././.."));
Assert.Throws<ArgumentException>(() => fs.GetFile(".."));
Assert.Throws<ArgumentException>(() => fs.GetFile("/.."));
Assert.Throws<ArgumentException>(() => fs.GetFile("/././.."));

Assert.That(
await fs.GetFilesAsync("/", "**").CountAsync(),
Is.Not.Zero);

await foreach (var node in fs.GetFileNodesAsync("/", "**"))
{
foreach (var path in GetPathsAboveRoot(node.FullName))
{
Assert.Throws<ArgumentException>(() =>
{
if (node is VirtualDirectory)
fs.GetDirectory(path);
else
fs.GetFile(path);
});
}
}
}

[Test]
public async Task Exists_ReturnsTrue_For_ExistingFile()
{
Expand Down Expand Up @@ -843,21 +810,4 @@ public void Directory_Readonly_Delete_ThrowsException_For_NonExistingDirectory()
/// A <see cref="DirectoryInfo"/> object that points to the root of the test directory.
/// </returns>
protected abstract DirectoryInfo GetDirectoryInfo();

private static IEnumerable<string> GetPathsAboveRoot(string path)
{
var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);

for (var n = 0; n <= segments.Length; n++)
{
var prefix = segments.Take(n).ToArray();
var suffix = segments.Skip(n).ToArray();
var parent = Enumerable.Repeat("..", n + 1).ToArray();
var curdir = Enumerable.Repeat(".", n + 1).ToArray();

yield return "/" + string.Join("/", prefix.Concat(parent).Concat(suffix));
yield return "/" + string.Join("/", prefix.Concat(curdir).Concat(parent).Concat(suffix));
yield return "/" + string.Join("/", curdir.Concat(prefix).Concat(parent).Concat(suffix));
}
}
}