< Summary

Information
Class: NexusLabs.Needlr.Injection.Reflection.Loaders.FileMatchAssemblyLoader
Assembly: NexusLabs.Needlr.Injection.Reflection
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.Reflection/Loaders/FileMatchAssemblyLoader.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
LoadAssemblies(...)100%22100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.Reflection/Loaders/FileMatchAssemblyLoader.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Reflection;
 3
 4namespace NexusLabs.Needlr.Injection.Reflection.Loaders;
 5
 6/// <summary>
 7/// Assembly loader that loads assemblies from disk based on file name matching.
 8/// </summary>
 9/// <remarks>
 10/// This loader is not compatible with NativeAOT or trimming. For AOT scenarios,
 11/// use GeneratedAssemblyProvider from NexusLabs.Needlr.Injection.SourceGen instead.
 12/// </remarks>
 13[RequiresUnreferencedCode("FileMatchAssemblyLoader uses Assembly.LoadFrom which is not AOT-compatible. Use GeneratedAsse
 14public sealed class FileMatchAssemblyLoader : IAssemblyLoader
 15{
 16    private readonly IReadOnlyList<string> _directories;
 17    private readonly Predicate<string> _fileFilter;
 18
 63919    public FileMatchAssemblyLoader(
 63920        IReadOnlyList<string> directories,
 63921        Predicate<string>? fileFilter)
 22    {
 63923        ArgumentNullException.ThrowIfNull(directories);
 63824        ArgumentNullException.ThrowIfNull(fileFilter);
 25
 63726        _directories = directories;
 63727        _fileFilter = fileFilter;
 63728    }
 29
 30    /// <inheritdoc />
 31    public IReadOnlyList<Assembly> LoadAssemblies(
 32        bool continueOnAssemblyError)
 33    {
 54834        var sourceAssemblies = _directories
 54835            .SelectMany(Directory.GetFiles)
 54836            .Where(x =>
 54837            {
 4211338                var fileName = Path.GetFileName(x);
 4211339                return _fileFilter.Invoke(fileName);
 54840            })
 54841            .Select(p =>
 54842            {
 54843                try
 54844                {
 89145                    return Assembly.LoadFrom(p);
 54846                }
 347                catch (Exception ex)
 54848                {
 349                    if (continueOnAssemblyError)
 54850                    {
 251                        return null;
 54852                    }
 54853                    else
 54854                    {
 155                        throw new InvalidOperationException($"Failed to load assembly from path: {p}", ex);
 54856                    }
 54857                }
 89058            })
 89059            .Where(x => x != null)
 54860            .Cast<Assembly>()
 54861            .ToArray();
 54662        return sourceAssemblies;
 63    }
 64}