Skip to content

Instantly share code, notes, and snippets.

@gpduck
Created November 6, 2013 02:01
Show Gist options
  • Save gpduck/7329707 to your computer and use it in GitHub Desktop.
Save gpduck/7329707 to your computer and use it in GitHub Desktop.
Test functions for missing help and missing process blocks if they take pipeline input. Get-Command -Module Whatever | Test-Function
function Test-Function {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNull()]
[System.Management.Automation.FunctionInfo]$Function,
[switch]$IgnoreWarnings,
[switch]$IgnoreMissingHelp
)
process {
$LocationString = "{0}\{1}" -f $Function.ScriptBlock.Ast.Extent.File, $Function.Name
#region Test for help content
if(!$IgnoreMissingHelp) {
$Help = $Function.ScriptBlock.Ast.GetHelpContent()
if($Help) {
#Test for parameters in help
#Test for function($Param) style parameters
if($Function.ScriptBlock.Ast.Parameters.Count -gt 0) {
$Function.ScriptBlock.Ast.Parameters | %{
$ParamName = $_.Name.VariablePath
if(!$Help.Parameters.ContainsKey($ParamName.ToUpper())) {
if(!$IgnoreWarnings) {
Write-Warning "Missing help content for parameter $ParamName in $LocationString"
}
}
}
}
#Test for function { param( $param)} style parameters
if($Function.ScriptBlock.Ast.Body.ParamBlock.Parameters.Count -gt 0) {
$Function.ScriptBlock.Ast.Body.ParamBlock.Parameters | %{
$ParamName = $_.Name.VariablePath.UserPath
if(!$Help.Parameters.ContainsKey($ParamName.ToUpper())) {
if(!$IgnoreWarnings) {
Write-Warning "Missing help content for parameter $ParamName in $LocationString"
}
}
}
}
} else {
if(!$IgnoreWarnings) {
Write-Warning "Missing help content in $LocationString"
}
}
}
#EndRegion
#Test for missing process block in pipeline functions
if( (Test-HasPipelineParameters -Function $Function) -and $Function.ScriptBlock.Ast.Body.ProcessBlock -eq $null) {
if(!$IgnoreWarnings) {
Write-Warning "Possible missing process block in $LocationString"
}
}
}
}
function Test-HasPipelineParameters {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNull()]
[System.Management.Automation.FunctionInfo]$Function
)
process {
$HasPipelineParameters = $false
#Test function($Param) style parameters
if($Function.ScriptBlock.Ast.Parameters.Count -gt 0) {
foreach($Parameter in $Function.ScriptBlock.Ast.Parameters) {
$Parameter.Attributes | ?{
$_ -is [System.Management.Automation.Language.AttributeAst]
} | %{
#use foreach so we can break out of the loop
foreach($Argument in $_.NamedArguments) {
if($Argument.ArgumentName -in @("ValueFromPipelineByPropertyName","ValueFromPipeline") -and $Argument.Argument.VariablePath.UserPath -eq "true") {
$HasPipelineParameters = $true
break
}
}
}
if($HasPipelineParameters) {
break
}
}
}
#If no pipeline parameters have been found, test function { param( $param)} style parameters
if(!$HasPipelineParameters -and $Function.ScriptBlock.Ast.Body.ParamBlock.Parameters.Count -gt 0) {
#use foreach so we can break out of the loop
foreach($Parameter in $Function.ScriptBlock.Ast.Body.ParamBlock.Parameters) {
$Parameter.Attributes | ?{
$_ -is [System.Management.Automation.Language.AttributeAst]
} | %{
#use foreach so we can break out of the loop
foreach($Argument in $_.NamedArguments) {
if($Argument.ArgumentName -in @("ValueFromPipelineByPropertyName","ValueFromPipeline") -and $Argument.Argument.VariablePath.UserPath -eq "true") {
$HasPipelineParameters = $true
break
}
}
}
if($HasPipelineParameters) {
break
}
}
}
$HasPipelineParameters
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment