22Param (
33 [switch ]
44 $SkipTest ,
5-
5+
66 [string []]
77 $CommandPath = @ (" $global :testroot \..\functions" , " $global :testroot \..\internal\functions" )
88)
99
10- if ($SkipTest ) { return }
10+ BeforeDiscovery {
11+ if ($SkipTest ) { return }
12+
13+ $global :__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList
14+
15+ # Create an array containing the path and basename of all files to test
16+ $commandFiles = $CommandPath | ForEach-Object {
17+ Get-ChildItem - Path $_ - Recurse | Where-Object Name -like " *.ps1"
18+ } | ForEach-Object {
19+ @ {
20+ BaseName = $_.BaseName
21+ FullName = $_.FullName
22+ }
23+ }
1124
12- $global :__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList
25+ # Create an array contain all rules
26+ $scriptAnalyzerRules = Get-ScriptAnalyzerRule | ForEach-Object {
27+ @ {
28+ RuleName = $_.RuleName
29+ }
30+ }
31+ }
1332
1433Describe ' Invoking PSScriptAnalyzer against commandbase' {
15- $commandFiles = Get-ChildItem - Path $CommandPath - Recurse | Where-Object Name -like " *.ps1"
16- $scriptAnalyzerRules = Get-ScriptAnalyzerRule
17-
18- foreach ($file in $commandFiles )
19- {
20- Context " Analyzing $ ( $file.BaseName ) " {
21- $analysis = Invoke-ScriptAnalyzer - Path $file.FullName - ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
22-
23- forEach ($rule in $scriptAnalyzerRules )
24- {
25- It " Should pass $rule " - TestCases @ { analysis = $analysis ; rule = $rule } {
26- If ($analysis.RuleName -contains $rule )
27- {
28- $analysis | Where-Object RuleName -EQ $rule - outvariable failures | ForEach-Object { $null = $global :__pester_data.ScriptAnalyzer.Add ($_ ) }
29-
30- 1 | Should - Be 0
31- }
32- else
33- {
34- 0 | Should - Be 0
35- }
36- }
37- }
34+
35+ Context " Analyzing <BaseName>" - ForEach $commandFiles {
36+ BeforeAll {
37+ $analysis = Invoke-ScriptAnalyzer - Path $FullName - ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
38+ }
39+
40+ It " Should pass <RuleName>" - Foreach $scriptAnalyzerRules {
41+ # Test if the rule is present and if so create a string containing more info which will be shown in the details of the test output. If it's empty the test is succesfull as there is no problem with this rule.
42+ $analysis | Where-Object RuleName -EQ $RuleName | Foreach-Object {
43+ # Create a string
44+ " $ ( $_.Severity ) at Line $ ( $_.Line ) Column $ ( $_.Column ) with '$ ( $_.Extent ) '"
45+ # Add the data (and supress the output) to the global variable for later use
46+ $null = $global :__pester_data.ScriptAnalyzer.Add ($_ )
47+ } | Should - BeNullOrEmpty
3848 }
3949 }
4050}
0 commit comments