Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion to async #62

Closed
wants to merge 2 commits into from
Closed
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
98 changes: 61 additions & 37 deletions src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Zio.FileSystems;

namespace Zio.Tests.FileSystems
Expand All @@ -27,109 +28,132 @@ public FileSystemEntryRedirect(MemoryFileSystem fs)
_fs = fs ?? throw new ArgumentNullException(nameof(fs));
}

protected override void CreateDirectoryImpl(UPath path)
protected override ValueTask CreateDirectoryImpl(UPath path)
{
new DirectoryEntry(_fs, path).Create();
return new();
}

protected override bool DirectoryExistsImpl(UPath path)
protected override ValueTask<bool> DirectoryExistsImpl(UPath path)
{
return new DirectoryEntry(_fs, path).Exists;
}

protected override void MoveDirectoryImpl(UPath srcPath, UPath destPath)
protected override ValueTask MoveDirectoryImpl(UPath srcPath, UPath destPath)
{
new DirectoryEntry(_fs, srcPath).MoveTo(destPath);
return new DirectoryEntry(_fs, srcPath).MoveTo(destPath);
}

protected override void DeleteDirectoryImpl(UPath path, bool isRecursive)
protected override ValueTask DeleteDirectoryImpl(UPath path, bool isRecursive)
{
new DirectoryEntry(_fs, path).Delete(isRecursive);
return new DirectoryEntry(_fs, path).Delete(isRecursive);
}

protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
protected override async ValueTask CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
{
new FileEntry(_fs, srcPath).CopyTo(destPath, overwrite);
await new FileEntry(_fs, srcPath).CopyTo(destPath, overwrite);
}

protected override void ReplaceFileImpl(UPath srcPath, UPath destPath, UPath destBackupPath, bool ignoreMetadataErrors)
protected override async ValueTask ReplaceFileImpl(UPath srcPath, UPath destPath, UPath destBackupPath, bool ignoreMetadataErrors)
{
new FileEntry(_fs, srcPath).ReplaceTo(destPath, destBackupPath, ignoreMetadataErrors);
await new FileEntry(_fs, srcPath).ReplaceTo(destPath, destBackupPath, ignoreMetadataErrors);
}

protected override long GetFileLengthImpl(UPath path)
protected override ValueTask<long> GetFileLengthImpl(UPath path)
{
return new FileEntry(_fs, path).Length;
}

protected override bool FileExistsImpl(UPath path)
protected override ValueTask<bool> FileExistsImpl(UPath path)
{
return new FileEntry(_fs, path).Exists;
}

protected override void MoveFileImpl(UPath srcPath, UPath destPath)
protected override ValueTask MoveFileImpl(UPath srcPath, UPath destPath)
{
new FileEntry(_fs, srcPath).MoveTo(destPath);
return new FileEntry(_fs, srcPath).MoveTo(destPath);
}

protected override void DeleteFileImpl(UPath path)
protected override ValueTask DeleteFileImpl(UPath path)
{
new FileEntry(_fs, path).Delete();
return new FileEntry(_fs, path).Delete();
}

protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess access, FileShare share)
protected override ValueTask<Stream> OpenFileImpl(UPath path, FileMode mode, FileAccess access, FileShare share)
{
return new FileEntry(_fs, path).Open(mode, access, share);
}

protected override FileAttributes GetAttributesImpl(UPath path)
protected override async ValueTask<FileAttributes> GetAttributesImpl(UPath path)
{
return _fs.GetFileSystemEntry(path).Attributes;
return await (await _fs.GetFileSystemEntry(path)).GetAttributes();
}

protected override void SetAttributesImpl(UPath path, FileAttributes attributes)
protected override async ValueTask SetAttributesImpl(UPath path, FileAttributes attributes)
{
_fs.GetFileSystemEntry(path).Attributes = attributes;
await (await _fs.GetFileSystemEntry(path)).SetAttributes(attributes);
}

protected override DateTime GetCreationTimeImpl(UPath path)
protected override async ValueTask<DateTime> GetCreationTimeImpl(UPath path)
{
return _fs.TryGetFileSystemEntry(path)?.CreationTime ?? DefaultFileTime;
var entry = await _fs.TryGetFileSystemEntry(path);
if (entry == null)
{
return DefaultFileTime;
}

return await _fs.GetCreationTime(path);
}

protected override void SetCreationTimeImpl(UPath path, DateTime time)
protected override ValueTask SetCreationTimeImpl(UPath path, DateTime time)
{
_fs.GetFileSystemEntry(path).CreationTime = time;
return _fs.SetCreationTime(path, time);
}

protected override DateTime GetLastAccessTimeImpl(UPath path)
protected override async ValueTask<DateTime> GetLastAccessTimeImpl(UPath path)
{
return _fs.TryGetFileSystemEntry(path)?.LastAccessTime ?? DefaultFileTime;
var entry = await _fs.TryGetFileSystemEntry(path);
if (entry == null)
{
return DefaultFileTime;
}


return await _fs.GetLastAccessTime(path);
}

protected override void SetLastAccessTimeImpl(UPath path, DateTime time)
protected override ValueTask SetLastAccessTimeImpl(UPath path, DateTime time)
{
_fs.GetFileSystemEntry(path).LastAccessTime = time;
return _fs.SetLastAccessTime(path, time);
}

protected override DateTime GetLastWriteTimeImpl(UPath path)
protected override async ValueTask<DateTime> GetLastWriteTimeImpl(UPath path)
{
return _fs.TryGetFileSystemEntry(path)?.LastWriteTime ?? DefaultFileTime;
var entry = await _fs.TryGetFileSystemEntry(path);
if (entry == null)
{
return DefaultFileTime;
}


return await _fs.GetLastWriteTime(path);
}

protected override void SetLastWriteTimeImpl(UPath path, DateTime time)
protected override ValueTask SetLastWriteTimeImpl(UPath path, DateTime time)
{
_fs.GetFileSystemEntry(path).LastWriteTime = time;
return _fs.SetLastWriteTime(path, time);
}

protected override IEnumerable<UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget)
protected override async ValueTask<IEnumerable<UPath>> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget)
{
return _fs.GetDirectoryEntry(path).EnumerateEntries(searchPattern, searchOption, searchTarget).Select(e => e.Path);
var directoryEntry = await _fs.GetDirectoryEntry(path);
var entries = await directoryEntry.EnumerateEntries(searchPattern, searchOption, searchTarget);
return entries.Select(e => e.Path).ToArray();
}

protected override IEnumerable<FileSystemItem> EnumerateItemsImpl(UPath path, SearchOption searchOption, SearchPredicate searchPredicate)
protected override async ValueTask<IEnumerable<FileSystemItem>> EnumerateItemsImpl(UPath path, SearchOption searchOption, SearchPredicate searchPredicate)
{
return _fs.GetDirectoryEntry(path).EnumerateItems(searchOption);
return await (await _fs.GetDirectoryEntry(path)).EnumerateItems(searchOption);
}

protected override IFileSystemWatcher WatchImpl(UPath path)
Expand Down
15 changes: 12 additions & 3 deletions src/Zio.Tests/FileSystems/PhysicalDirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.IO;
using System.Threading.Tasks;
using Zio.FileSystems;

namespace Zio.Tests.FileSystems
Expand All @@ -12,16 +13,24 @@ public class PhysicalDirectoryHelper : IDisposable
{
private readonly DirectoryInfo _compatDirectory;

public PhysicalDirectoryHelper(string rootPath)
private PhysicalDirectoryHelper(string rootPath)
{
_compatDirectory = new DirectoryInfo(Path.Combine(rootPath, "Physical-" + Guid.NewGuid()));
_compatDirectory.Create();
}

public static async ValueTask<PhysicalDirectoryHelper> Create(string rootPath)
{
var helper = new PhysicalDirectoryHelper(rootPath);

var pfs = new PhysicalFileSystem();
PhysicalFileSystem = new SubFileSystem(pfs, pfs.ConvertPathFromInternal(_compatDirectory.FullName));

helper.PhysicalFileSystem = await SubFileSystem.Create(pfs, pfs.ConvertPathFromInternal(helper._compatDirectory.FullName));

return helper;
}

public IFileSystem PhysicalFileSystem { get; }
public IFileSystem PhysicalFileSystem { get; private set; }

public void Dispose()
{
Expand Down
36 changes: 19 additions & 17 deletions src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Zio.FileSystems;

Expand All @@ -13,16 +14,17 @@ namespace Zio.Tests.FileSystems
public class TestAggregateFileSystem : TestFileSystemBase
{
[Fact]
public void TestCommonReadOnly()
public async Task TestCommonReadOnly()
{
var fs = GetCommonAggregateFileSystem();
AssertCommonReadOnly(fs);
var (fs, _, _, _) = await GetCommonAggregateFileSystem();
await AssertCommonReadOnly(fs);
}

[Fact]
public void TestWatcher()
public async Task TestWatcher()
{
var fs = GetCommonAggregateFileSystem(out var fs1, out var fs2, out _);
var (fs, fs1, fs2, _) = await GetCommonAggregateFileSystem();

var watcher = fs.Watch("/");

var gotChange1 = false;
Expand All @@ -43,8 +45,8 @@ public void TestWatcher()
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;

fs1.WriteAllText("/b/watched.txt", "test");
fs2.WriteAllText("/C/watched.txt", "test");
await fs1.WriteAllText("/b/watched.txt", "test");
await fs2.WriteAllText("/C/watched.txt", "test");

System.Threading.Thread.Sleep(100);

Expand All @@ -53,9 +55,9 @@ public void TestWatcher()
}

[Fact]
public void TestWatcherRemovedWhenDisposed()
public async Task TestWatcherRemovedWhenDisposed()
{
var fs = GetCommonAggregateFileSystem();
var (fs, _, _, _) = await GetCommonAggregateFileSystem();

var watcher = fs.Watch("/");
watcher.IncludeSubdirectories = true;
Expand Down Expand Up @@ -106,22 +108,22 @@ public void TestAddRemoveFileSystem()
}

[Fact]
public void TestFindFileSystemEntries()
public async Task TestFindFileSystemEntries()
{
var fs = new AggregateFileSystem();

var memfs1 = new MemoryFileSystem();
memfs1.WriteAllText("/a.txt", "content1");
memfs1.WriteAllText("/b", "notused");
await memfs1.WriteAllText("/a.txt", "content1");
await memfs1.WriteAllText("/b", "notused");
fs.AddFileSystem(memfs1);

var memfs2 = new MemoryFileSystem();
memfs2.WriteAllText("/a.txt", "content2");
memfs2.CreateDirectory("/b");
await memfs2.WriteAllText("/a.txt", "content2");
await memfs2.CreateDirectory("/b");
fs.AddFileSystem(memfs2);

{
var entries = fs.FindFileSystemEntries("/a.txt");
var entries = await fs.FindFileSystemEntries("/a.txt");
Assert.Equal(2, entries.Count);

Assert.IsType<FileEntry>(entries[0]);
Expand All @@ -133,7 +135,7 @@ public void TestFindFileSystemEntries()
}

{
var entries = fs.FindFileSystemEntries("/b");
var entries = await fs.FindFileSystemEntries("/b");
Assert.Single(entries);

Assert.IsType<DirectoryEntry>(entries[0]);
Expand All @@ -142,7 +144,7 @@ public void TestFindFileSystemEntries()
}

{
var entry = fs.FindFirstFileSystemEntry("/a.txt");
var entry = await fs.FindFirstFileSystemEntry("/a.txt");
Assert.NotNull(entry);

Assert.IsType<FileEntry>(entry);
Expand Down
Loading