< Summary - Kestrun — Combined Coverage

Information
Class: Private.Assembly.Assert-KrAssemblyLoaded
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Assert-KrAssemblyLoaded.ps1
Tag: Kestrun/Kestrun@eeafbe813231ed23417e7b339e170e307b2c86f9
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 59
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/12/2025 - 13:32:05 Line coverage: 46.1% (6/13) Total lines: 41 Tag: Kestrun/Kestrun@63ea5841fe73fd164406accba17a956e8c08357f10/13/2025 - 16:52:37 Line coverage: 0% (0/13) Total lines: 41 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/27/2025 - 17:48:01 Line coverage: 0% (0/14) Total lines: 42 Tag: Kestrun/Kestrun@43d8ece4bc8573b7948ce1cea478a13ad32652d012/21/2025 - 19:25:34 Line coverage: 0% (0/19) Total lines: 59 Tag: Kestrun/Kestrun@63eee3e6ff7662a7eb5bb3603d667daccb809f2d01/21/2026 - 17:07:46 Line coverage: 63.1% (12/19) Total lines: 59 Tag: Kestrun/Kestrun@3f6f61710c7ef7d5953cab578fe699c1e5e01a3602/05/2026 - 00:28:18 Line coverage: 0% (0/19) Total lines: 59 Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589

Coverage delta

Coverage delta 64 -64

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Assert-KrAssemblyLoaded.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Ensures that a .NET assembly is loaded only once.
 4
 5    .DESCRIPTION
 6        Checks the currently loaded assemblies for the specified path. If the
 7        assembly has not been loaded yet, it is added to the current AppDomain.
 8    .PARAMETER AssemblyPath
 9        Path to the assembly file to load.
 10    .PARAMETER LoadContext
 11        The AssemblyLoadContext to load the assembly into. Default is the Default context.
 12        This helps avoid issues with multiple contexts loading the same assembly.
 13    .OUTPUTS
 14        [bool] - True if the assembly is loaded successfully or already loaded, false otherwise.
 15#>
 16function Assert-KrAssemblyLoaded {
 17    [CmdletBinding()]
 18    [OutputType([bool])]
 19    param(
 20        [Parameter(Mandatory)]
 21        [string]$AssemblyPath,
 22
 23        # Default = require it to be loadable from the Default ALC (fixes VSCode/PSES split-load issue)
 24        [System.Runtime.Loader.AssemblyLoadContext]$LoadContext = [System.Runtime.Loader.AssemblyLoadContext]::Default
 25    )
 26
 027    if (-not (Test-Path -LiteralPath $AssemblyPath -PathType Leaf)) {
 028        throw "Assembly not found at path: $AssemblyPath"
 29    }
 30
 031    $full = (Resolve-Path -LiteralPath $AssemblyPath).Path
 032    $asmName = [System.Reflection.AssemblyName]::GetAssemblyName($full)
 33
 34    # Find an already-loaded assembly with same simple name *in the desired ALC*
 035    $loadedInContext = [AppDomain]::CurrentDomain.GetAssemblies() |
 036        Where-Object {
 037            $_.GetName().Name -eq $asmName.Name -and
 038            ([System.Runtime.Loader.AssemblyLoadContext]::GetLoadContext($_) -eq $LoadContext)
 39        } |
 040        Select-Object -First 1
 41
 042    if ($loadedInContext) {
 043        Write-Verbose ('Assembly already loaded in {0} ALC: {1} {2} from {3}' -f `
 044            ($LoadContext.Name ?? 'Default'), $loadedInContext.GetName().Name, $loadedInContext.GetName().Version, $load
 045        return $true
 46    }
 47
 048    Write-Verbose ('Loading assembly into {0} ALC: {1}' -f ($LoadContext.Name ?? 'Default'), $full)
 49
 50    try {
 51        # Load into the requested context (Default by default)
 052        [void]$LoadContext.LoadFromAssemblyPath($full)
 053        return $true
 54    } catch {
 055        Write-Error "Failed to load assembly: $full"
 056        Write-Error $_
 057        return $false
 58    }
 59}

Methods/Properties

Assert-KrAssemblyLoaded()