< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Runtime.EnvironmentHelper
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Runtime/EnvironmentHelper.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
62%
Covered lines: 10
Uncovered lines: 6
Coverable lines: 16
Total lines: 85
Line coverage: 62.5%
Branch coverage
78%
Covered branches: 11
Total branches: 14
Branch coverage: 78.5%
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: 12.5% (2/16) Branch coverage: 7.1% (1/14) Total lines: 85 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/17/2025 - 15:48:30 Line coverage: 62.5% (10/16) Branch coverage: 78.5% (11/14) Total lines: 85 Tag: Kestrun/Kestrun@b8199aff869a847b75e185d0527ba45e04a43d86 10/13/2025 - 16:52:37 Line coverage: 12.5% (2/16) Branch coverage: 7.1% (1/14) Total lines: 85 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/17/2025 - 15:48:30 Line coverage: 62.5% (10/16) Branch coverage: 78.5% (11/14) Total lines: 85 Tag: Kestrun/Kestrun@b8199aff869a847b75e185d0527ba45e04a43d86

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetHostEnvironment(...)100%210%
SetOverrideName(...)50%22100%
SetOverride(...)100%210%
ClearOverride()100%11100%
ResolveEnvironment()83.33%121287.5%
get_Name()100%11100%
IsDevelopment()100%210%
IsStaging()100%210%
IsProduction()100%210%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Runtime/EnvironmentHelper.cs

#LineLine coverage
 1namespace Kestrun.Runtime;
 2
 3/// <summary>
 4/// Helpers for determining the current environment name.
 5/// </summary>
 6public static class EnvironmentHelper
 7{
 8    private static Func<string?>? _overrideProvider;
 9    private static IHostEnvironment? _cachedEnv;
 10
 11    /// <summary>
 12    /// Set the host environment (usually from DI).
 13    /// </summary>
 14    /// <param name="env">The host environment.</param>
 015    public static void SetHostEnvironment(IHostEnvironment env) => _cachedEnv = env;
 16
 17    /// <summary>
 18    /// Set an explicit override for the environment name.
 19    /// </summary>
 20    /// <param name="name">The environment name to override with.</param>
 21    public static void SetOverrideName(string? name)
 3922        => _overrideProvider = string.IsNullOrWhiteSpace(name) ? null : () => name;
 23
 24    /// <summary>
 25    /// Set an explicit override provider for the environment name.
 26    /// </summary>
 27    /// <param name="provider">The provider function to retrieve the environment name.</param>
 028    public static void SetOverride(Func<string?> provider) => _overrideProvider = provider;
 29
 30    /// <summary>
 31    /// Clear any explicit override for the environment name.
 32    /// </summary>
 133    public static void ClearOverride() => _overrideProvider = null;
 34
 35    /// <summary>
 36    /// Determine the current environment name.
 37    /// </summary>
 38    /// <returns> The current environment name.</returns>
 39    private static string ResolveEnvironment()
 40    {
 41        // 1️⃣ Explicit override
 32342        var fromOverride = _overrideProvider?.Invoke();
 32343        if (!string.IsNullOrWhiteSpace(fromOverride))
 44        {
 3845            return fromOverride!;
 46        }
 47
 48        // 2️⃣ Cached host environment (from SetHostEnvironment)
 28549        if (!string.IsNullOrWhiteSpace(_cachedEnv?.EnvironmentName))
 50        {
 051            return _cachedEnv!.EnvironmentName;
 52        }
 53
 54        // 3️⃣ Standard environment variables (like Kestrel)
 28555        return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
 28556            ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")
 28557            ?? "Production";
 58    }
 59
 60    /// <summary>
 61    /// The current environment name.
 62    /// </summary>
 32363    public static string Name => ResolveEnvironment();
 64
 65    /// <summary>
 66    /// Is the current environment "Development"?
 67    /// </summary>
 68    /// <returns>True if the current environment is "Development"; otherwise, false.</returns>
 69    public static bool IsDevelopment()
 070        => string.Equals(Name, Environments.Development, StringComparison.OrdinalIgnoreCase);
 71
 72    /// <summary>
 73    /// Is the current environment "Staging"?
 74    /// </summary>
 75    /// <returns>True if the current environment is "Staging"; otherwise, false.</returns>
 76    public static bool IsStaging()
 077        => string.Equals(Name, Environments.Staging, StringComparison.OrdinalIgnoreCase);
 78
 79    /// <summary>
 80    /// Is the current environment "Production"?
 81    /// </summary>
 82    /// <returns>True if the current environment is "Production"; otherwise, false.</returns>
 83    public static bool IsProduction()
 084        => string.Equals(Name, Environments.Production, StringComparison.OrdinalIgnoreCase);
 85}