I am learning PowerShell
, and created the following script cmdlet
.
It reads one or more specified script files, to discover special functions (UnitTests, or functions with the Verb 'UnitTest' or 'DataTest'.
I designed the parameters to work like Get-Content
, as it reads content from files.
I would like general feedback on this cmdlet (design, workings), but i am also intrested in correctness.
<#
.SYNOPSIS
Gets UnitTests from a UnitTestFile.
.DESCRIPTION
UnitTests are PowerShell script functions, which are identified with the verb 'UnitTest' or 'DataTest'.
#>
function Get-UnitTest {
[CmdletBinding(DefaultParameterSetName = "Path")]
[OutputType([System.Management.Automation.FunctionInfo])]
Param(
<# Specifies a path to one or more UnitTestFiles. Wildcards are permitted. #>
[Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)]
[string[]] $Path,
<# Specifies a LiteralPath to one or more UnitTestFiles. Wildcards are permitted. #>
[Parameter(ParameterSetName = "LiteralPath", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias("FullName")]
[string[]] $LiteralPath,
<# Specifies the Verb to use to identify UnitTests. The default verbs are 'UnitTest' and 'DataTest'. This means that functions with a name like 'UnitTest-Get-ChildItem' or 'DataTest-Invoke-WebRequest' are found. #>
[String[]] $Verb = @( "UnitTest", "DataTest" )
)
Process {
foreach($curPath in ($LiteralPath + $Path | Where {$_} | Resolve-Path)) {
if(Test-Path $curPath -PathType Leaf) {
Write-Trace "Getting UnitTests from '$curPath'."
. $curPath
# find and return all unit tests (from the requested file)
Get-Command -Verb $Verb `
| Where { $_.CommandType -eq "Function" } `
| Where { ( $_.ScriptBlock.File ) -eq $curPath }
}
}
}
}
This cmdlet can be called as follows:
Get-UnitTest -Path .\somefile.ps1
Get-UnitTest -Path .\somefile.ps1 -Verb 'OtherTest'
Get-ChildItem -Recurse *.ps1 | Get-UnitTest