Files
Nemera/tests/IntegrationTests/Nemera.Infrastructure.IntegrationTests/Core/Plugins/PluginLoadContextTests.cs
T
BinaryAlley b30cbd6b9e Adds bundled plugin trust and cross-platform native deployment
- embeds the append-only historical and deny trust lists in the API and marks matching bundled plugins as Official without blocking loading
- builds and records the current plugin manifest only for official release builds
- deploys the PDF native libraries for the supported Linux, macOS and Windows runtimes from the plugin dependency manifest
- records release hashes by appending only missing entries to master's manifest and rejects any diff that removes a line
- documents the release flow, the supported runtimes and the plugin installer limits
2026-11-28 18:00:00 +02:00

107 lines
3.8 KiB
C#

#region ========================================================================= USING =====================================================================================
using Nemera.Infrastructure.Core.Plugins;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
#endregion
namespace Nemera.Infrastructure.IntegrationTests.Core.Plugins;
/// <summary>
/// Contains integration tests for the <see cref="PluginLoadContext"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class PluginLoadContextTests
{
[Fact]
public void LoadFromAssemblyName_WhenAssemblyIsResolvableFromDefaultContext_ShouldReturnTheSharedAssembly()
{
// Arrange
using TemporaryPluginDirectory temporaryPluginDirectory = new();
PluginLoadContext sut = new(Path.Combine(temporaryPluginDirectory.Path, "Plugin.dll"), "test-context");
// Act
Assembly result = sut.LoadFromAssemblyName(typeof(object).Assembly.GetName());
// Assert
Assert.NotNull(result);
Assert.Equal(typeof(object).Assembly.GetName().Name, result.GetName().Name);
}
[Fact]
public void Load_WhenAssemblyExistsOnlyInPluginDirectory_ShouldLoadItFromThePluginDirectory()
{
// Arrange
using TemporaryPluginDirectory temporaryPluginDirectory = new();
string destinationPath = temporaryPluginDirectory.CopyDll(Path.Combine(AppContext.BaseDirectory, "Nemera.TestPlugin.dll"), "PluginLocalDependency.dll");
PluginLoadContext sut = new(destinationPath, "test-context");
MethodInfo loadMethod = typeof(PluginLoadContext).GetMethod("Load", BindingFlags.Instance | BindingFlags.NonPublic)!;
// Act
Assembly? result = (Assembly?)loadMethod.Invoke(sut, [new AssemblyName("PluginLocalDependency")]);
// Assert
Assert.NotNull(result);
Assert.Equal("Nemera.TestPlugin", result!.GetName().Name);
Assert.Equal(destinationPath, result.Location);
}
[Fact]
public void LoadFromAssemblyName_WhenAssemblyIsNotFoundAnywhere_ShouldThrowFileNotFoundException()
{
// Arrange
using TemporaryPluginDirectory temporaryPluginDirectory = new();
PluginLoadContext sut = new(Path.Combine(temporaryPluginDirectory.Path, "Missing.dll"), "test-context");
// Act
Action act = () => sut.LoadFromAssemblyName(new AssemblyName($"Definitely.Not.Resolvable.{Guid.NewGuid()}"));
// Assert
Assert.Throws<FileNotFoundException>(act);
}
/// <summary>
/// Test helper managing a temporary plugins directory, deleted when the test finishes.
/// </summary>
private sealed class TemporaryPluginDirectory : IDisposable
{
private bool _disposed;
public TemporaryPluginDirectory()
{
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"nemera-plugin-load-context-{Guid.NewGuid()}");
Directory.CreateDirectory(Path);
}
public string Path { get; }
public string CopyDll(string sourceDllPath, string destinationFileName)
{
string destinationPath = System.IO.Path.Combine(Path, destinationFileName);
File.Copy(sourceDllPath, destinationPath);
return destinationPath;
}
public void Dispose()
{
if (!_disposed)
{
try
{
Directory.Delete(Path, recursive: true);
}
catch (IOException)
{
// Best effort cleanup of the temporary plugins directory.
}
catch (UnauthorizedAccessException)
{
// The plugin assemblies are still locked by their load contexts, best effort cleanup only.
}
_disposed = true;
}
}
}
}