< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Health.ScriptProbeFactory
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Health/ScriptProbeFactory.cs
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 177
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/13/2025 - 16:52:37 Line coverage: 0% (0/44) Branch coverage: 0% (0/20) Total lines: 174 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e11/14/2025 - 12:29:34 Line coverage: 0% (0/47) Branch coverage: 0% (0/20) Total lines: 177 Tag: Kestrun/Kestrun@5e12b09a6838e68e704cd3dc975331b9e680a626 10/13/2025 - 16:52:37 Line coverage: 0% (0/44) Branch coverage: 0% (0/20) Total lines: 174 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e11/14/2025 - 12:29:34 Line coverage: 0% (0/47) Branch coverage: 0% (0/20) Total lines: 177 Tag: Kestrun/Kestrun@5e12b09a6838e68e704cd3dc975331b9e680a626

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Create(...)0%7280%
CreatePowerShellProbe(...)100%210%
CreateCSharpProbe(...)100%210%
CreateVbProbe(...)100%210%
BuildCSharpRunner(...)0%156120%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Health/ScriptProbeFactory.cs

#LineLine coverage
 1using System.Reflection;
 2using Kestrun.Hosting;
 3using Kestrun.Languages;
 4using Kestrun.Scripting;
 5using Microsoft.CodeAnalysis;
 6using Microsoft.CodeAnalysis.CSharp.Scripting;
 7using Microsoft.CodeAnalysis.Scripting;
 8using Microsoft.CodeAnalysis.VisualBasic;
 9using KestrunCompilationErrorException = Kestrun.Scripting.CompilationErrorException;
 10using RoslynCompilationErrorException = Microsoft.CodeAnalysis.Scripting.CompilationErrorException;
 11
 12namespace Kestrun.Health;
 13
 14/// <summary>
 15/// Creates <see cref="IProbe"/> implementations backed by dynamic scripts.
 16/// </summary>
 17internal static class ScriptProbeFactory
 18{
 19    internal static IProbe Create(
 20        KestrunHost host,
 21        string name,
 22        IEnumerable<string>? tags,
 23        ScriptLanguage language,
 24        string code,
 25        Func<KestrunRunspacePoolManager>? runspaceAccessor,
 26        IReadOnlyDictionary<string, object?>? arguments,
 27        string[]? extraImports,
 28        Assembly[]? extraRefs)
 29    {
 030        ArgumentNullException.ThrowIfNull(host);
 031        ArgumentException.ThrowIfNullOrWhiteSpace(code);
 32
 033        return language switch
 034        {
 035            ScriptLanguage.PowerShell => CreatePowerShellProbe(host: host, name: name, tags: tags, code: code, runspaceA
 036            ScriptLanguage.CSharp => CreateCSharpProbe(host: host, name: name, tags: tags, code: code, arguments: argume
 037            ScriptLanguage.VBNet => CreateVbProbe(host: host, name: name, tags: tags, code: code, arguments: arguments, 
 038            ScriptLanguage.Native => throw new NotSupportedException("Use AddProbe(Func<...>) for native probes."),
 039            ScriptLanguage.FSharp => throw new NotImplementedException("F# health probes are not yet supported."),
 040            ScriptLanguage.Python => throw new NotImplementedException("Python health probes are not yet supported."),
 041            ScriptLanguage.JavaScript => throw new NotImplementedException("JavaScript health probes are not yet support
 042            _ => throw new ArgumentOutOfRangeException(nameof(language), language, null)
 043        };
 44    }
 45
 46    /// <summary>
 47    /// Builds and returns a PowerShell script probe.
 48    /// </summary>
 49    /// <param name="host">The Kestrun host instance.</param>
 50    /// <param name="name">The name of the probe.</param>
 51    /// <param name="tags">The tags associated with the probe.</param>
 52    /// <param name="code">The PowerShell code to execute.</param>
 53    /// <param name="runspaceAccessor">Accessor for the PowerShell runspace pool manager.</param>
 54    /// <param name="arguments">Arguments to pass to the script.</param>
 55    /// <returns>A new <see cref="IProbe"/> instance representing the PowerShell script probe.</returns>
 56    private static IProbe CreatePowerShellProbe(
 57        KestrunHost host,
 58        string name,
 59        IEnumerable<string>? tags,
 60        string code,
 61        Func<KestrunRunspacePoolManager>? runspaceAccessor,
 62        IReadOnlyDictionary<string, object?>? arguments)
 63    {
 064        ArgumentNullException.ThrowIfNull(host);
 065        ArgumentNullException.ThrowIfNull(runspaceAccessor);
 066        return new PowerShellScriptProbe(host: host, name: name, tags: tags,
 067            script: code, poolAccessor: runspaceAccessor, arguments: arguments);
 68    }
 69
 70    /// <summary>
 71    /// Builds and returns a C# script probe.
 72    /// </summary>
 73    /// <param name="host">The Kestrun host instance.</param>
 74    /// <param name="name">The name of the probe.</param>
 75    /// <param name="tags">The tags associated with the probe.</param>
 76    /// <param name="code">The C# code to compile.</param>
 77    /// <param name="arguments">The arguments for the script.</param>
 78    /// <param name="extraImports">Additional namespaces to import.</param>
 79    /// <param name="extraRefs">Additional assemblies to reference.</param>
 80    /// <returns>A new <see cref="IProbe"/> instance representing the C# script probe.</returns>
 81    private static IProbe CreateCSharpProbe(
 82        KestrunHost host,
 83        string name,
 84        IEnumerable<string>? tags,
 85        string code,
 86        IReadOnlyDictionary<string, object?>? arguments,
 87        string[]? extraImports,
 88        Assembly[]? extraRefs)
 89    {
 90        try
 91        {
 092            ArgumentNullException.ThrowIfNull(host);
 093            var runner = BuildCSharpRunner(code, extraImports, extraRefs);
 094            return new CSharpScriptProbe(host: host, name: name, tags: tags, runner: runner, locals: arguments);
 95        }
 096        catch (RoslynCompilationErrorException ex)
 97        {
 098            host.Logger.Error(ex, "Failed to compile C# health probe {Probe}.", name);
 099            throw;
 100        }
 0101    }
 102
 103    /// <summary>
 104    /// Builds and returns a VB.NET script probe.
 105    /// </summary>
 106    /// <param name="host">The Kestrun host instance.</param>
 107    /// <param name="name">The name of the probe.</param>
 108    /// <param name="tags">The tags associated with the probe.</param>
 109    /// <param name="code">The VB.NET code to compile.</param>
 110    /// <param name="arguments">The arguments for the script.</param>
 111    /// <param name="extraImports">Additional namespaces to import.</param>
 112    /// <param name="extraRefs">Additional assemblies to reference.</param>
 113    /// <returns>A new <see cref="IProbe"/> instance representing the VB.NET script probe.</returns>
 114    /// <remarks>
 115    /// This method compiles the provided VB.NET code into a script runner that can be executed asynchronously.
 116    /// It allows for additional namespaces and assemblies to be included in the compilation context.
 117    /// </remarks>
 118    private static IProbe CreateVbProbe(
 119        KestrunHost host,
 120        string name,
 121        IEnumerable<string>? tags,
 122        string code,
 123        IReadOnlyDictionary<string, object?>? arguments,
 124        string[]? extraImports,
 125        Assembly[]? extraRefs)
 126    {
 127        try
 128        {
 0129            var runner = VBNetDelegateBuilder.Compile<ProbeResult>(host: host, code: code, extraImports: extraImports, e
 0130            return new VbScriptProbe(host: host, name: name, tags: tags, runner: runner, locals: arguments);
 131        }
 0132        catch (KestrunCompilationErrorException ex)
 133        {
 0134            host.Logger.Error(ex, "Failed to compile VB.NET health probe {Probe}.", name);
 0135            throw;
 136        }
 0137    }
 138
 139    /// <summary>
 140    /// Builds a C# script runner for the given code, imports, and references.
 141    /// </summary>
 142    /// <param name="code">The C# code to compile.</param>
 143    /// <param name="extraImports">Additional namespaces to import.</param>
 144    /// <param name="extraRefs">Additional assemblies to reference.</param>
 145    /// <returns>A script runner that can execute the compiled code.</returns>
 146    /// <remarks>
 147    /// This method compiles the provided C# code into a script runner that can be executed asynchronously.
 148    /// It allows for additional namespaces and assemblies to be included in the compilation context.
 149    /// </remarks>
 150    private static ScriptRunner<ProbeResult> BuildCSharpRunner(string code, string[]? extraImports, Assembly[]? extraRef
 151    {
 0152        var options = ScriptOptions.Default
 0153            .AddReferences(DelegateBuilder.BuildBaselineReferences())
 0154            .AddReferences(typeof(ProbeResult).Assembly, typeof(ScriptProbeFactory).Assembly)
 0155            .WithImports(DelegateBuilder.PlatformImports)
 0156            .AddImports("Kestrun", "Kestrun.Health", "Kestrun.SharedState");
 157
 0158        if (extraImports is { Length: > 0 })
 159        {
 0160            options = options.WithImports(options.Imports.Concat(extraImports).Distinct(StringComparer.Ordinal));
 161        }
 162
 0163        if (extraRefs is { Length: > 0 })
 164        {
 0165            var additional = extraRefs
 0166                .Where(static r => !string.IsNullOrEmpty(r.Location) && File.Exists(r.Location))
 0167                .Select(static r => MetadataReference.CreateFromFile(r.Location));
 0168            options = options.AddReferences(additional);
 169        }
 170
 0171        var script = CSharpScript.Create<ProbeResult>(code, options, typeof(CsGlobals));
 0172        var diagnostics = script.Compile();
 0173        return diagnostics.Any(static d => d.Severity == DiagnosticSeverity.Error)
 0174            ? throw new RoslynCompilationErrorException("C# health probe compilation failed.", diagnostics)
 0175            : script.CreateDelegate();
 176    }
 177}