Skip to content

Commit

Permalink
Tests for replaced_by
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 13, 2023
1 parent 34991d6 commit fb2ab71
Showing 1 changed file with 126 additions and 38 deletions.
164 changes: 126 additions & 38 deletions Tests/Core/ModuleInstallerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NUnit.Framework;

using CKAN;
using CKAN.Versioning;
using CKAN.Games.KerbalSpaceProgram;

using Tests.Core.Configuration;
Expand All @@ -20,39 +21,11 @@ namespace Tests.Core
[TestFixture]
public class ModuleInstallerTests
{
private string flag_path;
private string dogezip;
private CkanModule dogemod;

private string mm_zip;
private CkanModule mm_mod;

private string mission_zip;
private CkanModule mission_mod;

private IUser nullUser;
private const string flag_path = "DogeCoinFlag-1.01/GameData/DogeCoinFlag/Flags/dogecoin.png";
private IUser nullUser = new NullUser();

private DisposableKSP ksp = new DisposableKSP();

[SetUp]
public void Setup()
{
// By setting these for every test, we can make sure our tests can change
// them any way they like without harming other tests.

flag_path = "DogeCoinFlag-1.01/GameData/DogeCoinFlag/Flags/dogecoin.png";
dogezip = TestData.DogeCoinFlagZip();
dogemod = TestData.DogeCoinFlag_101_module();

mm_zip = TestData.ModuleManagerZip();
mm_mod = TestData.ModuleManagerModule();

mission_zip = TestData.MissionZip();
mission_mod = TestData.MissionModule();

nullUser = new NullUser();
}

[OneTimeTearDown]
public void TearDown()
{
Expand Down Expand Up @@ -890,21 +863,136 @@ public void AllowInstallsToScenarios()
}

[Test]
public void SuccessfulReplacement()
public void Replace_WithCompatibleModule_Succeeds()
{
//Need to set up an installed DogeCoinFlag-101replaced mod that can validly be replaced by DogeTokenFlag-101
// Arrange
var user = new NullUser();
using (var inst = new DisposableKSP())
using (var repo = new TemporaryRepository(
@"{
""spec_version"": ""v1.4"",
""identifier"": ""replaced"",
""version"": ""1.0"",
""ksp_version"": ""1.12"",
""replaced_by"": {
""name"": ""replacer""
},
""download"": ""https://awesomemod.example/AwesomeMod.zip"",
}",
@"{
""spec_version"": ""v1.4"",
""identifier"": ""replacer"",
""version"": ""1.0"",
""ksp_version"": ""1.12"",
""download"": ""https://awesomemod.example/AwesomeMod.zip"",
""install"": [
{
""file"": ""DogeCoinFlag-1.01/GameData/DogeCoinFlag"",
""install_to"": ""GameData"",
""filter"" : [ ""Thumbs.db"", ""README.md"" ],
""filter_regexp"" : ""\\.bak$""
}
]
}"))
using (var repoData = new TemporaryRepositoryData(user, repo.repo))
using (var config = new FakeConfiguration(inst.KSP, inst.KSP.Name))
using (var manager = new GameInstanceManager(nullUser, config)
{
CurrentInstance = inst.KSP
})
{
var regMgr = CKAN.RegistryManager.Instance(manager.CurrentInstance, repoData.Manager);
var registry = regMgr.registry;
IRegistryQuerier querier = registry;
registry.RepositoriesAdd(repo.repo);
var replaced = registry.GetModuleByVersion("replaced", "1.0");
Assert.IsNotNull(replaced, "Replaced module should exist");
var replacer = registry.GetModuleByVersion("replacer", "1.0");
Assert.IsNotNull(replacer, "Replacer module should exist");
var installer = new CKAN.ModuleInstaller(inst.KSP, manager.Cache, nullUser);
HashSet<string> possibleConfigOnlyDirs = null;
var downloader = new NetAsyncModulesDownloader(user, manager.Cache);

// Act
registry.RegisterModule(replaced, Enumerable.Empty<string>(), inst.KSP, false);
manager.Cache.Store(replaced, TestData.DogeCoinFlagZip(), new Progress<long>(bytes => {}));
var replacement = querier.GetReplacement(replaced.identifier,
new GameVersionCriteria(new GameVersion(1, 12)));
installer.Replace(Enumerable.Repeat<ModuleReplacement>(replacement, 1),
new RelationshipResolverOptions(),
downloader, ref possibleConfigOnlyDirs, regMgr,
false);

// Assert that DogeCoinFlag has been removed and DogeTokenFlag has been installed
Assert.IsTrue(true);
// Assert
CollectionAssert.AreEqual(
Enumerable.Repeat<CkanModule>(replacer, 1),
registry.InstalledModules.Select(im => im.Module));
}
}

[Test]
public void UnsuccessfulReplacement()
public void Replace_WithIncompatibleModule_Fails()
{
//Need to set up an installed DogeCoinFlag-101-replaced mod in a KSP version too low for DogeTokenFlag-101
// Arrange
var user = new NullUser();
using (var inst = new DisposableKSP())
using (var repo = new TemporaryRepository(
@"{
""spec_version"": ""v1.4"",
""identifier"": ""replaced"",
""version"": ""1.0"",
""ksp_version"": ""1.12"",
""replaced_by"": {
""name"": ""replacer""
},
""download"": ""https://awesomemod.example/AwesomeMod.zip"",
}",
@"{
""spec_version"": ""v1.4"",
""identifier"": ""replacer"",
""version"": ""1.0"",
""ksp_version"": ""1.12"",
""download"": ""https://awesomemod.example/AwesomeMod.zip"",
""install"": [
{
""file"": ""DogeCoinFlag-1.01/GameData/DogeCoinFlag"",
""install_to"": ""GameData"",
""filter"" : [ ""Thumbs.db"", ""README.md"" ],
""filter_regexp"" : ""\\.bak$""
}
]
}"))
using (var repoData = new TemporaryRepositoryData(user, repo.repo))
using (var config = new FakeConfiguration(inst.KSP, inst.KSP.Name))
using (var manager = new GameInstanceManager(nullUser, config)
{
CurrentInstance = inst.KSP
})
{
var regMgr = CKAN.RegistryManager.Instance(manager.CurrentInstance, repoData.Manager);
var registry = regMgr.registry;
IRegistryQuerier querier = registry;
registry.RepositoriesAdd(repo.repo);
var replaced = registry.GetModuleByVersion("replaced", "1.0");
Assert.IsNotNull(replaced, "Replaced module should exist");
var replacer = registry.GetModuleByVersion("replacer", "1.0");
Assert.IsNotNull(replacer, "Replacer module should exist");
var installer = new CKAN.ModuleInstaller(inst.KSP, manager.Cache, nullUser);
HashSet<string> possibleConfigOnlyDirs = null;
var downloader = new NetAsyncModulesDownloader(user, manager.Cache);

// Act
registry.RegisterModule(replaced, Enumerable.Empty<string>(), inst.KSP, false);
manager.Cache.Store(replaced, TestData.DogeCoinFlagZip(), new Progress<long>(bytes => {}));
var replacement = querier.GetReplacement(replaced.identifier,
new GameVersionCriteria(new GameVersion(1, 11)));

// Assert that DogeCoinFlag has not been removed and DogeTokenFlag has not been installed
Assert.IsTrue(true);
// Assert
Assert.IsNull(replacement);
CollectionAssert.AreEqual(
Enumerable.Repeat<CkanModule>(replaced, 1),
registry.InstalledModules.Select(im => im.Module));
}
}
}
}

0 comments on commit fb2ab71

Please sign in to comment.