1- # Some examples are from Josh Burkard, thanks!
2- # https://www.burkard.it/2019/08/pester-tests-for-powershell-functions/
3-
41# region prepare folders
52$Current = (Split-Path - Path $MyInvocation.MyCommand.Path )
63$Root = ((Get-Item $Current ).Parent).FullName
@@ -17,68 +14,76 @@ if([String]::IsNullOrEmpty($ModulePrefix)){
1714$CommonPrefix = $ModulePrefix
1815# endregion
1916
20- Get-ChildItem - Path $CodeSourcePath - Filter " *.ps1" | ForEach-Object {
17+ BeforeDiscovery {
18+ $CodeFile = Get-ChildItem - Path $CodeSourcePath - Filter " *.ps1"
19+ }
2120
22- Describe " Testing Code- file $ ( $_ .Name ) " {
21+ foreach ( $ file in $CodeFile ) {
2322
24- . ($_.FullName )
25- $ScriptName = $_.BaseName
26- $Verb = @ ( $ ($ScriptName ) -split ' -' )[0 ]
23+ . ($file.FullName )
2724
28- # $DetailedHelp = Get-Help $ScriptName -Detailed
29- $ScriptCommand = Get-Command - Name $ScriptName - All
30- $Ast = $ScriptCommand.ScriptBlock.Ast
31-
32-
33- Context " Naming" {
34- It " $ScriptName should have an approved verb" {
35- ( $Verb -in @ ( Get-Verb ).Verb ) | Should - Be $true
36- }
37-
38- try {
39- $FunctionPrefix = @ ( $ScriptName -split ' -' )[1 ].Substring( 0 , $CommonPrefix.Length )
40- }
41- catch {
42- $FunctionPrefix = @ ( $ScriptName -split ' -' )[1 ]
25+ # region variable
26+ $ScriptName = $file.BaseName
27+ $Verb = @ ( $ ($ScriptName ) -split ' -' )[0 ]
28+
29+ try {
30+ $FunctionPrefix = @ ( $ScriptName -split ' -' )[1 ].Substring( 0 , $CommonPrefix.Length )
31+ }
32+ catch {
33+ $FunctionPrefix = @ ( $ScriptName -split ' -' )[1 ]
34+ }
35+
36+ $DetailedHelp = Get-Help $ScriptName - Detailed
37+ $ScriptCommand = Get-Command - Name $ScriptName - All
38+ $Ast = $ScriptCommand.ScriptBlock.Ast
39+ # endregion
40+
41+ Describe " Test Code-file $ ( $file.Name ) " {
42+
43+ Context " Naming of $ ( $file.BaseName ) " {
44+
45+ It " $ScriptName should have an approved verb -> $Verb " - TestCases @ { Verb = $Verb } {
46+ ( $Verb -in @ ( Get-Verb ).Verb ) | Should - BeTrue
4347 }
44- It " $ScriptName Noun should have the Prefix '$ ( $CommonPrefix ) '" {
45- $FunctionPrefix | Should -match $CommonPrefix
48+
49+ It " $ScriptName Noun should have the Prefix '$ ( $CommonPrefix ) '" - TestCases @ { FunctionPrefix = $FunctionPrefix ; CommonPrefix = $CommonPrefix } {
50+ $FunctionPrefix | Should - Be $CommonPrefix
4651 }
52+
4753 }
4854
49- Context " Synopsis" {
50- It " $ScriptName should have a SYNOPSIS" {
51- ( $Ast -match ' SYNOPSIS' ) | Should - Be $true
55+ Context " Synopsis of $ ( $file.BaseName ) " {
56+
57+ It " $ScriptName should have a SYNOPSIS" - TestCases @ { Ast = $Ast } {
58+ ( $Ast -match ' SYNOPSIS' ) | Should - BeTrue
5259 }
53-
54- It " $ScriptName should have a DESCRIPTION" {
55- ( $Ast -match ' DESCRIPTION' ) | Should - Be $true
60+
61+ It " $ScriptName should have a DESCRIPTION" - TestCases @ { Ast = $Ast } {
62+ ( $Ast -match ' DESCRIPTION' ) | Should - BeTrue
5663 }
5764
58- It " $ScriptName should have a EXAMPLE" {
59- ( $Ast -match ' EXAMPLE' ) | Should - Be $true
65+ It " $ScriptName should have a EXAMPLE" - TestCases @ { Ast = $Ast } {
66+ ( $Ast -match ' EXAMPLE' ) | Should - BeTrue
6067 }
6168
6269 }
6370
64- Context " Parameters" {
71+ Context " Parameters of $ ( $file .BaseName ) " {
6572
66- It " $ScriptName $ ( $_ .Name ) should have a function $ScriptName " {
73+ It " $ ( $file .Name ) should have a function named $ ( $file .BaseName ) " - TestCases @ { Ast = $Ast ; ScriptName = $ScriptName } {
6774 ($Ast -match $ScriptName ) | Should - be $true
6875 }
6976
70- It " $ScriptName should have a CmdletBinding" {
77+ It " $ScriptName should have a CmdletBinding" - TestCases @ { Ast = $Ast } {
7178 [boolean ]( @ ( $Ast.FindAll ( { $true } , $true ) ) | Where-Object { $_.TypeName.Name -eq ' cmdletbinding' } ) | Should - Be $true
7279 }
7380
7481 $DefaultParams = @ ( ' Verbose' , ' Debug' , ' ErrorAction' , ' WarningAction' , ' InformationAction' , ' ErrorVariable' , ' WarningVariable' , ' InformationVariable' , ' OutVariable' , ' OutBuffer' , ' PipelineVariable' )
7582 foreach ( $p in @ ( $ScriptCommand.Parameters.Keys | Where-Object { $_ -notin $DefaultParams } | Sort-Object ) ) {
7683
77- <#
7884 It " $ScriptName the Help-text for paramater '$ ( $p ) ' should exist" {
7985 ( $p -in $DetailedHelp.parameters.parameter.name ) | Should - Be $true
8086 }
81- #>
8287 $Declaration = ( ( @ ( $Ast.FindAll ( { $true } , $true ) ) | Where-Object { $_.Name.Extent.Text -eq " $ ( ' $' ) $p " } ).Extent.Text -replace ' INT32' , ' INT' )
8388 # $VariableType = ( "\[$( $ScriptCommand.Parameters."$p".ParameterType.Name )\]" -replace 'INT32', 'INT' )
8489 $VariableTypeFull = " \[$ ( $ScriptCommand.Parameters ." $p " .ParameterType.FullName ) \]"
@@ -90,26 +95,8 @@ Get-ChildItem -Path $CodeSourcePath -Filter "*.ps1" | ForEach-Object {
9095 ( ( $Declaration -match $VariableType ) -or ( $Declaration -match $VariableTypeFull ) ) | Should - Be $true
9196 }
9297 }
93-
94- }
95-
96- Context " Variables" {
97- It " $ScriptName should have a function-variable" {
98- ($Ast -match ' \$function\s=\s\$\(\$MyInvocation.MyCommand.Name\)' ) | Should - be $true
99- }
100-
101- $code = $ScriptCommand.ScriptBlock
102- $ScriptVariables = $code.Ast.FindAll ( { $true } , $true ) |
103- Where-Object { $_.GetType ().Name -eq ' VariableExpressionAst' } |
104- Select-Object - Property VariablePath - ExpandProperty Extent
105-
106- foreach ( $sv in @ ( $ScriptVariables | Select-Object - ExpandProperty Text - Unique | Sort-Object ) ) {
107- It " $ScriptName variable '$ ( $sv ) ' should be in same (upper/lower) case everywhere" {
108- [boolean ]( $ScriptVariables | Where-Object { ( ( $_.Text -eq $sv ) -and ( $_.Text -cne $sv ) ) } ) | Should - Be $false
109- }
110- }
98+
11199 }
112-
113100 }
114- }
115101
102+ }
0 commit comments