From 165b857709e5c5c304b157ea6184217425f90ccc Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Fri, 10 Apr 2020 14:33:09 -0500 Subject: [PATCH 1/5] WabbajackTest.TestUtils calls await on directory deletion --- Wabbajack.Common/Json.cs | 4 +--- Wabbajack.Test/ACompilerTest.cs | 2 +- Wabbajack.Test/EndToEndTests.cs | 1 + Wabbajack.Test/TestUtils.cs | 13 ++++++------- .../VirtualFileSystemTests.cs | 14 +++++++------- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Wabbajack.Common/Json.cs b/Wabbajack.Common/Json.cs index 4d2584c7..01a07fc3 100644 --- a/Wabbajack.Common/Json.cs +++ b/Wabbajack.Common/Json.cs @@ -78,10 +78,8 @@ namespace Wabbajack.Common using var tr = new StreamReader(stream, Encoding.UTF8, leaveOpen: true); using var reader = new JsonTextReader(tr); var ser = JsonSerializer.Create(JsonSettings); - return ser.Deserialize(reader); + return ser.Deserialize(reader)!; } - - private class HashJsonConverter : JsonConverter { diff --git a/Wabbajack.Test/ACompilerTest.cs b/Wabbajack.Test/ACompilerTest.cs index c980b234..a48d50a4 100644 --- a/Wabbajack.Test/ACompilerTest.cs +++ b/Wabbajack.Test/ACompilerTest.cs @@ -28,7 +28,7 @@ namespace Wabbajack.Test public override void Dispose() { - utils.Dispose(); + utils.DisposeAsync().AsTask().Wait(); _unsub.Dispose(); base.Dispose(); } diff --git a/Wabbajack.Test/EndToEndTests.cs b/Wabbajack.Test/EndToEndTests.cs index 70caa696..9ad82fb8 100644 --- a/Wabbajack.Test/EndToEndTests.cs +++ b/Wabbajack.Test/EndToEndTests.cs @@ -39,6 +39,7 @@ namespace Wabbajack.Test { Queue.Dispose(); _unsub.Dispose(); + utils.DisposeAsync().AsTask().Wait(); base.Dispose(); } diff --git a/Wabbajack.Test/TestUtils.cs b/Wabbajack.Test/TestUtils.cs index 5efdb7a1..0081c560 100644 --- a/Wabbajack.Test/TestUtils.cs +++ b/Wabbajack.Test/TestUtils.cs @@ -14,7 +14,7 @@ using Path = Alphaleonis.Win32.Filesystem.Path; namespace Wabbajack.Test { - public class TestUtils : IDisposable + public class TestUtils : IAsyncDisposable { private static Random _rng = new Random(); public TestUtils() @@ -119,13 +119,14 @@ namespace Wabbajack.Test return arr; } - public void Dispose() + public async ValueTask DisposeAsync() { - var exts = new [] {".md", ".exe"}; - WorkingDirectory.Combine(ID).DeleteDirectory(); + var exts = new[] { ".md", ".exe" }; + await WorkingDirectory.Combine(ID).DeleteDirectory(); Profiles.Do(p => { - foreach (var ext in exts) { + foreach (var ext in exts) + { var path = Path.Combine(Directory.GetCurrentDirectory(), p + ext); if (File.Exists(path)) File.Delete(path); @@ -246,7 +247,5 @@ namespace Wabbajack.Test GenerateRandomFileData(fullPath, i); return fullPath; } - - } } diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index eca1b235..b5ae80f4 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -56,7 +56,7 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task ArchiveContentsAreIndexed() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); var absPath = "test.zip".RelativeTo(VFS_TEST_DIR); @@ -78,7 +78,7 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task DuplicateFileHashes() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddFile(TEST_TXT, "This is a test"); await AddTestRoot(); @@ -127,7 +127,7 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task CanStageSimpleArchives() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); var res = new FullPath(TEST_ZIP, new[] {(RelativePath)"test.txt"}); @@ -143,12 +143,12 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task CanStageNestedArchives() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); var inner_dir = @"archive\other\dir".RelativeTo(VFS_TEST_DIR); inner_dir.CreateDirectory(); TEST_ZIP.MoveTo( @"archive\other\dir\nested.zip".RelativeTo(VFS_TEST_DIR)); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); @@ -168,10 +168,10 @@ namespace Wabbajack.VirtualFileSystem.Test await filename.WriteAllTextAsync(text); } - private static void ZipUpFolder(AbsolutePath folder, AbsolutePath output) + private static async Task ZipUpFolder(AbsolutePath folder, AbsolutePath output) { ZipFile.CreateFromDirectory((string)folder, (string)output); - folder.DeleteDirectory(); + await folder.DeleteDirectory(); } } } From f8d692afd09ec724275c4a18728be7314b4d669b Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Fri, 10 Apr 2020 14:35:47 -0500 Subject: [PATCH 2/5] Added async factory to VFSTests, so deletion can be awaited --- .../VirtualFileSystemTests.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index b5ae80f4..001dba00 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -20,16 +20,21 @@ namespace Wabbajack.VirtualFileSystem.Test private readonly ITestOutputHelper _helper; private WorkQueue Queue { get; } - public VFSTests(ITestOutputHelper helper) + private VFSTests(ITestOutputHelper helper) { _helper = helper; Utils.LogMessages.Subscribe(f => _helper.WriteLine(f.ShortDescription)); - VFS_TEST_DIR.DeleteDirectory(); - VFS_TEST_DIR.CreateDirectory(); Queue = new WorkQueue(); context = new Context(Queue); } + public static async Task Factory(ITestOutputHelper helper) + { + await VFS_TEST_DIR.DeleteDirectory(); + VFS_TEST_DIR.CreateDirectory(); + return new VFSTests(helper); + } + [Fact] public async Task FilesAreIndexed() { From 69f18f2e222d585e822d9e544f0d5b150fc9da8f Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Fri, 10 Apr 2020 14:44:36 -0500 Subject: [PATCH 3/5] Context.Stage's return action swapped to Func So that its internal delete directory call can be awaited --- Wabbajack.Lib/AInstaller.cs | 2 +- Wabbajack.Lib/MO2Compiler.cs | 2 +- .../VirtualFileSystemTests.cs | 4 +-- Wabbajack.VirtualFileSystem/Context.cs | 31 ++++++++++++++----- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Wabbajack.Lib/AInstaller.cs b/Wabbajack.Lib/AInstaller.cs index 497ffd33..fd3b6292 100644 --- a/Wabbajack.Lib/AInstaller.cs +++ b/Wabbajack.Lib/AInstaller.cs @@ -177,7 +177,7 @@ namespace Wabbajack.Lib }); Status("Unstaging files"); - onFinish(); + await onFinish(); // Now patch all the files from this archive await grouping.OfType() diff --git a/Wabbajack.Lib/MO2Compiler.cs b/Wabbajack.Lib/MO2Compiler.cs index 4ddc87ca..af0145eb 100644 --- a/Wabbajack.Lib/MO2Compiler.cs +++ b/Wabbajack.Lib/MO2Compiler.cs @@ -450,7 +450,7 @@ namespace Wabbajack.Lib private async Task BuildArchivePatches(Hash archiveSha, IEnumerable group, Dictionary absolutePaths) { - using var files = await VFS.StageWith(@group.Select(g => VFS.Index.FileForArchiveHashPath(g.ArchiveHashPath))); + await using var files = await VFS.StageWith(@group.Select(g => VFS.Index.FileForArchiveHashPath(g.ArchiveHashPath))); var byPath = files.GroupBy(f => string.Join("|", f.FilesInFullPath.Skip(1).Select(i => i.Name))) .ToDictionary(f => f.Key, f => f.First()); // Now Create the patches diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index 001dba00..b54b812a 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -141,7 +141,7 @@ namespace Wabbajack.VirtualFileSystem.Test var cleanup = await context.Stage(new List {file}); Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync()); - cleanup(); + await cleanup(); } [Fact] @@ -164,7 +164,7 @@ namespace Wabbajack.VirtualFileSystem.Test foreach (var file in files) Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync()); - cleanup(); + await cleanup(); } private static async Task AddFile(AbsolutePath filename, string text) diff --git a/Wabbajack.VirtualFileSystem/Context.cs b/Wabbajack.VirtualFileSystem/Context.cs index 2d42769b..5105d83d 100644 --- a/Wabbajack.VirtualFileSystem/Context.cs +++ b/Wabbajack.VirtualFileSystem/Context.cs @@ -195,7 +195,7 @@ namespace Wabbajack.VirtualFileSystem } } - public async Task Stage(IEnumerable files) + public async Task> Stage(IEnumerable files) { var grouped = files.SelectMany(f => f.FilesInFullPath) .Distinct() @@ -215,18 +215,18 @@ namespace Wabbajack.VirtualFileSystem file.StagedPath = file.RelativeName.RelativeTo(tmpPath); } - return () => + return async () => { - paths.Do(p => + foreach (var p in paths) { - p.DeleteDirectory(); - }); + await p.DeleteDirectory(); + } }; } - public async Task> StageWith(IEnumerable files) + public async Task> StageWith(IEnumerable files) { - return new DisposableList(await Stage(files), files); + return new AsyncDisposableList(await Stage(files), files); } @@ -275,7 +275,7 @@ namespace Wabbajack.VirtualFileSystem _knownFiles = new List(); } - + #endregion } @@ -294,6 +294,21 @@ namespace Wabbajack.VirtualFileSystem } } + public class AsyncDisposableList : List, IAsyncDisposable + { + private Func _unstage; + + public AsyncDisposableList(Func unstage, IEnumerable files) : base(files) + { + _unstage = unstage; + } + + public async ValueTask DisposeAsync() + { + await _unstage(); + } + } + public class IndexRoot { public static IndexRoot Empty = new IndexRoot(); From 3f8f83e027f790d5f759d79eb59bcc22d1cfbf8d Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Fri, 10 Apr 2020 22:22:10 -0600 Subject: [PATCH 4/5] Update VirtualFileSystemTests.cs --- Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index b54b812a..12e27073 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -20,7 +20,7 @@ namespace Wabbajack.VirtualFileSystem.Test private readonly ITestOutputHelper _helper; private WorkQueue Queue { get; } - private VFSTests(ITestOutputHelper helper) + public VFSTests(ITestOutputHelper helper) { _helper = helper; Utils.LogMessages.Subscribe(f => _helper.WriteLine(f.ShortDescription)); From 24ef0a74a526033406a27f8e0446eef6cca9453b Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Sat, 11 Apr 2020 14:00:52 -0500 Subject: [PATCH 5/5] VirtualFileSystemTests utilize xUnit IAsyncLifetime --- .../VirtualFileSystemTests.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index 12e27073..5059fbf8 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -9,7 +9,7 @@ using Xunit.Abstractions; namespace Wabbajack.VirtualFileSystem.Test { - public class VFSTests + public class VFSTests : IAsyncLifetime { private static readonly AbsolutePath VFS_TEST_DIR = "vfs_test_dir".ToPath().RelativeToEntryPoint(); private static readonly AbsolutePath TEST_ZIP = "test.zip".RelativeTo(VFS_TEST_DIR); @@ -18,21 +18,24 @@ namespace Wabbajack.VirtualFileSystem.Test private Context context; private readonly ITestOutputHelper _helper; - private WorkQueue Queue { get; } + private WorkQueue Queue { get; } = new WorkQueue(); public VFSTests(ITestOutputHelper helper) { _helper = helper; Utils.LogMessages.Subscribe(f => _helper.WriteLine(f.ShortDescription)); - Queue = new WorkQueue(); context = new Context(Queue); } - public static async Task Factory(ITestOutputHelper helper) + public async Task InitializeAsync() { await VFS_TEST_DIR.DeleteDirectory(); VFS_TEST_DIR.CreateDirectory(); - return new VFSTests(helper); + } + + public async Task DisposeAsync() + { + await VFS_TEST_DIR.DeleteDirectory(); } [Fact] @@ -56,7 +59,6 @@ namespace Wabbajack.VirtualFileSystem.Test await context.IntegrateFromFile( "vfs_cache.bin".RelativeTo(VFS_TEST_DIR)); } - [Fact] public async Task ArchiveContentsAreIndexed() {