wabbajack/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs

178 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Wabbajack.Common;
2020-03-24 12:21:19 +00:00
using Xunit;
using Xunit.Abstractions;
namespace Wabbajack.VirtualFileSystem.Test
{
public class VFSTests
{
2020-03-24 02:46:30 +00:00
private static readonly AbsolutePath VFS_TEST_DIR = "vfs_test_dir".ToPath().RelativeToEntryPoint();
private static readonly AbsolutePath TEST_ZIP = "test.zip".RelativeTo(VFS_TEST_DIR);
private static readonly AbsolutePath TEST_TXT = "test.txt".RelativeTo(VFS_TEST_DIR);
2020-03-24 21:42:28 +00:00
private static readonly AbsolutePath ARCHIVE_TEST_TXT = "archive/test.txt".RelativeTo(VFS_TEST_DIR);
private Context context;
2020-03-24 12:21:19 +00:00
private readonly ITestOutputHelper _helper;
private WorkQueue Queue { get; }
2020-03-24 12:21:19 +00:00
public VFSTests(ITestOutputHelper helper)
{
2020-03-24 12:21:19 +00:00
_helper = helper;
Utils.LogMessages.Subscribe(f => _helper.WriteLine(f.ShortDescription));
VFS_TEST_DIR.DeleteDirectory();
VFS_TEST_DIR.CreateDirectory();
2019-11-17 04:16:42 +00:00
Queue = new WorkQueue();
context = new Context(Queue);
}
2020-03-24 12:21:19 +00:00
[Fact]
public async Task FilesAreIndexed()
{
2020-03-24 02:46:30 +00:00
await AddFile(TEST_TXT, "This is a test");
await AddTestRoot();
var file = context.Index.ByRootPath["test.txt".ToPath().RelativeTo(VFS_TEST_DIR)];
2020-03-24 12:21:19 +00:00
Assert.NotNull(file);
2020-03-24 12:21:19 +00:00
Assert.Equal(14, file.Size);
Assert.Equal(file.Hash, Hash.FromBase64("qX0GZvIaTKM="));
}
2020-03-24 12:21:19 +00:00
private async Task AddTestRoot()
{
await context.AddRoot(VFS_TEST_DIR);
await context.WriteToFile("vfs_cache.bin".RelativeTo(VFS_TEST_DIR));
await context.IntegrateFromFile( "vfs_cache.bin".RelativeTo(VFS_TEST_DIR));
}
2020-03-24 21:42:28 +00:00
[Fact]
public async Task ArchiveContentsAreIndexed()
{
2020-03-24 02:46:30 +00:00
await AddFile(ARCHIVE_TEST_TXT, "This is a test");
ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP);
await AddTestRoot();
2020-03-24 02:46:30 +00:00
var absPath = "test.zip".RelativeTo(VFS_TEST_DIR);
var file = context.Index.ByRootPath[absPath];
2020-03-24 21:42:28 +00:00
Assert.NotNull(file);
2020-03-24 21:42:28 +00:00
Assert.Equal(128, file.Size);
Assert.Equal(absPath.FileHash(), file.Hash);
2020-03-24 21:42:28 +00:00
Assert.True(file.IsArchive);
var innerFile = file.Children.First();
2020-03-24 21:42:28 +00:00
Assert.Equal(14, innerFile.Size);
Assert.Equal(Hash.FromBase64("qX0GZvIaTKM="), innerFile.Hash);
Assert.Same(file, file.Children.First().Parent);
}
2020-03-24 21:42:28 +00:00
2020-03-24 21:42:28 +00:00
[Fact]
public async Task DuplicateFileHashes()
{
2020-03-24 02:46:30 +00:00
await AddFile(ARCHIVE_TEST_TXT, "This is a test");
ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP);
2020-03-24 02:46:30 +00:00
await AddFile(TEST_TXT, "This is a test");
await AddTestRoot();
2020-03-22 15:50:53 +00:00
var files = context.Index.ByHash[Hash.FromBase64("qX0GZvIaTKM=")];
2020-03-24 21:42:28 +00:00
Assert.Equal(2, files.Count());
}
2020-03-24 21:42:28 +00:00
[Fact]
public async Task DeletedFilesAreRemoved()
{
2020-03-24 02:46:30 +00:00
await AddFile(TEST_TXT, "This is a test");
await AddTestRoot();
2020-03-24 02:46:30 +00:00
var file = context.Index.ByRootPath[TEST_TXT];
2020-03-24 21:42:28 +00:00
Assert.NotNull(file);
2020-03-24 21:42:28 +00:00
Assert.Equal(14, file.Size);
Assert.Equal(Hash.FromBase64("qX0GZvIaTKM="), file.Hash);
2020-03-24 02:46:30 +00:00
TEST_TXT.Delete();
await AddTestRoot();
2020-03-24 21:42:28 +00:00
Assert.DoesNotContain(TEST_TXT, context.Index.AllFiles.Select(f => f.AbsoluteName));
}
2020-03-24 21:42:28 +00:00
[Fact]
public async Task UnmodifiedFilesAreNotReIndexed()
{
2020-03-24 02:46:30 +00:00
await AddFile(TEST_TXT, "This is a test");
await AddTestRoot();
2020-03-24 02:46:30 +00:00
var old_file = context.Index.ByRootPath[TEST_TXT];
var old_time = old_file.LastAnalyzed;
await AddTestRoot();
2020-03-24 02:46:30 +00:00
var new_file = context.Index.ByRootPath[TEST_TXT];
2020-03-24 21:42:28 +00:00
Assert.Equal(old_time, new_file.LastAnalyzed);
}
2020-03-24 21:42:28 +00:00
[Fact]
public async Task CanStageSimpleArchives()
{
2020-03-24 02:46:30 +00:00
await AddFile(ARCHIVE_TEST_TXT, "This is a test");
ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP);
await AddTestRoot();
2020-03-24 21:42:28 +00:00
var res = new FullPath(TEST_ZIP, new[] {(RelativePath)"test.txt"});
var file = context.Index.ByFullPath[res];
var cleanup = await context.Stage(new List<VirtualFile> {file});
2020-03-24 21:42:28 +00:00
Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync());
cleanup();
}
2020-03-24 21:42:28 +00:00
[Fact]
public async Task CanStageNestedArchives()
{
2020-03-24 02:46:30 +00:00
await AddFile(ARCHIVE_TEST_TXT, "This is a test");
ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP);
2020-03-24 02:46:30 +00:00
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 AddTestRoot();
2020-03-22 15:50:53 +00:00
var files = context.Index.ByHash[Hash.FromBase64("qX0GZvIaTKM=")];
var cleanup = await context.Stage(files);
foreach (var file in files)
2020-03-24 21:42:28 +00:00
Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync());
cleanup();
}
2020-03-24 02:46:30 +00:00
private static async Task AddFile(AbsolutePath filename, string text)
{
2020-03-24 02:46:30 +00:00
filename.Parent.CreateDirectory();
await filename.WriteAllTextAsync(text);
}
2020-03-24 02:46:30 +00:00
private static void ZipUpFolder(AbsolutePath folder, AbsolutePath output)
{
2020-03-24 02:46:30 +00:00
ZipFile.CreateFromDirectory((string)folder, (string)output);
folder.DeleteDirectory();
}
}
}