< Summary - Kestrun — Combined Coverage

Information
Class: Private.Assembly.Test-KrFunctionHasAttribute
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Test-KrFunctionHasAttribute.ps1
Tag: Kestrun/Kestrun@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 69
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 01/02/2026 - 00:16:25 Line coverage: 0% (0/21) Total lines: 69 Tag: Kestrun/Kestrun@8405dc23b786b9d436fba0d65fb80baa4171e1d0

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Test-KrFunctionHasAttribute.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Tests whether a given PowerShell function has a specified attribute applied.
 4.DESCRIPTION
 5    This function checks if the provided PowerShell function (CommandInfo) has an attribute that matches
 6    the specified attribute name regex. It first checks the runtime attributes applied to the function's
 7    ScriptBlock, and if not found, it parses the function's definition to look for attributes in the AST.
 8.PARAMETER Command
 9    The PowerShell function (CommandInfo) to check for the attribute.
 10.PARAMETER AttributeNameRegex
 11    A regex pattern to match the name of the attribute to look for.
 12.EXAMPLE
 13    PS> $cmd = Get-Command -Name 'MyFunction'
 14    PS> Test-KrFunctionHasAttribute -Command $cmd -AttributeNameRegex 'MyAttribute'
 15    Returns $true if 'MyFunction' has an attribute matching 'MyAttribute', otherwise $false.
 16.NOTES
 17    This function is part of the Kestrun PowerShell module.
 18#>
 19function Test-KrFunctionHasAttribute {
 20    [CmdletBinding()]
 21    [outputType([bool])]
 22    param(
 23        [Parameter(Mandatory = $true)]
 24        [System.Management.Automation.CommandInfo]$Command,
 25
 26        [Parameter(Mandatory = $true)]
 27        [ValidateNotNullOrEmpty()]
 28        [string]$AttributeNameRegex
 29    )
 30    try {
 031        $sb = $Command.ScriptBlock
 032        if (-not $sb) {
 033            return $false
 34        }
 35
 36        # Prefer runtime attributes: this is what PowerShell actually binds
 037        foreach ($a in @($sb.Attributes)) {
 038            $t = $a.GetType()
 039            if ($t.Name -match $AttributeNameRegex -or $t.FullName -match $AttributeNameRegex) {
 040                return $true
 41            }
 42        }
 43
 44        # Fallback: parse the definition and scan AttributeAst nodes
 045        $def = $Command.Definition
 046        if (-not $def) {
 047            return $false
 48        }
 49
 050        $tokens = $null
 051        $parseErrors = $null
 052        $ast = [System.Management.Automation.Language.Parser]::ParseInput($def, [ref]$tokens, [ref]$parseErrors)
 53
 054        if ($parseErrors -and $parseErrors.Count -gt 0) {
 055            return $false
 56        }
 57
 058        $found = $ast.FindAll({
 59                param($n)
 060                $n -is [System.Management.Automation.Language.AttributeAst] -and
 061                ($n.TypeName?.Name -match $AttributeNameRegex)
 62            }, $true)
 63
 064        return ($found.Count -gt 0)
 65    } catch {
 066        return $false
 67    }
 068    return $false
 69}

Methods/Properties

Test-KrFunctionHasAttribute()