< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
83%
Covered lines: 20
Uncovered lines: 4
Coverable lines: 24
Total lines: 99
Line coverage: 83.3%
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 08/26/2025 - 01:25:22 Line coverage: 77.7% (14/18) Total lines: 83 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 22:37:32 Line coverage: 77.7% (14/18) Total lines: 84 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec610/13/2025 - 16:52:37 Line coverage: 83.3% (20/24) Total lines: 99 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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    [KestrunRuntimeApi('Everywhere')]
 27    [CmdletBinding(DefaultParameterSetName = 'RelativeBasePath')]
 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
 164            if ([IO.Path]::IsPathRooted($p3)) {
 065                $full = [IO.Path]::GetFullPath($p3)
 66            } else {
 67                # Combine and normalize WITHOUT requiring the target to exist
 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        # --- 3. Normalize directory separators to the current platform & remove '\.' segments ---
 177        if ([IO.Path]::DirectorySeparatorChar -eq '\\') {
 78            # Windows: favor backslashes
 079            $full = $full -replace '/', '\\'
 80        } else {
 81            # *nix: favor forward slashes
 182            $full = $full -replace '\\', '/'
 83        }
 84        # Remove any /./ or \./ style segments (keep UNC // at start intact)
 185        $full = $full -replace '([\\/])\.(?=[\\/])', '$1'
 86        # Collapse any accidental duplicate separators except a leading UNC //
 187        if ($full -notmatch '^[\\/]{2}[^\\/]') {
 188            $full = $full -replace '([\\/]){2,}', '$1'
 89        }
 90
 91        # --- 4. If -Test was used and file doesn't exist, return the original input Path ---
 292        if ($Test -and -not (Test-Path $full)) {
 193            return $Path
 94        }
 95
 196        return $full
 97    }
 98}
 99

Methods/Properties

Resolve-KrPath()