< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.BuildError
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/BuildError.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
90%
Covered lines: 30
Uncovered lines: 3
Coverable lines: 33
Total lines: 81
Line coverage: 90.9%
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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Result(...)100%11100%
Text(...)100%11100%
append()75%141276.92%
ResponseAsync(...)100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/BuildError.cs

#LineLine coverage
 1using System.Management.Automation;
 2using System.Text;
 3using Serilog;
 4
 5namespace Kestrun.Utilities;
 6
 7/// <summary>
 8/// Utilities for formatting PowerShell error streams into HTTP responses.
 9/// </summary>
 10public static class BuildError
 11{
 12    /// <summary>
 13    /// Convert the current PowerShell error streams to a plain-text <see cref="IResult"/>.
 14    /// </summary>
 115    public static IResult Result(PowerShell ps) => Results.Text(content: Text(ps), statusCode: 500, contentType: "text/p
 16
 17
 18
 19    /// <summary>
 20    /// Collate all PowerShell streams (error, verbose, warning, etc.) into a single string.
 21    /// </summary>
 22    public static string Text(PowerShell ps)
 23    {
 624        ArgumentNullException.ThrowIfNull(ps);
 25
 1226        var errors = ps.Streams.Error.Select(e => e.ToString());
 627        var verbose = ps.Streams.Verbose.Select(v => v.ToString());
 628        var warnings = ps.Streams.Warning.Select(w => w.ToString());
 629        var debug = ps.Streams.Debug.Select(d => d.ToString());
 730        var info = ps.Streams.Information.Select(i => i.ToString());
 31        // Format the output
 32        // 500 + text body
 33
 634        var sb = new StringBuilder();
 35
 36        void append(string emoji, IEnumerable<string> lines)
 37        {
 3038            if (!lines.Any())
 39            {
 2340                return;
 41            }
 42
 743            _ = sb.AppendLine($"{emoji}[{emoji switch
 744            {
 645                "❌" => "Error",
 046                "💬" => "Verbose",
 047                "⚠️" => "Warning",
 048                "🐞" => "Debug",
 149                _ => "Info"
 750            }}]");
 2851            foreach (var l in lines)
 52            {
 753                _ = sb.AppendLine($"\t{l}");
 54            }
 755        }
 56
 657        append("❌", errors);
 658        append("💬", verbose);
 659        append("⚠️", warnings);
 660        append("🐞", debug);
 661        append("ℹ️", info);
 62
 663        var msg = sb.ToString();
 664        Log.Information(msg);
 65
 66
 667        return msg;
 68    }
 69
 70    // Helper that writes the error to the response stream
 71    /// <summary>
 72    /// Write the formatted PowerShell errors directly to the HTTP response.
 73    /// </summary>
 74    public static Task ResponseAsync(HttpContext context, PowerShell ps)
 75    {
 376        var errText = Text(ps);
 377        context.Response.StatusCode = 500;
 378        context.Response.ContentType = "text/plain; charset=utf-8";
 379        return context.Response.WriteAsync(errText);
 80    }
 81}