wabbajack/Wabbajack.Test/TestUtils.cs

284 lines
10 KiB
C#
Raw Normal View History

2019-09-24 04:20:24 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
2019-09-24 04:20:24 +00:00
using Wabbajack.Common;
2020-03-26 21:15:44 +00:00
using Xunit;
2019-09-24 04:20:24 +00:00
using Directory = Alphaleonis.Win32.Filesystem.Directory;
using File = Alphaleonis.Win32.Filesystem.File;
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
2019-09-24 04:20:24 +00:00
using Path = Alphaleonis.Win32.Filesystem.Path;
namespace Wabbajack.Test
{
public class TestUtils : IAsyncDisposable
2019-09-24 04:20:24 +00:00
{
private static Random _rng = new Random();
2019-09-24 04:20:24 +00:00
public TestUtils()
{
ID = RandomName();
2020-03-26 21:15:44 +00:00
WorkingDirectory = ((RelativePath)"tmp_data").RelativeToEntryPoint();
2019-09-24 04:20:24 +00:00
}
public AbsolutePath WorkingDirectory { get;}
2019-09-24 04:20:24 +00:00
public string ID { get; }
public Random RNG => _rng;
2019-09-24 04:20:24 +00:00
2019-11-16 22:02:36 +00:00
public Game Game { get; set; }
2019-09-24 04:20:24 +00:00
public AbsolutePath TestFolder => WorkingDirectory.Combine(ID);
public AbsolutePath GameFolder => WorkingDirectory.Combine(ID, "game_folder");
2019-09-24 04:20:24 +00:00
2020-10-18 19:03:50 +00:00
public AbsolutePath SourcePath => WorkingDirectory.Combine(ID, "source_folder");
public AbsolutePath ModsPath => SourcePath.Combine(Consts.MO2ModFolderName);
public AbsolutePath DownloadsPath => SourcePath.Combine("downloads");
2019-09-24 04:20:24 +00:00
2020-10-18 19:03:50 +00:00
public AbsolutePath InstallPath => TestFolder.Combine("installed");
2019-09-24 04:20:24 +00:00
2019-11-16 21:29:05 +00:00
public HashSet<string> Profiles = new HashSet<string>();
public List<string> Mods = new List<string>();
2019-09-24 04:20:24 +00:00
public async Task Configure(IEnumerable<(string ModName, bool IsEnabled)> enabledMods = null)
2019-09-24 04:20:24 +00:00
{
2020-10-18 19:03:50 +00:00
await SourcePath.Combine("ModOrganizer.ini").WriteAllLinesAsync(
2020-03-28 02:54:14 +00:00
"[General]",
$"gameName={Game.MetaData().MO2Name}",
$"gamePath={((string)GameFolder).Replace("\\", "\\\\")}",
2020-10-18 19:03:50 +00:00
$"download_directory={DownloadsPath}");
2019-09-24 04:20:24 +00:00
2020-10-18 19:03:50 +00:00
DownloadsPath.CreateDirectory();
GameFolder.Combine("Data").CreateDirectory();
2019-09-24 04:20:24 +00:00
if (enabledMods == null)
2019-09-24 04:20:24 +00:00
{
Profiles.Do(profile =>
{
2020-10-18 19:03:50 +00:00
SourcePath.Combine("profiles", profile, "modlist.txt").WriteAllLinesAsync(
Mods.Select(s => $"+{s}").ToArray());
});
}
else
{
Profiles.Do(profile =>
{
2020-10-18 19:03:50 +00:00
SourcePath.Combine("profiles", profile, "modlist.txt").WriteAllLinesAsync(
enabledMods.Select(s => $"{(s.IsEnabled ? "+" : "-")}{s.ModName}").ToArray());
});
}
2019-09-24 04:20:24 +00:00
}
2019-10-28 03:15:48 +00:00
public string AddProfile(string name = null)
2019-09-24 04:20:24 +00:00
{
2019-10-28 03:15:48 +00:00
string profile_name = name ?? RandomName();
2020-10-18 19:03:50 +00:00
SourcePath.Combine("profiles", profile_name).CreateDirectory();
2019-09-24 04:20:24 +00:00
Profiles.Add(profile_name);
return profile_name;
}
public async Task<string> AddMod(string name = null)
2019-09-24 04:20:24 +00:00
{
string mod_name = name ?? RandomName();
2020-10-18 19:03:50 +00:00
var mod_folder = SourcePath.Combine(Consts.MO2ModFolderName, (RelativePath)mod_name);
mod_folder.CreateDirectory();
await mod_folder.Combine("meta.ini").WriteAllTextAsync("[General]");
Mods.Add(mod_name);
return mod_name;
2019-09-24 04:20:24 +00:00
}
/// <summary>
/// Adds a file to the given mod with a given path in the mod. Fills it with random data unless
/// random_fill == 0;
/// </summary>
/// <param name="mod_name"></param>
/// <param name="path"></param>
/// <param name="random_fill"></param>
/// <returns></returns>
public async Task<AbsolutePath> AddModFile(string mod_name, string path, int random_fill=128)
{
2020-10-18 19:03:50 +00:00
var full_path = ModsPath.Combine(mod_name, path);
2020-03-26 21:15:44 +00:00
full_path.Parent.CreateDirectory();
await GenerateRandomFileData(full_path, random_fill);
return full_path;
}
public async Task GenerateRandomFileData(AbsolutePath full_path, int random_fill)
2019-09-24 04:20:24 +00:00
{
byte[] bytes = new byte[0];
if (random_fill != 0)
{
bytes = new byte[random_fill];
RNG.NextBytes(bytes);
}
await full_path.WriteAllBytesAsync(bytes);
2019-09-24 04:20:24 +00:00
}
public static byte[] RandomData(int? size = null, int maxSize = 1024)
{
if (size == null)
size = _rng.Next(1, maxSize);
var arr = new byte[(int) size];
_rng.NextBytes(arr);
return arr;
}
public async ValueTask DisposeAsync()
2019-09-24 04:20:24 +00:00
{
var exts = new[] { ".md", ".exe" };
await WorkingDirectory.Combine(ID).DeleteDirectory();
2019-09-24 04:20:24 +00:00
Profiles.Do(p =>
{
foreach (var ext in exts)
{
2019-09-24 04:20:24 +00:00
var path = Path.Combine(Directory.GetCurrentDirectory(), p + ext);
if (File.Exists(path))
File.Delete(path);
}
});
}
/// <summary>
/// Returns a random string name (with spaces)
/// </summary>
public string RandomName()
{
return Guid.NewGuid().ToString();
}
2020-04-22 20:58:50 +00:00
public byte[] RandomData(int size = 0)
{
if (size == 0)
size = _rng.Next(256);
var data = new byte[size];
_rng.NextBytes(data);
return data;
}
public async ValueTask<string> AddManualDownload(Dictionary<string, byte[]> contents)
2019-09-24 04:20:24 +00:00
{
var name = RandomName() + ".zip";
2020-10-18 19:03:50 +00:00
await using FileStream fs = await DownloadsPath.Combine(name).Create();
2020-03-26 21:15:44 +00:00
using ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Create);
foreach (var (key, value) in contents)
2019-09-24 04:20:24 +00:00
{
Utils.Log($"Adding {value.Length.ToFileSizeString()} entry {key}");
var entry = archive.CreateEntry(key);
await using var os = entry.Open();
await os.WriteAsync(value, 0, value.Length);
}
2019-09-24 04:20:24 +00:00
2020-10-18 19:03:50 +00:00
await DownloadsPath.Combine(name + Consts.MetaFileExtension).WriteAllLinesAsync(
"[General]",
"manualURL=<TESTING>"
);
2019-09-24 04:20:24 +00:00
return name;
}
public async Task VerifyInstalledFile(string mod, string file)
2019-09-24 04:20:24 +00:00
{
2020-10-18 19:03:50 +00:00
var src = SourcePath.Combine((string)Consts.MO2ModFolderName, mod, file);
2020-03-26 21:15:44 +00:00
Assert.True(src.Exists);
2019-09-24 04:20:24 +00:00
2020-10-18 19:03:50 +00:00
var dest = InstallPath.Combine((string)Consts.MO2ModFolderName, mod, file);
2020-03-28 02:54:14 +00:00
Assert.True(dest.Exists, $"Destination {dest} doesn't exist");
2019-09-24 04:20:24 +00:00
var srcData = await src.ReadAllBytesAsync();
var destData = await dest.ReadAllBytesAsync();
2019-09-24 04:20:24 +00:00
2020-03-26 21:15:44 +00:00
Assert.Equal(srcData.Length, destData.Length);
2019-09-24 04:20:24 +00:00
2020-03-26 21:15:44 +00:00
for(int x = 0; x < srcData.Length; x++)
2019-09-24 04:20:24 +00:00
{
2020-03-26 21:15:44 +00:00
if (srcData[x] != destData[x])
Assert.True(false, $"Index {x} of {mod}\\{file} are not the same");
2019-09-24 04:20:24 +00:00
}
}
2020-01-07 00:24:33 +00:00
public async Task VerifyInstalledGameFile(string file)
2020-01-07 00:24:33 +00:00
{
2020-03-26 21:15:44 +00:00
var src = GameFolder.Combine(file);
Assert.True(src.Exists);
2020-01-07 00:24:33 +00:00
2020-10-18 19:03:50 +00:00
var dest = InstallPath.Combine((string)Consts.GameFolderFilesDir, file);
2020-03-26 21:15:44 +00:00
Assert.True(dest.Exists);
2020-01-07 00:24:33 +00:00
var srcData = await src.ReadAllBytesAsync();
var destData = await dest.ReadAllBytesAsync();
2020-01-07 00:24:33 +00:00
2020-03-26 21:15:44 +00:00
Assert.Equal(srcData.Length, destData.Length);
2020-03-26 21:15:44 +00:00
for(int x = 0; x < srcData.Length; x++)
2020-01-07 00:24:33 +00:00
{
2020-03-26 21:15:44 +00:00
if (srcData[x] != destData[x])
Assert.True(false, $"Index {x} of {Consts.GameFolderFilesDir}\\{file} are not the same");
2020-01-07 00:24:33 +00:00
}
}
2020-03-26 21:15:44 +00:00
public AbsolutePath PathOfInstalledFile(string mod, string file)
{
2020-10-18 19:03:50 +00:00
return InstallPath.Combine((string)Consts.MO2ModFolderName, mod, file);
}
public async ValueTask VerifyAllFiles(bool gameFileShouldNotExistInGameFolder = true)
{
if (gameFileShouldNotExistInGameFolder)
{
foreach (var file in Game.MetaData().RequiredFiles!)
{
2020-10-18 19:03:50 +00:00
Assert.False(InstallPath.Combine(Consts.GameFolderFilesDir, (RelativePath)file).Exists);
}
}
2020-03-26 21:15:44 +00:00
var skipFiles = new []{"portable.txt"}.Select(e => (RelativePath)e).ToHashSet();
2020-10-18 19:03:50 +00:00
foreach (var destFile in InstallPath.EnumerateFiles())
{
2020-10-18 19:03:50 +00:00
var relFile = destFile.RelativeTo(InstallPath);
if (destFile.InFolder(Consts.LOOTFolderFilesDir.RelativeTo(SourcePath)) || destFile.InFolder(Consts.GameFolderFilesDir.RelativeTo(SourcePath)))
continue;
2020-01-06 23:08:19 +00:00
2020-03-26 21:15:44 +00:00
if (!skipFiles.Contains(relFile))
2020-10-18 19:03:50 +00:00
Assert.True(SourcePath.Combine(relFile).Exists, $"Only in Destination: {relFile}");
}
2020-03-26 21:15:44 +00:00
var skipExtensions = new []{".txt", ".ini"}.Select(e => new Extension(e)).ToHashSet();
2020-10-18 19:03:50 +00:00
foreach (var srcFile in SourcePath.EnumerateFiles())
{
2020-10-18 19:03:50 +00:00
var relFile = srcFile.RelativeTo(SourcePath);
2020-03-26 21:15:44 +00:00
if (relFile.StartsWith("downloads\\"))
continue;
2020-10-18 19:03:50 +00:00
var destFile = InstallPath.Combine(relFile);
2020-03-26 21:15:44 +00:00
Assert.True(destFile.Exists, $"Only in Source: {relFile}");
2020-03-26 21:15:44 +00:00
if (!skipExtensions.Contains(srcFile.Extension))
{
2020-03-26 21:15:44 +00:00
Assert.Equal(srcFile.Size, destFile.Size);
Assert.Equal(await srcFile.FileHashAsync(), await destFile.FileHashAsync());
}
}
}
2020-01-07 00:24:33 +00:00
public async ValueTask<AbsolutePath> AddGameFile(string path, int i)
2020-01-07 00:24:33 +00:00
{
2020-03-26 21:15:44 +00:00
var fullPath = GameFolder.Combine(path);
fullPath.Parent.CreateDirectory();
await GenerateRandomFileData(fullPath, i);
2020-03-26 21:15:44 +00:00
return fullPath;
2020-01-07 00:24:33 +00:00
}
2020-10-18 19:03:50 +00:00
public void CreatePaths()
{
SourcePath.CreateDirectory();
DownloadsPath.CreateDirectory();
InstallPath.CreateDirectory();
}
2019-09-24 04:20:24 +00:00
}
}