Skip to content
102 changes: 102 additions & 0 deletions UpdateLib.Tests/Core/CacheManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UpdateLib.Abstractions.Storage;
using UpdateLib.Core;
using UpdateLib.Core.Storage;
using UpdateLib.Core.Storage.Files;
using Xunit;
using static System.Environment;
using static UpdateLib.Tests.Helpers;

namespace UpdateLib.Tests.Core
{
public class CacheManagerTests
{
[Fact]
public async Task NonExistingCacheCreatesANewOne()
{
var fs = new MockFileSystem();
var cache = new CacheStorage(fs);
var manager = CreateCacheManager(fs, cache);

await manager.UpdateCacheAsync();

Assert.NotNull(await cache.LoadAsync());
}

[Fact]
public async Task OldCacheEntriesAreDeleted()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>(), "C:\\app");
var cache = new CacheStorage(fs);

var file = new HashCacheFile();
file.Entries.Add(new HashCacheEntry("name", 0, ""));

await cache.SaveAsync(file);

var manager = CreateCacheManager(fs, cache);

await manager.UpdateCacheAsync();

var result = await cache.LoadAsync();

Assert.Empty(result.Entries);
}

[Fact]
public async Task UpdateCacheAddsNewEntries()
{
var fs = new MockFileSystem();
fs.AddFile("./myfile.txt", new MockFileData("blabla"));

var cache = new CacheStorage(fs);
var manager = CreateCacheManager(fs, cache);

await manager.UpdateCacheAsync();

var result = (await cache.LoadAsync()).Entries.First();

Assert.Equal(fs.Path.GetFullPath("./myfile.txt"), result.FilePath);
}

[Fact]
public async Task UpdateCacheAddsNewEntries_TempFilesAreIgnored()
{
var fs = new MockFileSystem();
fs.AddFile("./myfile.txt", new MockFileData("blabla"));
fs.AddFile("./myfile.txt.old.tmp", new MockFileData("blabla"));
fs.AddFile("./someOtherFile.txt.old.tmp", new MockFileData("blabla"));

var cache = new CacheStorage(fs);
var manager = CreateCacheManager(fs, cache);

await manager.UpdateCacheAsync();

Assert.Single((await cache.LoadAsync()).Entries);
}

[Fact]
public async Task CorruptCacheFileGetsRestored()
{
var fs = new MockFileSystem();
var cache = new CacheStorage(fs);

fs.AddFile(cache.FileInfo.FullName, new MockFileData("blabla")); // not valid json

var manager = CreateCacheManager(fs, cache);

await manager.UpdateCacheAsync();
}

private CacheManager CreateCacheManager(IFileSystem fs, ICacheStorage storage)
=> new CacheManager(fs, storage, CreateLogger<CacheManager>());
}
}
44 changes: 44 additions & 0 deletions UpdateLib.Tests/Core/Common/IO/DirectoryEntryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UpdateLib.Core.Common.IO;
using Xunit;

namespace UpdateLib.Tests.Core.Common.IO
{
public class DirectoryEntryTests
{
[Fact]
public void CountReturnsCorrectAmountOfFiles()
{
var root = CreateDirectoryWithFiles("1", 2);
var dir2 = CreateDirectoryWithFiles("2", 0);
var dir3 = CreateDirectoryWithFiles("3", 1);
var dir4 = CreateDirectoryWithFiles("4", 4);
var dir5 = CreateDirectoryWithFiles("5", 2);

root.Add(dir2);
root.Add(dir3);
dir2.Add(dir4);
dir3.Add(dir5);

Assert.Equal(9, root.Count);
Assert.Equal(9, root.GetItems().Count());
}

private DirectoryEntry CreateDirectoryWithFiles(string name, int filesToCreate)
{
var dir = new DirectoryEntry(name);

for (int i = 0; i < filesToCreate; i++)
{
var entry = new FileEntry($"{name}.{i.ToString()}");

dir.Add(entry);
}

return dir;
}
}
}
24 changes: 24 additions & 0 deletions UpdateLib.Tests/Core/Common/IO/FileEntryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Xunit;
using UpdateLib.Core.Common.IO;

namespace UpdateLib.Tests.Core.Common.IO
{
public class FileEntryTests
{
[Fact]
public void ShouldGiveCorrectSourceAndDestination()
{
DirectoryEntry root = new DirectoryEntry("%root%");
DirectoryEntry subFolder = new DirectoryEntry("sub");
FileEntry file = new FileEntry("myfile.txt");

root.Add(subFolder);

subFolder.Add(file);

string outputDest = "%root%\\sub\\myfile.txt";

Assert.Equal(outputDest, file.Path);
}
}
}
23 changes: 23 additions & 0 deletions UpdateLib.Tests/Core/Common/UpdateInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using UpdateLib.Core;
using UpdateLib.Core.Common;
using Xunit;

namespace UpdateLib.Tests.Core.Common
{
public class UpdateInfoTests
{
[Theory]
[InlineData("1.0.0", "2.0.0")]
[InlineData("1.0.0", "1.0.0")]
public void BasedOnVersionHigherThenSelfVersionThrowsException(string currVersion, string baseVersion)
{
var basedOnVersion = new UpdateVersion(baseVersion);
var version = new UpdateVersion(currVersion);

Assert.Throws<ArgumentOutOfRangeException>(() => new UpdateInfo(version, basedOnVersion, "", ""));
}
}
}
35 changes: 35 additions & 0 deletions UpdateLib.Tests/Core/Storage/CacheStorageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using UpdateLib.Core.Storage;
using UpdateLib.Core.Storage.Files;
using Xunit;

namespace UpdateLib.Tests.Core.Storage
{
public class CacheStorageTests
{
[Fact]
public async Task CacheSaveAndLoadAreTheSame()
{
var mockFileSystem = new MockFileSystem();

var cache = new CacheStorage(mockFileSystem);
var file = new HashCacheFile();
var entry = new HashCacheEntry("name", DateTime.UtcNow.Ticks, "some hash");

file.Entries.Add(entry);

await cache.SaveAsync(file);

var loadedFile = await cache.LoadAsync();

var loadedEntry = loadedFile.Entries.First();

Assert.Equal(entry.FilePath, loadedEntry.FilePath);
Assert.Equal(entry.Hash, loadedEntry.Hash);
Assert.Equal(entry.Ticks, loadedEntry.Ticks);
}
}
}
37 changes: 37 additions & 0 deletions UpdateLib.Tests/Core/Storage/UpdateCatalogStorageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using UpdateLib.Core.Common;
using UpdateLib.Core.Storage;
using UpdateLib.Core.Storage.Files;
using Xunit;

namespace UpdateLib.Tests.Core.Storage
{
public class UpdateCatalogStorageTests
{
[Fact]
public async Task SaveAndLoadAreTheSame()
{
var mockFileSystem = new MockFileSystem();

var storage = new UpdateCatalogStorage(mockFileSystem);
var file = new UpdateCatalogFile();
var info = new UpdateInfo("1.0.0", null, "name", "hash");

file.Catalog.Add(info);

await storage.SaveAsync(file);

var loadedFile = await storage.LoadAsync();

var loadedEntry = loadedFile.Catalog.First();

Assert.Equal(info.FileName, loadedEntry.FileName);
Assert.Equal(info.Hash, loadedEntry.Hash);
Assert.Equal(info.IsPatch, loadedEntry.IsPatch);
Assert.Equal(info.Version, loadedEntry.Version);
Assert.Equal(info.BasedOnVersion, loadedEntry.BasedOnVersion);
}
}
}
39 changes: 39 additions & 0 deletions UpdateLib.Tests/Core/Storage/UpdateFileStorageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using UpdateLib.Core.Common.IO;
using UpdateLib.Core.Storage;
using UpdateLib.Core.Storage.Files;
using Xunit;

namespace UpdateLib.Tests.Core.Storage
{
public class UpdateFileStorageTests
{
[Fact]
public async Task SaveAndLoadAreTheSame()
{
var mockFileSystem = new MockFileSystem();

var storage = new UpdateFileStorage(mockFileSystem);
var file = new UpdateFile();

var dir = new DirectoryEntry("dir");
var fileEntry = new FileEntry("file.txt");

dir.Add(fileEntry);

file.Entries.Add(dir);

await storage.SaveAsync(file);

var loadedFile = await storage.LoadAsync();

var loadedEntry = loadedFile.Entries.First().Files.First();

Assert.Equal(fileEntry.Hash, loadedEntry.Hash);
Assert.Equal(fileEntry.Name, loadedEntry.Name);
Assert.Equal(fileEntry.Path, loadedEntry.Path);
}
}
}
Loading