< Summary - Kestrun — Combined Coverage

Information
Class: Public.Helper.Resolve-KrPath
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Helper/Resolve-KrPath.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
77%
Covered lines: 14
Uncovered lines: 4
Coverable lines: 18
Total lines: 84
Line coverage: 77.7%
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

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Helper/Resolve-KrPath.ps1

#LineLine coverage
 1
 2<#
 3    .SYNOPSIS
 4        Resolves a file path relative to the Kestrun root or a specified base path.
 5    .DESCRIPTION
 6        This function expands environment variables and resolves the provided path against the Kestrun root or a specifi
 7        If the path is relative, it combines it with the base path. If the -Test switch is used, it checks if the resolv
 8    .PARAMETER Path
 9        The path to resolve. This can be an absolute path or a relative path.
 10    .PARAMETER RelativeBasePath
 11        An optional base path to resolve the relative path against. If not specified, the current directory is used.
 12    .PARAMETER KestrunRoot
 13        If specified, the Kestrun root directory is used as the base path for resolving the relative path.
 14    .PARAMETER Test
 15        If specified, the function will check if the resolved path exists. If it does not, the original input path is re
 16    .EXAMPLE
 17        Resolve-KrPath -Path "~/Documents/file.txt" -KestrunRoot
 18        Resolves the path "~/Documents/file.txt" relative to the Kestrun root directory, expanding any environment varia
 19    .EXAMPLE
 20        Resolve-KrPath -Path "file.txt" -RelativeBasePath "C:\Base\Path"
 21        Resolves the path "file.txt" relative to "C:\Base\Path", expanding any environment variables.
 22    .NOTES
 23        This function is designed to be used in the context of a Kestrun server to resolve file paths correctly.
 24#>
 25function Resolve-KrPath {
 26    [CmdletBinding()]
 27    [KestrunRuntimeApi('Everywhere')]
 28    [OutputType([string])]
 29    param(
 30        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline)]
 31        [string] $Path,
 32
 33        [Parameter(parameterSetName = 'RelativeBasePath')]
 34        [string] $RelativeBasePath,
 35
 36        [Parameter(parameterSetName = 'KestrunRoot')]
 37        [switch] $KestrunRoot,
 38
 39        [Parameter()]
 40        [switch] $Test
 41    )
 42    process {
 43        # --- 1. Expand ~/env in both Path and, if supplied, RelativeBasePath ---
 144        $expand = {
 45            param($p)
 146            if ($p -like '~*') {
 047                $p = $p -replace '^~', $HOME
 48            }
 149            [Environment]::ExpandEnvironmentVariables($p)
 50        }
 51
 152        $p3 = & $expand $Path
 53
 154        if ($KestrunRoot) {
 55            # Use the Kestrun root as base
 156            $RelativeBasePath = [Kestrun.KestrunHostManager]::KestrunRoot
 57        }
 58
 159        if ($RelativeBasePath) {
 60            # Expand + normalize the base, even if it doesn't exist
 161            $base3 = & $expand $RelativeBasePath
 162            $baseFull = [IO.Path]::GetFullPath($base3)
 63
 64            # If $Path is rooted, ignore the base; else combine
 165            if ([IO.Path]::IsPathRooted($p3)) {
 066                $full = [IO.Path]::GetFullPath($p3)
 67            } else {
 168                $combined = [IO.Path]::Combine($baseFull, $p3)
 169                $full = [IO.Path]::GetFullPath($combined)
 70            }
 71        } else {
 72            # No base supplied: just make absolute against current directory
 073            $full = [IO.Path]::GetFullPath($p3)
 74        }
 75
 76        # --- 4. If -Test was used and file doesn't exist, return the original input Path ---
 277        if ($Test -and -not (Test-Path $full)) {
 078            return $Path
 79        }
 80
 181        return $full
 82    }
 83}
 84

Methods/Properties

Resolve-KrPath()