From 3ea261fc0a8272ab1a9644052af5b67ec6941a18 Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 24 Jan 2023 18:10:56 +0100 Subject: [PATCH] Condense some facts into one theory, change windows test zip to be more logical --- AM2RPortHelperLib/RawMods.cs | 2 +- AM2RPortHelperTests/GameWin.zip | Bin 2838325 -> 2838311 bytes AM2RPortHelperTests/RawModsTests.cs | 194 ++++++++++------------------ 3 files changed, 71 insertions(+), 125 deletions(-) diff --git a/AM2RPortHelperLib/RawMods.cs b/AM2RPortHelperLib/RawMods.cs index c6fc26f..ea8b80a 100644 --- a/AM2RPortHelperLib/RawMods.cs +++ b/AM2RPortHelperLib/RawMods.cs @@ -108,7 +108,7 @@ public abstract class RawMods : ModsBase if (currentOS == ModOS.Windows) { outputDelegate.SendOutput("Zip is already a raw Windows zip. Copying to output directory..."); - File.Copy(inputRawZipPath, outputRawZipPath, true); + File.Copy(inputRawZipPath, outputRawZipPath); return; } diff --git a/AM2RPortHelperTests/GameWin.zip b/AM2RPortHelperTests/GameWin.zip index 8e2dbf4a5d20094a1d911c90818a29407ab99f93..d3b79a92692523ab2f0087f426b5a82264ee376a 100644 GIT binary patch delta 253 zcmXwzyG}wu7=(9K;CRI=Dqau)<#Q7X6APk^wd{%U5$sNLFww@!P)u)V*2>Dn%97fr z@DXf%1>I9PlgZ39|38!05BnbVTu%~X%%wlQ86m8g64PQvltfunL?ULzoR}92Vo@xK zs#q2)VpXh(b+I98VpD91ZBZ9HVpr^mebEpHqA3nVOSDBt9EoGm74h}S1o!A`#hQP- zjfzxk3!mQg?05L_u5YWMn?@Ui+ti)eJiMgt!uG>g>drgA180VJ57{uk?`3&*eN`$K OH!6{1Ys`~(vHby;@<1;D delta 267 zcmYk0yGjF57=+JJv+=%OqprrpPvh-C3c*6O&{}p8<13`tCXsjn%M@c_Wp}W#609sN z@(jL)58y+1wh)|Qn1O-+pZC`fI|zDC;@B8-mL6QzTkYO`yVplpF(ih?h$x9jl*OnR z6XRk+Oo}NnEn+bvX2qPC7YkxhRK${47AqnVt71)5#k#18y4Vom%cj^84Y4hD#ID#A z`vcrVS}jKX;jLe!e-r&@>YDb)f2Z!qR(z5LYx%3p9ojqpl(`do>c28~+=x(() => RawMods.GetModOSOfRawZip(destinationZip)); File.Delete(destinationZip); } [Fact] - public void LinuxZipWithGoodRunnerShouldBeLinux() - { - var result = RawMods.GetModOSOfRawZip("./GameLin.zip"); - Assert.True(result == Core.ModOS.Linux); - } - - [Fact] - public void LinuxZipWithWrongRunnerShouldThrow() + public void WindowsZipWithDifferentRunnerShouldSucceed() { - var destinationZip = Path.GetTempPath() + Guid.NewGuid(); - ZipFile.ExtractToDirectory("./GameLin.zip", testTempDir); - File.Move(testTempDir + "/runner", testTempDir + "/AM2R"); + var destinationZip = Path.GetTempPath() + Guid.NewGuid() + ".zip"; + ZipFile.ExtractToDirectory("./GameWin.zip", testTempDir); + File.Move(testTempDir + "/AM2R.exe", testTempDir + "/Game.exe"); ZipFile.CreateFromDirectory(testTempDir, destinationZip); - Assert.Throws(() => RawMods.GetModOSOfRawZip(destinationZip)); + var result = RawMods.GetModOSOfRawZip(destinationZip); + Assert.True(result == Core.ModOS.Windows); File.Delete(destinationZip); } [Fact] - public void MacZipWithGoodRunnerShouldBeMac() - { - var result = RawMods.GetModOSOfRawZip("./GameMac.zip"); - Assert.True(result == Core.ModOS.Mac); - } - - [Fact] - public void MacZipWithWrongRunnerShouldThrow() + public void WindowsZipWithTwoRunnersShouldThrow() { var destinationZip = Path.GetTempPath() + Guid.NewGuid(); - ZipFile.ExtractToDirectory("./GameMac.zip", testTempDir); - File.Move(testTempDir + "/AM2R.app/Contents/MacOS/Mac_Runner", testTempDir + "/AM2R.app/Contents/MacOS/Binary"); - ZipFile.CreateFromDirectory(testTempDir, destinationZip); - Assert.Throws(() => RawMods.GetModOSOfRawZip(destinationZip)); - File.Delete(destinationZip); - } - - [Fact] - public void WindowsZipWithInvalidDataFileShouldThrow() - { - var destinationZip = Path.GetTempPath() + Guid.NewGuid() + ".zip"; ZipFile.ExtractToDirectory("./GameWin.zip", testTempDir); - File.Move(testTempDir + "/data.win", testTempDir + "/data.win_"); + File.Copy(testTempDir + "/AM2R.exe", testTempDir + "/Game.exe"); ZipFile.CreateFromDirectory(testTempDir, destinationZip); Assert.Throws(() => RawMods.GetModOSOfRawZip(destinationZip)); File.Delete(destinationZip); } - [Fact] - public void LinuxZipWithInvalidDataFileShouldThrow() + [Theory] + [InlineData("./GameWin.zip", "/data.win")] + [InlineData("./GameLin.zip", "/assets/game.unx")] + [InlineData("./GameMac.zip", "/AM2R.app/Contents/Resources/game.ios")] + public void OSZipWithInvalidDataFileShouldThrow(string input, string dataSuffix) { var destinationZip = Path.GetTempPath() + Guid.NewGuid() + ".zip"; - ZipFile.ExtractToDirectory("./GameLin.zip", testTempDir); - File.Move(testTempDir + "/assets/game.unx", testTempDir + "/assets/game.unx_"); - ZipFile.CreateFromDirectory(testTempDir, destinationZip); - Assert.Throws(() => RawMods.GetModOSOfRawZip(destinationZip)); - File.Delete(destinationZip); - } - - [Fact] - public void MacZipWithInvalidDataFileShouldThrow() - { - var destinationZip = Path.GetTempPath() + Guid.NewGuid(); - ZipFile.ExtractToDirectory("./GameMac.zip", testTempDir); - File.Move(testTempDir + "/AM2R.app/Contents/Resources/game.ios", testTempDir + "//AM2R.app/Contents/Resources/game.ios_"); + ZipFile.ExtractToDirectory(input, testTempDir); + File.Move(testTempDir + dataSuffix, testTempDir + dataSuffix + "_"); ZipFile.CreateFromDirectory(testTempDir, destinationZip); Assert.Throws(() => RawMods.GetModOSOfRawZip(destinationZip)); File.Delete(destinationZip); @@ -303,7 +264,7 @@ public class RawModsTests : IDisposable // File contents should be same between the zips except for old runner+d3d.dll, new splash+icon, // files being lowered and data file being different var origFiles = new DirectoryInfo(origExtract).GetFiles().Select(f => f.Name.ToLower()).ToList(); - origFiles.Remove("am2r server.exe"); + origFiles.Remove("am2r.exe"); origFiles.Remove("d3dx9_43.dll"); origFiles.Add("splash.png"); origFiles.Add("icon.png"); @@ -458,7 +419,7 @@ public class RawModsTests : IDisposable // File contents should be same between the zips except for old runner+d3d.dll, new splash+icon, // files being lowercase, data file being different a new gamecontrollerdb and a new yoyorunner.config var origFiles = new DirectoryInfo(origExtract).GetFiles().Select(f => f.Name.ToLower()).ToList(); - origFiles.Remove("am2r server.exe"); + origFiles.Remove("am2r.exe"); origFiles.Remove("d3dx9_43.dll"); origFiles.Add("splash.png"); origFiles.Add("icon.png"); @@ -551,72 +512,57 @@ public class RawModsTests : IDisposable #region PortInvalidZips - [Fact] - public void PortInvalidZipToWindows() - { - Assert.Throws(() => RawMods.PortToWindows(null, "/foo")); - Assert.Throws(() => RawMods.PortToWindows("/foo", "/foo")); - Assert.Throws(() => RawMods.PortToWindows("./GameLin.zip", null)); - } - - [Fact] - public void PortInvalidZipToLinux() - { - Assert.Throws(() => RawMods.PortToLinux(null, "/foo")); - Assert.Throws(() => RawMods.PortToLinux("/foo", "/foo")); - Assert.Throws(() => RawMods.PortToLinux("./GameLin.zip", null)); - } - - [Fact] - public void PortInvalidZipToMac() + [Theory] + [InlineData(Core.ModOS.Windows)] + [InlineData(Core.ModOS.Linux)] + [InlineData(Core.ModOS.Mac)] + public void PortInvalidZipsToOS(Core.ModOS os) { - Assert.Throws(() => RawMods.PortToMac(null, "/foo")); - Assert.Throws(() => RawMods.PortToMac("/foo", "/foo")); - Assert.Throws(() => RawMods.PortToMac("./GameLin.zip", null)); + Action function = os switch + { + Core.ModOS.Windows => (input, outputFile) => RawMods.PortToWindows(input, outputFile), + Core.ModOS.Linux => (input, outputFile) => RawMods.PortToLinux(input, outputFile), + Core.ModOS.Mac => (input, outputFile) => RawMods.PortToMac(input, outputFile), + _ => throw new Exception("This should not have happened! new unhandled data!") + }; + + Assert.Throws(() => function.Invoke(null, "/foo")); + Assert.Throws(() => function.Invoke("/foo", "/foo")); + Assert.Throws(() => function.Invoke("./GameLin.zip", null)); + } - #endregion #region Make sure porting methods work when called in succession - [Fact] - public void TestPortToWindowsMultipleTimes() - { - const string input = "./GameLin.zip"; - string outputZip = testTempDir + "/foobar.zip"; - RawMods.PortToWindows(input, outputZip); - Assert.Throws(() => RawMods.PortToWindows(input, outputZip)); - File.Delete(outputZip); - RawMods.PortToWindows(input, outputZip); - Assert.True(File.Exists(outputZip)); - } - - [Fact] - public void TestPortToLinuxMultipleTimes() - { - const string input = "./GameLin.zip"; - string outputZip = testTempDir + "/foobar.zip"; - RawMods.PortToLinux(input, outputZip); - Assert.Throws(() => RawMods.PortToLinux(input, outputZip)); - File.Delete(outputZip); - RawMods.PortToLinux(input, outputZip); - Assert.True(File.Exists(outputZip)); - } - [Theory] - [InlineData("./GameWin.zip")] - [InlineData("./GameLin.zip")] - [InlineData("./GameMac.zip")] - public void TestPortToMacMultipleTimes(string input) + [InlineData("./GameWin.zip", Core.ModOS.Windows)] + [InlineData("./GameWin.zip", Core.ModOS.Linux)] + [InlineData("./GameWin.zip", Core.ModOS.Mac)] + [InlineData("./GameLin.zip", Core.ModOS.Windows)] + [InlineData("./GameLin.zip", Core.ModOS.Linux)] + [InlineData("./GameLin.zip", Core.ModOS.Mac)] + [InlineData("./GameMac.zip", Core.ModOS.Windows)] + [InlineData("./GameMac.zip", Core.ModOS.Linux)] + [InlineData("./GameMac.zip", Core.ModOS.Mac)] + public void TestPortToOSMultipleTimes(string input, Core.ModOS os) { + Action function = os switch + { + Core.ModOS.Windows => (inputFile, outputFile) => RawMods.PortToWindows(inputFile, outputFile), + Core.ModOS.Linux => (inputFile, outputFile) => RawMods.PortToLinux(inputFile, outputFile), + Core.ModOS.Mac => (inputFile, outputFile) => RawMods.PortToMac(inputFile, outputFile), + _ => throw new Exception("This should not have happened! new unhandled data!") + }; + string outputZip = testTempDir + "/foobar.zip"; - RawMods.PortToMac(input, outputZip); - Assert.Throws(() => RawMods.PortToMac(input, outputZip)); + function.Invoke(input, outputZip); + Assert.Throws(() => function.Invoke(input, outputZip)); File.Delete(outputZip); - RawMods.PortToMac(input, outputZip); + function.Invoke(input, outputZip); Assert.True(File.Exists(outputZip)); } - + #endregion // TODO: write tests for porttoandroid