Skip to content

Commit

Permalink
Nullable references for Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 3, 2024
1 parent 8776ff7 commit 1545b63
Show file tree
Hide file tree
Showing 63 changed files with 779 additions and 805 deletions.
4 changes: 2 additions & 2 deletions Tests/CmdLine/InstallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void RunCommand_IdentifierEqualsVersionSyntax_InstallsCorrectVersion(
var regMgr = RegistryManager.Instance(inst.KSP, repoData.Manager);
regMgr.registry.RepositoriesClear();
regMgr.registry.RepositoriesAdd(repo.repo);
var module = regMgr.registry.GetModuleByVersion(identifier, version);
manager.Cache.Store(module, TestData.DogeCoinFlagZip(), null);
var module = regMgr.registry.GetModuleByVersion(identifier, version)!;
manager.Cache?.Store(module, TestData.DogeCoinFlagZip(), null);
var opts = new InstallOptions()
{
modules = new List<string> { $"{identifier}={version}" },
Expand Down
16 changes: 8 additions & 8 deletions Tests/CmdLine/UpgradeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public void RunCommand_IdentifierEqualsVersionSyntax_UpgradesToCorrectVersion(
var regMgr = RegistryManager.Instance(inst.KSP, repoData.Manager);
regMgr.registry.RepositoriesClear();
regMgr.registry.RepositoriesAdd(repo.repo);
var fromModule = regMgr.registry.GetModuleByVersion(identifier, fromVersion);
var toModule = regMgr.registry.GetModuleByVersion(identifier, toVersion);
var fromModule = regMgr.registry.GetModuleByVersion(identifier, fromVersion)!;
var toModule = regMgr.registry.GetModuleByVersion(identifier, toVersion)!;
regMgr.registry.RegisterModule(fromModule, new List<string>(), inst.KSP, false);
manager.Cache.Store(toModule, TestData.DogeCoinFlagZip(), null);
manager.Cache?.Store(toModule, TestData.DogeCoinFlagZip(), null);
var opts = new UpgradeOptions()
{
modules = new List<string> { $"{identifier}={toVersion}" },
Expand Down Expand Up @@ -632,7 +632,7 @@ public void RunCommand_VersionDependsUpgrade_UpgradesCorrectly(string descript
// Pre-store mods that might be installed
foreach (var toModule in addlModules.Select(CkanModule.FromJson))
{
manager.Cache.Store(toModule, TestData.DogeCoinFlagZip(), null);
manager.Cache?.Store(toModule, TestData.DogeCoinFlagZip(), null);
}
// Simulate passing `--all`
var opts = upgradeIdentifiers != null
Expand All @@ -652,10 +652,10 @@ public void RunCommand_VersionDependsUpgrade_UpgradesCorrectly(string descript

// Assert
CollectionAssert.AreEqual(versionsAfter,
instMods.Select(m => regMgr.registry
.GetInstalledVersion(m.identifier)
.version
.ToString())
instMods.Select(m => regMgr?.registry
.GetInstalledVersion(m.identifier)
?.version
.ToString())
.ToArray(),
description);

Expand Down
92 changes: 49 additions & 43 deletions Tests/Core/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace Tests.Core
[TestFixture]
public class Cache
{
private string cache_dir;
private string? cache_dir;

private NetFileCache cache;
private NetModuleCache module_cache;
private NetFileCache? cache;
private NetModuleCache? module_cache;

[SetUp]
public void MakeCache()
Expand All @@ -30,11 +30,14 @@ public void MakeCache()
[TearDown]
public void RemoveCache()
{
cache.Dispose();
cache?.Dispose();
cache = null;
module_cache.Dispose();
module_cache?.Dispose();
module_cache = null;
Directory.Delete(cache_dir, true);
if (cache_dir != null)
{
Directory.Delete(cache_dir, true);
}
}

[Test]
Expand All @@ -51,30 +54,32 @@ public void StoreRetrieve()
string file = TestData.DogeCoinFlagZip();

// Our URL shouldn't be cached to begin with.
Assert.IsFalse(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));

// Store our file.
cache.Store(url, file);
cache?.Store(url, file);

// Now it should be cached.
Assert.IsTrue(cache.IsCached(url));
Assert.IsTrue(cache?.IsCached(url));

// Check contents match.
string cached_file = cache.GetCachedFilename(url);
var cached_file = cache?.GetCachedFilename(url);
FileAssert.AreEqual(file, cached_file);
}

[Test, TestCase("cheesy.zip","cheesy.zip"), TestCase("Foo-1-2.3","Foo-1-2.3"),
TestCase("Foo-1-2-3","Foo-1-2-3"), TestCase("Foo-..-etc-passwd","Foo-..-etc-passwd")]
[Test, TestCase("cheesy.zip","cheesy.zip"),
TestCase("Foo-1-2.3","Foo-1-2.3"),
TestCase("Foo-1-2-3","Foo-1-2-3"),
TestCase("Foo-..-etc-passwd","Foo-..-etc-passwd")]
public void NamingHints(string hint, string appendage)
{
Uri url = new Uri("http://example.com/");
string file = TestData.DogeCoinFlagZip();

Assert.IsFalse(cache.IsCached(url));
cache.Store(url, file, hint);
Assert.IsFalse(cache?.IsCached(url));
cache?.Store(url, file, hint);

StringAssert.EndsWith(appendage, cache.GetCachedFilename(url));
StringAssert.EndsWith(appendage, cache?.GetCachedFilename(url));
}

[Test]
Expand All @@ -83,13 +88,13 @@ public void StoreRemove()
Uri url = new Uri("http://example.com/");
string file = TestData.DogeCoinFlagZip();

Assert.IsFalse(cache.IsCached(url));
cache.Store(url, file);
Assert.IsTrue(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));
cache?.Store(url, file);
Assert.IsTrue(cache?.IsCached(url));

cache.Remove(url);
cache?.Remove(url);

Assert.IsFalse(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));
}

[Test]
Expand All @@ -113,22 +118,22 @@ public void StoreInvalid()
// Try to store a nonexistent zip into a NetModuleCache
// and expect an FileNotFoundKraken
Assert.Throws<FileNotFoundKraken>(() =>
module_cache.Store(
module_cache?.Store(
TestData.DogeCoinFlag_101_LZMA_module,
"/DoesNotExist.zip", new Progress<int>(percent => {})));

// Try to store the LZMA-format DogeCoin zip into a NetModuleCache
// and expect an InvalidModuleFileKraken
Assert.Throws<InvalidModuleFileKraken>(() =>
module_cache.Store(
module_cache?.Store(
TestData.DogeCoinFlag_101_LZMA_module,
TestData.DogeCoinFlagZipLZMA, new Progress<int>(percent => {})));

// Try to store the normal DogeCoin zip into a NetModuleCache
// using the WRONG metadata (file size and hashes)
// and expect an InvalidModuleFileKraken
Assert.Throws<InvalidModuleFileKraken>(() =>
module_cache.Store(
module_cache?.Store(
TestData.DogeCoinFlag_101_LZMA_module,
TestData.DogeCoinFlagZip(), new Progress<int>(percent => {})));
}
Expand All @@ -140,19 +145,19 @@ public void DoubleCache()
// the most recent file we store for any given URL.

Uri url = new Uri("http://Double.Rainbow.What.Does.It.Mean/");
Assert.IsFalse(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));

string file1 = TestData.DogeCoinFlagZip();
string file2 = TestData.ModuleManagerZip();

cache.Store(url, file1);
FileAssert.AreEqual(file1, cache.GetCachedFilename(url));
cache?.Store(url, file1);
FileAssert.AreEqual(file1, cache?.GetCachedFilename(url));

cache.Store(url, file2);
FileAssert.AreEqual(file2, cache.GetCachedFilename(url));
cache?.Store(url, file2);
FileAssert.AreEqual(file2, cache?.GetCachedFilename(url));

cache.Store(url, file1);
FileAssert.AreEqual(file1, cache.GetCachedFilename(url));
cache?.Store(url, file1);
FileAssert.AreEqual(file1, cache?.GetCachedFilename(url));
}

[Test]
Expand All @@ -161,21 +166,22 @@ public void ZipValidation()
// We could use any URL, but this one is awesome. <3
Uri url = new Uri("http://kitte.nz/");

Assert.IsFalse(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));

// Store a bad zip.
cache.Store(url, TestData.DogeCoinFlagZipCorrupt());
cache?.Store(url, TestData.DogeCoinFlagZipCorrupt());

// Make sure it's stored
Assert.IsTrue(cache.IsCached(url));
Assert.IsTrue(cache?.IsCached(url));
// Make sure it's not valid as a zip
Assert.IsFalse(NetModuleCache.ZipValid(cache.GetCachedFilename(url), out _, null));
Assert.IsFalse(NetModuleCache.ZipValid(cache?.GetCachedFilename(url) ?? "",
out _, null));

// Store a good zip.
cache.Store(url, TestData.DogeCoinFlagZip());
cache?.Store(url, TestData.DogeCoinFlagZip());

// Make sure it's stored, and valid.
Assert.IsTrue(cache.IsCached(url));
Assert.IsTrue(cache?.IsCached(url));
}

[Test]
Expand Down Expand Up @@ -209,7 +215,7 @@ public void ZipValid_ContainsFilenameWithBadChars_NoException()
public void ZipValid_ContainsFilenameWithUnicodeChars_Valid()
{
bool valid = false;
string reason = null;
string? reason = null;

Assert.DoesNotThrow(() =>
valid = NetModuleCache.ZipValid(TestData.ZipWithUnicodeChars, out reason, null));
Expand All @@ -225,11 +231,11 @@ public void EnforceSizeLimit_UnderLimit_FileRetained()

// Act
Uri url = new Uri("http://kitte.nz/");
cache.Store(url, TestData.DogeCoinFlagZip());
cache.EnforceSizeLimit(fileSize + 100, registry);
cache?.Store(url, TestData.DogeCoinFlagZip());
cache?.EnforceSizeLimit(fileSize + 100, registry);

// Assert
Assert.IsTrue(cache.IsCached(url));
Assert.IsTrue(cache?.IsCached(url));
}

[Test]
Expand All @@ -241,11 +247,11 @@ public void EnforceSizeLimit_OverLimit_FileRemoved()

// Act
Uri url = new Uri("http://kitte.nz/");
cache.Store(url, TestData.DogeCoinFlagZip());
cache.EnforceSizeLimit(fileSize - 100, registry);
cache?.Store(url, TestData.DogeCoinFlagZip());
cache?.EnforceSizeLimit(fileSize - 100, registry);

// Assert
Assert.IsFalse(cache.IsCached(url));
Assert.IsFalse(cache?.IsCached(url));
}

}
Expand Down
25 changes: 14 additions & 11 deletions Tests/Core/Configuration/FakeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public FakeConfiguration(CKAN.GameInstance instance, string autostart)
/// </summary>
/// <param name="instances">List of name/path pairs for the instances</param>
/// <param name="auto_start_instance">The auto start instance to use</param>
public FakeConfiguration(List<Tuple<string, string, string>> instances, string auto_start_instance)
public FakeConfiguration(List<Tuple<string, string, string>> instances, string? auto_start_instance)
{
Instances = instances;
AutoStartInstance = auto_start_instance;
Expand All @@ -40,11 +40,11 @@ public FakeConfiguration(List<Tuple<string, string, string>> instances, string a
/// <summary>
/// Build map for the fake registry
/// </summary>
public JBuilds BuildMap { get; set; }
public JBuilds? BuildMap { get; set; }
/// <summary>
/// Path to download cache folder for the fake registry
/// </summary>
public string DownloadCacheDir { get; set; }
public string? DownloadCacheDir { get; set; }
/// <summary>
/// Maximum number of bytes of downloads to retain on disk
/// </summary>
Expand All @@ -60,12 +60,12 @@ public FakeConfiguration(List<Tuple<string, string, string>> instances, string a
public int InstanceCount => Instances.Count;

// In the Win32Registry it is not possible to get null in autostart.
private string _AutoStartInstance;
private string? _AutoStartInstance;

/// <summary>
/// The auto start instance for the fake registry
/// </summary>
public string AutoStartInstance
public string? AutoStartInstance
{
get => _AutoStartInstance ?? string.Empty;
#pragma warning disable IDE0027
Expand Down Expand Up @@ -107,7 +107,7 @@ public void SetRegistryToInstances(SortedList<string, CKAN.GameInstance> instanc
/// <summary>
/// The build map of the fake registry
/// </summary>
public JBuilds GetKSPBuilds() => BuildMap;
public JBuilds? GetKSPBuilds() => BuildMap;

/// <summary>
/// Set the build map for the fake registry
Expand All @@ -123,7 +123,7 @@ public IEnumerable<string> GetAuthTokenHosts()
throw new NotImplementedException();
}

public void SetAuthToken(string host, string token)
public void SetAuthToken(string host, string? token)
{
throw new NotImplementedException();
}
Expand All @@ -133,8 +133,8 @@ public bool TryGetAuthToken(string host, out string token)
throw new NotImplementedException();
}

private string _Language;
public string Language
private string? _Language;
public string? Language
{
get => _Language;

Expand All @@ -149,13 +149,16 @@ public string Language

public string[] GlobalInstallFilters { get; set; } = Array.Empty<string>();

public string[] PreferredHosts { get; set; } = Array.Empty<string>();
public string?[] PreferredHosts { get; set; } = Array.Empty<string>();

public bool? DevBuilds { get; set; }

public void Dispose()
{
Directory.Delete(DownloadCacheDir, true);
if (DownloadCacheDir != null)
{
Directory.Delete(DownloadCacheDir, true);
}
}
}
}
6 changes: 3 additions & 3 deletions Tests/Core/Configuration/JsonConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void LoadsGoodConfig()
"host3"
}, reg.GetAuthTokenHosts());

Assert.IsTrue(reg.TryGetAuthToken("host1", out string token));
Assert.IsTrue(reg.TryGetAuthToken("host1", out string? token));
Assert.AreEqual("token1", token);
Assert.IsTrue(reg.TryGetAuthToken("host2", out token));
Assert.AreEqual("token2", token);
Expand Down Expand Up @@ -139,7 +139,7 @@ public void LoadsExtraConfig()
"host3"
}, reg.GetAuthTokenHosts());

Assert.IsTrue(reg.TryGetAuthToken("host1", out string token));
Assert.IsTrue(reg.TryGetAuthToken("host1", out string? token));
Assert.AreEqual("token1", token);
Assert.IsTrue(reg.TryGetAuthToken("host2", out token));
Assert.AreEqual("token2", token);
Expand Down Expand Up @@ -339,7 +339,7 @@ public void AuthTokensPersist()
CollectionAssert.Contains(reg.GetAuthTokenHosts(), "test_host1");
CollectionAssert.Contains(reg.GetAuthTokenHosts(), "test_host2");

Assert.IsTrue(reg.TryGetAuthToken("test_host1", out string token));
Assert.IsTrue(reg.TryGetAuthToken("test_host1", out string? token));
Assert.AreEqual("hunter2", token);
Assert.IsTrue(reg.TryGetAuthToken("test_host2", out token));
Assert.AreEqual("asdf", token);
Expand Down
Loading

0 comments on commit 1545b63

Please sign in to comment.