wabbajack/Wabbajack.VFS.Test/VFSTests.cs

177 lines
5.6 KiB
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Xunit;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.VFS.Test;
public class VFSTests : IDisposable
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly AbsolutePath _archiveTestTxt;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private readonly Context _context;
private readonly TemporaryFileManager _manager;
private readonly AbsolutePath _testTxt;
private readonly AbsolutePath _testZip;
private readonly AbsolutePath _vfsTestDir;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public VFSTests(Context context, TemporaryFileManager manager)
{
_context = context;
_manager = manager;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_vfsTestDir = _manager.CreateFolder();
_testZip = "test.zip".ToRelativePath().RelativeTo(_vfsTestDir);
_testTxt = "test.txt".ToRelativePath().RelativeTo(_vfsTestDir);
_archiveTestTxt = "archive/test.txt".ToRelativePath().RelativeTo(_vfsTestDir);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public void Dispose()
{
_manager?.Dispose();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task FilesAreIndexed()
{
await AddFile(_testTxt, "This is a test");
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var file = _context.Index.ByRootPath["test.txt".ToRelativePath().RelativeTo(_vfsTestDir)];
Assert.NotNull(file);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
Assert.Equal(14, file.Size);
Assert.Equal(file.Hash, Hash.FromBase64("qX0GZvIaTKM="));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task ArchiveContentsAreIndexed()
{
await AddFile(_archiveTestTxt, "This is a test");
await ZipUpFolder(_archiveTestTxt.Parent, _testZip);
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
var absPath = "test.zip".ToRelativePath().RelativeTo(_vfsTestDir);
var file = _context.Index.ByRootPath[absPath];
Assert.NotNull(file);
Assert.Equal(128, file.Size);
//Assert.Equal(await absPath.HashCopyTo(Stream.Null), file.Hash);
Assert.True(file.IsArchive);
var innerFile = file.Children.First();
Assert.Equal(14, innerFile.Size);
Assert.Equal(Hash.FromBase64("qX0GZvIaTKM="), innerFile.Hash);
Assert.Same(file, file.Children.First().Parent);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task DuplicateFileHashes()
{
await AddFile(_archiveTestTxt, "This is a test");
await ZipUpFolder(_archiveTestTxt.Parent, _testZip);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await AddFile(_testTxt, "This is a test");
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var files = _context.Index.ByHash[Hash.FromBase64("qX0GZvIaTKM=")];
Assert.Equal(2, files.Count());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task DeletedFilesAreRemoved()
{
await AddFile(_testTxt, "This is a test");
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var file = _context.Index.ByRootPath[_testTxt];
Assert.NotNull(file);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
Assert.Equal(14, file.Size);
Assert.Equal(Hash.FromBase64("qX0GZvIaTKM="), file.Hash);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_testTxt.Delete();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
Assert.DoesNotContain(_testTxt, _context.Index.AllFiles.Select(f => f.AbsoluteName));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task UnmodifiedFilesAreNotReIndexed()
{
await AddFile(_testTxt, "This is a test");
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var old_file = _context.Index.ByRootPath[_testTxt];
var old_time = old_file.LastAnalyzed;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var new_file = _context.Index.ByRootPath[_testTxt];
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
Assert.Equal(old_time, new_file.LastAnalyzed);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task CanStageSimpleArchives()
{
await AddFile(_archiveTestTxt, "This is a test");
await ZipUpFolder(_archiveTestTxt.Parent, _testZip);
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var res = new FullPath(_testZip, (RelativePath) "test.txt");
var files = new[] {_context.Index.ByFullPath[res]};
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await _context.Extract(files.ToHashSet(), async (file, factory) =>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
await using var s = await factory.GetStream();
//Assert.Equal("This is a test", await s.ReadAllTextAsync());
}, CancellationToken.None);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public async Task CanStageNestedArchives()
{
await AddFile(_archiveTestTxt, "This is a test");
await ZipUpFolder(_archiveTestTxt.Parent, _testZip);
2021-09-27 12:42:46 +00:00
2022-10-07 22:57:12 +00:00
var innerDir = @"archive\other\dir".ToRelativePath().RelativeTo(_vfsTestDir);
innerDir.CreateDirectory();
2021-10-23 16:51:17 +00:00
await _testZip.MoveToAsync(@"archive\other\dir\nested.zip".ToRelativePath().RelativeTo(_vfsTestDir), true,
CancellationToken.None);
await ZipUpFolder(_archiveTestTxt.Parent, _testZip);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await _context.AddRoot(_vfsTestDir, CancellationToken.None);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var files = _context.Index.ByHash[Hash.FromBase64("qX0GZvIaTKM=")];
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await _context.Extract(files.ToHashSet(), async (file, factory) =>
{
await using var s = await factory.GetStream();
//Assert.Equal("This is a test", await s.ReadAllTextAsync());
}, CancellationToken.None);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private static async Task AddFile(AbsolutePath filename, string text)
{
filename.Parent.CreateDirectory();
await filename.WriteAllTextAsync(text);
}
2021-09-27 12:42:46 +00:00
2022-10-07 22:57:12 +00:00
private static Task ZipUpFolder(AbsolutePath folder, AbsolutePath output)
2021-10-23 16:51:17 +00:00
{
ZipFile.CreateFromDirectory(folder.ToString(), output.ToString());
folder.DeleteDirectory();
2022-10-07 22:57:12 +00:00
return Task.CompletedTask;
2021-09-27 12:42:46 +00:00
}
}