Skip to content

Commit 510f3f0

Browse files
committed
new test for psscriptanalyzer providing better output and using pester foreach cases
1 parent 730f49b commit 510f3f0

File tree

3 files changed

+108
-80
lines changed

3 files changed

+108
-80
lines changed

PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,49 @@
22
Param (
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

1433
Describe '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
}

templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,49 @@
22
Param (
33
[switch]
44
$SkipTest,
5-
5+
66
[string[]]
77
$CommandPath = @("$global:testroot\..\þnameþ\functions", "$global:testroot\..\þnameþ\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

1433
Describe 'Invoking PSScriptAnalyzer against commandbase' {
15-
$commandFiles = foreach ($path in $CommandPath) { Get-ChildItem -Path $path -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
}

templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,49 @@
22
Param (
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

1433
Describe 'Invoking PSScriptAnalyzer against commandbase' {
15-
$commandFiles = foreach ($path in $CommandPath) {
16-
Get-ChildItem -Path $path -Recurse | Where-Object Name -like "*.ps1"
17-
}
18-
$scriptAnalyzerRules = Get-ScriptAnalyzerRule
19-
20-
foreach ($file in $commandFiles)
21-
{
22-
Context "Analyzing $($file.BaseName)" {
23-
$analysis = Invoke-ScriptAnalyzer -Path $file.FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
24-
25-
forEach ($rule in $scriptAnalyzerRules)
26-
{
27-
It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } {
28-
If ($analysis.RuleName -contains $rule)
29-
{
30-
$analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) }
31-
32-
1 | Should -Be 0
33-
}
34-
else
35-
{
36-
0 | Should -Be 0
37-
}
38-
}
39-
}
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
4048
}
4149
}
4250
}

0 commit comments

Comments
 (0)