< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Languages.PowerShellExecutionHelpers
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Languages/PowerShellExecutionHelpers.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
76%
Covered lines: 16
Uncovered lines: 5
Coverable lines: 21
Total lines: 64
Line coverage: 76.1%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
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: 52.3% (11/21) Branch coverage: 33.3% (4/12) Total lines: 64 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/15/2025 - 01:01:18 Line coverage: 76.1% (16/21) Branch coverage: 75% (9/12) Total lines: 64 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc5840 10/13/2025 - 16:52:37 Line coverage: 52.3% (11/21) Branch coverage: 33.3% (4/12) Total lines: 64 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/15/2025 - 01:01:18 Line coverage: 76.1% (16/21) Branch coverage: 75% (9/12) Total lines: 64 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc5840

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetVariables(...)87.5%8887.5%
AddScript(...)100%11100%
InvokeAsync()50%4477.77%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Languages/PowerShellExecutionHelpers.cs

#LineLine coverage
 1using System.Management.Automation;
 2using Serilog.Events;
 3
 4namespace Kestrun.Languages;
 5
 6/// <summary>
 7/// Shared helper methods for configuring and invoking PowerShell scripts in a consistent manner.
 8/// Extracted to reduce duplication between <see cref="PowerShellDelegateBuilder"/> and script-based probes.
 9/// </summary>
 10internal static class PowerShellExecutionHelpers
 11{
 12    /// <summary>
 13    /// Sets variables in the PowerShell runspace from the provided argument dictionary.
 14    /// Existing variables are overwritten. Null or empty collections are ignored.
 15    /// </summary>
 16    internal static void SetVariables(PowerShell ps, IReadOnlyDictionary<string, object?>? arguments, Serilog.ILogger lo
 17    {
 818        if (arguments is null || arguments.Count == 0)
 19        {
 420            return;
 21        }
 22
 423        if (log.IsEnabled(LogEventLevel.Verbose))
 24        {
 025            log.Verbose("Setting PowerShell variables from arguments: {Count}", arguments.Count);
 26        }
 427        var proxy = ps.Runspace!.SessionStateProxy;
 2428        foreach (var kv in arguments)
 29        {
 830            proxy.SetVariable(kv.Key, kv.Value);
 31        }
 432    }
 33
 34    /// <summary>
 35    /// Adds a script block to the pipeline.
 36    /// </summary>
 437    internal static void AddScript(PowerShell ps, string code) => _ = ps.AddScript(code);
 38
 39    /// <summary>
 40    /// Invokes the configured PowerShell pipeline asynchronously with cooperative cancellation.
 41    /// Cancellation attempts to stop the pipeline gracefully.
 42    /// </summary>
 43    internal static async Task<PSDataCollection<PSObject>> InvokeAsync(PowerShell ps, Serilog.ILogger log, CancellationT
 44    {
 445        if (log.IsEnabled(LogEventLevel.Verbose))
 46        {
 047            log.Verbose("Executing PowerShell script...");
 48        }
 49
 450        using var registration = ct.Register(static state =>
 451        {
 052            var shell = (PowerShell)state!;
 053            try { shell.Stop(); } catch { /* ignored */ }
 454        }, ps);
 55
 456        var results = await ps.InvokeAsync().ConfigureAwait(false);
 57
 458        if (log.IsEnabled(LogEventLevel.Verbose))
 59        {
 060            log.Verbose("PowerShell script executed with {Count} results.", results.Count);
 61        }
 462        return results;
 463    }
 64}