Skip to content

Commit ec3a3e5

Browse files
authored
Merge pull request #94 from agocke/optimize-fscross
Optimize cross-filesystem operations
2 parents 5dca9d6 + 7a83a45 commit ec3a3e5

9 files changed

+171
-45
lines changed

src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Alexandre Mutel. All rights reserved.
2-
// This file is licensed under the BSD-Clause 2 license.
2+
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

55
using Zio.FileSystems;
@@ -36,10 +36,10 @@ public void TestCopyFileSystemSubFolder()
3636
fs.CopyTo(dest, subFolder, true);
3737

3838
var destSubFileSystem = dest.GetOrCreateSubFileSystem(subFolder);
39-
39+
4040
AssertFileSystemEqual(fs, destSubFileSystem);
4141
}
42-
42+
4343

4444
[Fact]
4545
public void TestWatcher()
@@ -63,4 +63,57 @@ public void TestDispose()
6363
memfs.Dispose();
6464
Assert.Throws<ObjectDisposedException>(() => memfs.DirectoryExists("/"));
6565
}
66+
67+
[Fact]
68+
public void TestCopyFileCross()
69+
{
70+
var fs = new TriggerMemoryFileSystem();
71+
fs.CreateDirectory("/sub1");
72+
fs.CreateDirectory("/sub2");
73+
var sub1 = new SubFileSystem(fs, "/sub1");
74+
var sub2 = new SubFileSystem(fs, "/sub2");
75+
sub1.WriteAllText("/file.txt", "test");
76+
sub1.CopyFileCross("/file.txt", sub2, "/file.txt", overwrite: false);
77+
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
78+
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Copy, fs.Triggered);
79+
}
80+
81+
[Fact]
82+
public void TestMoveFileCross()
83+
{
84+
var fs = new TriggerMemoryFileSystem();
85+
fs.CreateDirectory("/sub1");
86+
fs.CreateDirectory("/sub2");
87+
var sub1 = new SubFileSystem(fs, "/sub1");
88+
var sub2 = new SubFileSystem(fs, "/sub2");
89+
sub1.WriteAllText("/file.txt", "test");
90+
sub1.MoveFileCross("/file.txt", sub2, "/file.txt");
91+
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
92+
Assert.False(sub1.FileExists("/file.txt"));
93+
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Move, fs.Triggered);
94+
}
95+
96+
private sealed class TriggerMemoryFileSystem : MemoryFileSystem
97+
{
98+
public enum TriggerType
99+
{
100+
None,
101+
Copy,
102+
Move
103+
}
104+
105+
public TriggerType Triggered { get; private set; } = TriggerType.None;
106+
107+
protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
108+
{
109+
Triggered = TriggerType.Copy;
110+
base.CopyFileImpl(srcPath, destPath, overwrite);
111+
}
112+
113+
protected override void MoveFileImpl(UPath srcPath, UPath destPath)
114+
{
115+
Triggered = TriggerType.Move;
116+
base.MoveFileImpl(srcPath, destPath);
117+
}
118+
}
66119
}

src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Alexandre Mutel. All rights reserved.
2-
// This file is licensed under the BSD-Clause 2 license.
2+
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

55
using System.IO;
@@ -336,7 +336,7 @@ public void TestEnumerate()
336336
var expectedPaths = Directory.EnumerateFileSystemEntries(Path.GetFullPath(Path.Combine(SystemPath, "../.."))).ToList();
337337
Assert.Equal(expectedPaths, paths);
338338
}
339-
339+
340340
[SkippableFact]
341341
public void TestFileWindowsExceptions()
342342
{
@@ -536,4 +536,14 @@ public void TestFileSymlink()
536536
SafeDeleteFile(systemPathDest);
537537
}
538538
}
539+
540+
[Fact]
541+
public void TestResolvePath()
542+
{
543+
var fs = new PhysicalFileSystem();
544+
var testPath = fs.ConvertPathFromInternal(SystemPath);
545+
var (resFs, resPath) = fs.ResolvePath(testPath);
546+
Assert.Equal(testPath, resPath);
547+
Assert.Equal(fs, resFs);
548+
}
539549
}

src/Zio.Tests/FileSystems/TestSubFileSystem.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Alexandre Mutel. All rights reserved.
2-
// This file is licensed under the BSD-Clause 2 license.
2+
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

55
using System.IO;
@@ -36,7 +36,7 @@ public void TestBasic()
3636
}
3737

3838
// 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
39-
}
39+
}
4040

4141
[Fact]
4242
public void TestGetOrCreateFileSystem()
@@ -110,6 +110,27 @@ public void TestWatcherCaseSensitive(string physicalDir, string subDir, string f
110110
Assert.True(waitHandle.WaitOne(100));
111111
}
112112

113+
[Fact]
114+
public void TestResolvePath()
115+
{
116+
var fs = GetCommonMemoryFileSystem();
117+
var subFs = fs.GetOrCreateSubFileSystem("/a/b");
118+
var (resFs, resPath) = subFs.ResolvePath("/c");
119+
Assert.Equal("/a/b/c", resPath);
120+
Assert.NotEqual(subFs, resFs);
121+
Assert.Equal(fs, resFs);
122+
(resFs, resPath) = subFs.ResolvePath("/c/d");
123+
Assert.Equal("/a/b/c/d", resPath);
124+
Assert.NotEqual(subFs, resFs);
125+
Assert.Equal(fs, resFs);
126+
127+
var subFs2 = subFs.GetOrCreateSubFileSystem("/q");
128+
(resFs, resPath) = subFs2.ResolvePath("/c");
129+
Assert.Equal("/a/b/q/c", resPath);
130+
Assert.NotEqual(subFs2, resFs);
131+
Assert.Equal(fs, resFs);
132+
}
133+
113134
[SkippableFact]
114135
public void TestDirectorySymlink()
115136
{

src/Zio.Tests/Zio.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net472</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<LangVersion>10</LangVersion>
77
</PropertyGroup>

0 commit comments

Comments
 (0)