Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BaseName>" -ForEach $commandFiles {
BeforeAll {
$analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
}

It "Should pass <RuleName>" -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
}
}
}
62 changes: 36 additions & 26 deletions templates/MiniModule/tests/general/PSScriptAnalyzer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BaseName>" -ForEach $commandFiles {
BeforeAll {
$analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
}

It "Should pass <RuleName>" -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
}
}
}
64 changes: 36 additions & 28 deletions templates/PSFTests/general/PSScriptAnalyzer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BaseName>" -ForEach $commandFiles {
BeforeAll {
$analysis = Invoke-ScriptAnalyzer -Path $FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess
}

It "Should pass <RuleName>" -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
}
}
}