< 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@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
63%
Covered lines: 12
Uncovered lines: 7
Coverable lines: 19
Total lines: 59
Line coverage: 63.1%
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/08/2025 - 20:34:03 Line coverage: 46.1% (6/13) Total lines: 41 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7210/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@3f6f61710c7ef7d5953cab578fe699c1e5e01a36

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
 227    if (-not (Test-Path -LiteralPath $AssemblyPath -PathType Leaf)) {
 028        throw "Assembly not found at path: $AssemblyPath"
 29    }
 30
 231    $full = (Resolve-Path -LiteralPath $AssemblyPath).Path
 132    $asmName = [System.Reflection.AssemblyName]::GetAssemblyName($full)
 33
 34    # Find an already-loaded assembly with same simple name *in the desired ALC*
 135    $loadedInContext = [AppDomain]::CurrentDomain.GetAssemblies() |
 136        Where-Object {
 137            $_.GetName().Name -eq $asmName.Name -and
 138            ([System.Runtime.Loader.AssemblyLoadContext]::GetLoadContext($_) -eq $LoadContext)
 39        } |
 140        Select-Object -First 1
 41
 142    if ($loadedInContext) {
 243        Write-Verbose ('Assembly already loaded in {0} ALC: {1} {2} from {3}' -f `
 144            ($LoadContext.Name ?? 'Default'), $loadedInContext.GetName().Name, $loadedInContext.GetName().Version, $load
 145        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()