< 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
97%
Covered lines: 36
Uncovered lines: 1
Coverable lines: 37
Total lines: 64
Line coverage: 97.2%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
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(...)50%2296.55%

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
 62219    public FileMatchAssemblyLoader(
 62220        IReadOnlyList<string> directories,
 62221        Predicate<string>? fileFilter)
 22    {
 62223        ArgumentNullException.ThrowIfNull(directories);
 62124        ArgumentNullException.ThrowIfNull(fileFilter);
 25
 62026        _directories = directories;
 62027        _fileFilter = fileFilter;
 62028    }
 29
 30    /// <inheritdoc />
 31    public IReadOnlyList<Assembly> LoadAssemblies(
 32        bool continueOnAssemblyError)
 33    {
 53134        var sourceAssemblies = _directories
 53135            .SelectMany(Directory.GetFiles)
 53136            .Where(x =>
 53137            {
 3980438                var fileName = Path.GetFileName(x);
 3980439                return _fileFilter.Invoke(fileName);
 53140            })
 53141            .Select(p =>
 53142            {
 53143                try
 53144                {
 84745                    return Assembly.LoadFrom(p);
 53146                }
 147                catch (Exception ex)
 53148                {
 149                    if (continueOnAssemblyError)
 53150                    {
 051                        return null;
 53152                    }
 53153                    else
 53154                    {
 155                        throw new InvalidOperationException($"Failed to load assembly from path: {p}", ex);
 53156                    }
 53157                }
 84658            })
 84659            .Where(x => x != null)
 53160            .Cast<Assembly>()
 53161            .ToArray();
 52962        return sourceAssemblies;
 63    }
 64}