tests/general/PSScriptAnalyzer.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[CmdletBinding()]
Param (
    [switch]
    $SkipTest,

    [string[]]
    $CommandPath = @("$PSScriptRoot\..\..\functions", "$PSScriptRoot\..\..\internal\functions")
)

if ($SkipTest) { return }

$list = New-Object System.Collections.ArrayList

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 PSUseSingularNouns

            forEach ($rule in $scriptAnalyzerRules)
            {
                It "Should pass $rule" {
                    $ruleBreakers = $analysis | Where-Object RuleName -eq $rule
                    $ruleBreakers | ForEach-Object { $list.Add($_) }
                    $ruleBreakers.count | Should Be 0
                }
            }
        }
    }
}

$list | Out-Default