< 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
 46119    public FileMatchAssemblyLoader(
 46120        IReadOnlyList<string> directories,
 46121        Predicate<string>? fileFilter)
 22    {
 46123        ArgumentNullException.ThrowIfNull(directories);
 46024        ArgumentNullException.ThrowIfNull(fileFilter);
 25
 45926        _directories = directories;
 45927        _fileFilter = fileFilter;
 45928    }
 29
 30    /// <inheritdoc />
 31    public IReadOnlyList<Assembly> LoadAssemblies(
 32        bool continueOnAssemblyError)
 33    {
 37634        var sourceAssemblies = _directories
 37635            .SelectMany(Directory.GetFiles)
 37636            .Where(x =>
 37637            {
 2323338                var fileName = Path.GetFileName(x);
 2323339                return _fileFilter.Invoke(fileName);
 37640            })
 37641            .Select(p =>
 37642            {
 37643                try
 37644                {
 70745                    return Assembly.LoadFrom(p);
 37646                }
 147                catch (Exception ex)
 37648                {
 149                    if (continueOnAssemblyError)
 37650                    {
 051                        return null;
 37652                    }
 37653                    else
 37654                    {
 155                        throw new InvalidOperationException($"Failed to load assembly from path: {p}", ex);
 37656                    }
 37657                }
 70658            })
 70659            .Where(x => x != null)
 37660            .Cast<Assembly>()
 37661            .ToArray();
 37462        return sourceAssemblies;
 63    }
 64}