mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 00:11:10 +00:00
Adds tests for the book reading feature
This commit is contained in:
1 parent
f7ccdf3d1c
commit
3a3c0c047c
192 files changed
+14104
-342
No files matched your search
@@ -50,6 +50,24 @@ public class PluginLoaderTests
|
||||
Assert.Contains("invalid.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsNativeLibrary_ShouldSkipItSilently()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// A native library (like pdfium.dll) is a valid PE image starting with the DOS signature, but it is not a managed assembly.
|
||||
temporaryPluginDirectory.CreateFile("native.dll", "MZ\u0001\u0000\u0000\u0000garbage native library content");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Empty(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsAssemblyWithoutPluginDescriptors_ShouldReturnNoPluginsAndNoErrors()
|
||||
{
|
||||
@@ -85,6 +103,104 @@ public class PluginLoaderTests
|
||||
Assert.Contains(services, descriptor => descriptor.ImplementationInstance as string == "plugin-services-registered");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsValidPluginAssembly_ShouldRetainTheLoadContextOfThePlugin()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
// A collectible load context is only kept alive by a direct reference to the context object itself, so the context of every loaded plugin is retained
|
||||
// for the lifetime of the host, keeping the plugin assemblies from being unloaded by the garbage collector.
|
||||
Assert.Single(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsManagedDependencyAssemblyAlongsideAPlugin_ShouldSkipTheDependencyAndLoadOnlyThePlugin()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
// A plugin ships its managed dependencies into the plugins directory (like PdfPig.dll and SkiaSharp.dll next to the PDF plugin). A managed dependency is a valid
|
||||
// assembly that does not reference the plugin contracts, so it is not a plugin and must be skipped without reflecting over its types, which would otherwise surface
|
||||
// spurious load errors for a file that is not a plugin.
|
||||
temporaryPluginDirectory.CopyDll(GetDependencyAssemblyPath());
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
IPlugin loadedPlugin = Assert.Single(result.Plugins);
|
||||
Assert.Equal(Guid.Parse("7B5A3E5D-9B7F-4A1E-8D9C-2F3A4B5C6D7E"), loadedPlugin.Id);
|
||||
Assert.Equal("Test Plugin", loadedPlugin.Name);
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Single(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsFileShorterThanDosSignature_ShouldRecordInvalidAssemblyError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// A single-character file is not long enough to carry the two-byte DOS signature "MZ", so it is not a PE image.
|
||||
temporaryPluginDirectory.CreateFile("tiny.dll", "M");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("tiny", error.AssemblyName);
|
||||
Assert.Contains("tiny.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsFileWithWrongDosSignature_ShouldRecordInvalidAssemblyError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// The file starts with the wrong DOS signature, so it is not a PE image at all.
|
||||
temporaryPluginDirectory.CreateFile("notpe.dll", "XY\u0000\u0000\u0000some content that is not a PE image");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("notpe", error.AssemblyName);
|
||||
Assert.Contains("notpe.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsLockedNativeLibrary_ShouldNotThrowAndRecordError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string nativePath = temporaryPluginDirectory.CreateFileReturningPath("locked.dll", "MZ\u0000\u0000\u0000locked native library content");
|
||||
using FileStream lockStream = new(nativePath, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
// The file cannot be read to verify its PE signature, so it is reported as not loadable rather than crashing the loader.
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("locked", error.AssemblyName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of the <c>Lumina.TestPlugin</c> assembly from the test output directory.
|
||||
/// The assembly is referenced by the test project but no type from it is used, so it is not loaded
|
||||
@@ -96,6 +212,18 @@ public class PluginLoaderTests
|
||||
return Path.Combine(AppContext.BaseDirectory, "Lumina.TestPlugin.dll");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of a managed dependency assembly from the test output directory, standing in for the managed dependencies
|
||||
/// a plugin ships next to itself (like PdfPig.dll and SkiaSharp.dll next to the PDF plugin). The <c>Microsoft.AspNetCore.SignalR.Core</c>
|
||||
/// assembly is used because it does not reference the plugin contracts assembly, and reflecting over its types inside a plugin load
|
||||
/// context throws, which reproduces the spurious load errors the loader used to record for dependency assemblies.
|
||||
/// </summary>
|
||||
/// <returns>The path of the managed dependency assembly.</returns>
|
||||
private static string GetDependencyAssemblyPath()
|
||||
{
|
||||
return Path.Combine(AppContext.BaseDirectory, "Microsoft.AspNetCore.SignalR.Core.dll");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test helper managing a temporary plugins directory, deleted when the test finishes.
|
||||
/// </summary>
|
||||
@@ -116,6 +244,13 @@ public class PluginLoaderTests
|
||||
File.WriteAllText(System.IO.Path.Combine(Path, fileName), content);
|
||||
}
|
||||
|
||||
public string CreateFileReturningPath(string fileName, string content)
|
||||
{
|
||||
string filePath = System.IO.Path.Combine(Path, fileName);
|
||||
File.WriteAllText(filePath, content);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void CopyDll(string sourceDllPath)
|
||||
{
|
||||
File.Copy(sourceDllPath, System.IO.Path.Combine(Path, System.IO.Path.GetFileName(sourceDllPath)));
|
||||
|
||||
Reference in new issue
Block a user