pester-tests/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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)][validateScript({Test-Path $_})][string]$Path,
    [Parameter(Mandatory=$false)][switch]$recurse,
    [Parameter(Mandatory=$false)][validateScript({Test-Path $_ -PathType Container})][string]$CustomRulePath,
    [Parameter(Mandatory=$false)][validateSet('Information', 'Warning','Error')][string]$MinimumSeverityLevel = 'Error'
)
#Get native rules
$Rules = Get-ScriptAnalyzerRule
If ($CustomRulePath)
{
    Write-Verbose "Getting custom PSSA rules from '$CustomRulePath'"
    $Rules += Get-ScriptAnalyzerRule -CustomRulePath $CustomRulePath -RecurseCustomRulePath
}
Write-Verbose "Total Rule Count: $($Rules.count)"
#Get scripts to be tested
if ((Get-Item $path).PSIsContainer)
{
    Write-Verbose "Specified path '$path' is a directory"
    $scriptsModules = Get-ChildItem $Path -Include *.psd1, *.psm1, *.ps1 -Recurse
} else {
    Write-Verbose "Specified path '$path' is a file"
    $scriptsModules = Get-Item $path -Include *.psd1, *.psm1, *.ps1 
}
#work out the severities to be included

Switch ($MinimumSeverityLevel)
{
    'Information' {$Severities = @('Information', 'Warning', 'Error')}
    'Warning' {$Severities = @('Warning', 'Error')}
    'Error' {$Severities = @('Error')}
}
Write-Verbose "Total Script file count: $($scriptsModules.count)"
Describe "PowerShell Script Analyzer Test" {
    Context "Test Scripts Should Exist" {
        It "Test file count should be greater than 0" {
            $scriptsModules.count | Should Not Be 0
        }
    }
    Foreach ($scriptModule in $scriptsModules) {
        switch -Wildcard ($scriptModule) { 
            '*.psm1' { $ScriptType = 'Module' } 
            '*.ps1'  { $ScriptType = 'Script' } 
            '*.psd1' { $ScriptType = 'Manifest' } 
        }
        Write-Verbose "Test $ScriptType '$scriptModule' against to Script Analyzer Rules"
        Context "Test $ScriptType '$scriptModule' against to Script Analyzer Rules" {
            Foreach ($rule in $Rules) {
                Write-Verbose "Rule name: $rule"
                
                It "Should Pass Script Analyzer Rule '$rule'"{
                    (Invoke-ScriptAnalyzer -Path $scriptModule -IncludeRule $rule -Severity $Severities -SaveDscDependency).count | Should Be 0
                }
            }
        }
    }
}