Skip to content

Commit

Permalink
Merge pull request #94 from agocke/optimize-fscross
Browse files Browse the repository at this point in the history
Optimize cross-filesystem operations
  • Loading branch information
xoofx authored Jun 23, 2024
2 parents 5dca9d6 + 7a83a45 commit ec3a3e5
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 45 deletions.
59 changes: 56 additions & 3 deletions src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Zio.FileSystems;
Expand Down Expand Up @@ -36,10 +36,10 @@ public void TestCopyFileSystemSubFolder()
fs.CopyTo(dest, subFolder, true);

var destSubFileSystem = dest.GetOrCreateSubFileSystem(subFolder);

AssertFileSystemEqual(fs, destSubFileSystem);
}


[Fact]
public void TestWatcher()
Expand All @@ -63,4 +63,57 @@ public void TestDispose()
memfs.Dispose();
Assert.Throws<ObjectDisposedException>(() => memfs.DirectoryExists("/"));
}

[Fact]
public void TestCopyFileCross()
{
var fs = new TriggerMemoryFileSystem();
fs.CreateDirectory("/sub1");
fs.CreateDirectory("/sub2");
var sub1 = new SubFileSystem(fs, "/sub1");
var sub2 = new SubFileSystem(fs, "/sub2");
sub1.WriteAllText("/file.txt", "test");
sub1.CopyFileCross("/file.txt", sub2, "/file.txt", overwrite: false);
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Copy, fs.Triggered);
}

[Fact]
public void TestMoveFileCross()
{
var fs = new TriggerMemoryFileSystem();
fs.CreateDirectory("/sub1");
fs.CreateDirectory("/sub2");
var sub1 = new SubFileSystem(fs, "/sub1");
var sub2 = new SubFileSystem(fs, "/sub2");
sub1.WriteAllText("/file.txt", "test");
sub1.MoveFileCross("/file.txt", sub2, "/file.txt");
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
Assert.False(sub1.FileExists("/file.txt"));
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Move, fs.Triggered);
}

private sealed class TriggerMemoryFileSystem : MemoryFileSystem
{
public enum TriggerType
{
None,
Copy,
Move
}

public TriggerType Triggered { get; private set; } = TriggerType.None;

protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
{
Triggered = TriggerType.Copy;
base.CopyFileImpl(srcPath, destPath, overwrite);
}

protected override void MoveFileImpl(UPath srcPath, UPath destPath)
{
Triggered = TriggerType.Move;
base.MoveFileImpl(srcPath, destPath);
}
}
}
14 changes: 12 additions & 2 deletions src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;
Expand Down Expand Up @@ -336,7 +336,7 @@ public void TestEnumerate()
var expectedPaths = Directory.EnumerateFileSystemEntries(Path.GetFullPath(Path.Combine(SystemPath, "../.."))).ToList();
Assert.Equal(expectedPaths, paths);
}

[SkippableFact]
public void TestFileWindowsExceptions()
{
Expand Down Expand Up @@ -536,4 +536,14 @@ public void TestFileSymlink()
SafeDeleteFile(systemPathDest);
}
}

[Fact]
public void TestResolvePath()
{
var fs = new PhysicalFileSystem();
var testPath = fs.ConvertPathFromInternal(SystemPath);
var (resFs, resPath) = fs.ResolvePath(testPath);
Assert.Equal(testPath, resPath);
Assert.Equal(fs, resFs);
}
}
25 changes: 23 additions & 2 deletions src/Zio.Tests/FileSystems/TestSubFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;
Expand Down Expand Up @@ -36,7 +36,7 @@ public void TestBasic()
}

// TODO: We could add another test just to make sure that files can be created...etc. But the test above should already cover the code provided in SubFileSystem
}
}

[Fact]
public void TestGetOrCreateFileSystem()
Expand Down Expand Up @@ -110,6 +110,27 @@ public void TestWatcherCaseSensitive(string physicalDir, string subDir, string f
Assert.True(waitHandle.WaitOne(100));
}

[Fact]
public void TestResolvePath()
{
var fs = GetCommonMemoryFileSystem();
var subFs = fs.GetOrCreateSubFileSystem("/a/b");
var (resFs, resPath) = subFs.ResolvePath("/c");
Assert.Equal("/a/b/c", resPath);
Assert.NotEqual(subFs, resFs);
Assert.Equal(fs, resFs);
(resFs, resPath) = subFs.ResolvePath("/c/d");
Assert.Equal("/a/b/c/d", resPath);
Assert.NotEqual(subFs, resFs);
Assert.Equal(fs, resFs);

var subFs2 = subFs.GetOrCreateSubFileSystem("/q");
(resFs, resPath) = subFs2.ResolvePath("/c");
Assert.Equal("/a/b/q/c", resPath);
Assert.NotEqual(subFs2, resFs);
Assert.Equal(fs, resFs);
}

[SkippableFact]
public void TestDirectorySymlink()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Zio.Tests/Zio.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>10</LangVersion>
</PropertyGroup>
Expand Down
Loading

0 comments on commit ec3a3e5

Please sign in to comment.