From 510f3f0613202211ab4385ff5a535248ce218045 Mon Sep 17 00:00:00 2001 From: Leo Visser Date: Sun, 7 Jan 2024 21:13:19 +0100 Subject: [PATCH] new test for psscriptanalyzer providing better output and using pester foreach cases --- .../tests/general/PSScriptAnalyzer.Tests.ps1 | 62 ++++++++++-------- .../tests/general/PSScriptAnalyzer.Tests.ps1 | 62 ++++++++++-------- .../general/PSScriptAnalyzer.Tests.ps1 | 64 +++++++++++-------- 3 files changed, 108 insertions(+), 80 deletions(-) diff --git a/PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1 b/PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1 index 74e5a65..4b9b2d2 100644 --- a/PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1 +++ b/PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1 @@ -2,39 +2,49 @@ Param ( [switch] $SkipTest, - + [string[]] $CommandPath = @("$global:testroot\..\functions", "$global:testroot\..\internal\functions") ) -if ($SkipTest) { return } +BeforeDiscovery { + if ($SkipTest) { return } + + $global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + + # Create an array containing the path and basename of all files to test + $commandFiles = $CommandPath | ForEach-Object { + Get-ChildItem -Path $_ -Recurse | Where-Object Name -like "*.ps1" + } | ForEach-Object { + @{ + BaseName = $_.BaseName + FullName = $_.FullName + } + } -$global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + # Create an array contain all rules + $scriptAnalyzerRules = Get-ScriptAnalyzerRule | ForEach-Object { + @{ + RuleName = $_.RuleName + } + } +} Describe 'Invoking PSScriptAnalyzer against commandbase' { - $commandFiles = Get-ChildItem -Path $CommandPath -Recurse | Where-Object Name -like "*.ps1" - $scriptAnalyzerRules = Get-ScriptAnalyzerRule - - foreach ($file in $commandFiles) - { - Context "Analyzing $($file.BaseName)" { - $analysis = Invoke-ScriptAnalyzer -Path $file.FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess - - forEach ($rule in $scriptAnalyzerRules) - { - It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } { - If ($analysis.RuleName -contains $rule) - { - $analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) } - - 1 | Should -Be 0 - } - else - { - 0 | Should -Be 0 - } - } - } + + Context "Analyzing " -ForEach $commandFiles { + BeforeAll { + $analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess + } + + It "Should pass " -Foreach $scriptAnalyzerRules { + # 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. + $analysis | Where-Object RuleName -EQ $RuleName | Foreach-Object { + # Create a string + "$($_.Severity) at Line $($_.Line) Column $($_.Column) with '$($_.Extent)'" + # Add the data (and supress the output) to the global variable for later use + $null = $global:__pester_data.ScriptAnalyzer.Add($_) + } | Should -BeNullOrEmpty } } } \ No newline at end of file diff --git a/templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1 b/templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1 index 1686112..f606f49 100644 --- a/templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1 +++ b/templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1 @@ -2,39 +2,49 @@ Param ( [switch] $SkipTest, - + [string[]] $CommandPath = @("$global:testroot\..\þnameþ\functions", "$global:testroot\..\þnameþ\internal\functions") ) -if ($SkipTest) { return } +BeforeDiscovery { + if ($SkipTest) { return } + + $global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + + # Create an array containing the path and basename of all files to test + $commandFiles = $CommandPath | ForEach-Object { + Get-ChildItem -Path $_ -Recurse | Where-Object Name -like "*.ps1" + } | ForEach-Object { + @{ + BaseName = $_.BaseName + FullName = $_.FullName + } + } -$global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + # Create an array contain all rules + $scriptAnalyzerRules = Get-ScriptAnalyzerRule | ForEach-Object { + @{ + RuleName = $_.RuleName + } + } +} Describe 'Invoking PSScriptAnalyzer against commandbase' { - $commandFiles = foreach ($path in $CommandPath) { Get-ChildItem -Path $path -Recurse | Where-Object Name -like "*.ps1" } - $scriptAnalyzerRules = Get-ScriptAnalyzerRule - - foreach ($file in $commandFiles) - { - Context "Analyzing $($file.BaseName)" { - $analysis = Invoke-ScriptAnalyzer -Path $file.FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess - - forEach ($rule in $scriptAnalyzerRules) - { - It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } { - If ($analysis.RuleName -contains $rule) - { - $analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) } - - 1 | Should -Be 0 - } - else - { - 0 | Should -Be 0 - } - } - } + + Context "Analyzing " -ForEach $commandFiles { + BeforeAll { + $analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess + } + + It "Should pass " -Foreach $scriptAnalyzerRules { + # 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. + $analysis | Where-Object RuleName -EQ $RuleName | Foreach-Object { + # Create a string + "$($_.Severity) at Line $($_.Line) Column $($_.Column) with '$($_.Extent)'" + # Add the data (and supress the output) to the global variable for later use + $null = $global:__pester_data.ScriptAnalyzer.Add($_) + } | Should -BeNullOrEmpty } } } \ No newline at end of file diff --git a/templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1 b/templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1 index a99b60d..4b9b2d2 100644 --- a/templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1 +++ b/templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1 @@ -2,41 +2,49 @@ Param ( [switch] $SkipTest, - + [string[]] $CommandPath = @("$global:testroot\..\functions", "$global:testroot\..\internal\functions") ) -if ($SkipTest) { return } +BeforeDiscovery { + if ($SkipTest) { return } + + $global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + + # Create an array containing the path and basename of all files to test + $commandFiles = $CommandPath | ForEach-Object { + Get-ChildItem -Path $_ -Recurse | Where-Object Name -like "*.ps1" + } | ForEach-Object { + @{ + BaseName = $_.BaseName + FullName = $_.FullName + } + } -$global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList + # Create an array contain all rules + $scriptAnalyzerRules = Get-ScriptAnalyzerRule | ForEach-Object { + @{ + RuleName = $_.RuleName + } + } +} Describe 'Invoking PSScriptAnalyzer against commandbase' { - $commandFiles = foreach ($path in $CommandPath) { - Get-ChildItem -Path $path -Recurse | Where-Object Name -like "*.ps1" - } - $scriptAnalyzerRules = Get-ScriptAnalyzerRule - - foreach ($file in $commandFiles) - { - Context "Analyzing $($file.BaseName)" { - $analysis = Invoke-ScriptAnalyzer -Path $file.FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess - - forEach ($rule in $scriptAnalyzerRules) - { - It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } { - If ($analysis.RuleName -contains $rule) - { - $analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) } - - 1 | Should -Be 0 - } - else - { - 0 | Should -Be 0 - } - } - } + + Context "Analyzing " -ForEach $commandFiles { + BeforeAll { + $analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess + } + + It "Should pass " -Foreach $scriptAnalyzerRules { + # 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. + $analysis | Where-Object RuleName -EQ $RuleName | Foreach-Object { + # Create a string + "$($_.Severity) at Line $($_.Line) Column $($_.Column) with '$($_.Extent)'" + # Add the data (and supress the output) to the global variable for later use + $null = $global:__pester_data.ScriptAnalyzer.Add($_) + } | Should -BeNullOrEmpty } } } \ No newline at end of file