tests/Project.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
$projectRoot = Resolve-Path (Split-Path  $PSScriptRoot -Parent)
$moduleRoot = Join-Path $projectRoot ([System.IO.Path]::DirectorySeparatorChar)
# $moduleName = Split-Path $moduleRoot -Leaf

Describe "PSScriptAnalyzer rule-sets" -Tag Build {
    $Rules = Get-ScriptAnalyzerRule -Severity Error
    $ExcludedRules = 'PSAvoidUsingEmptyCatchBlock', 'PSUseShouldProcessForStateChangingFunctions', 'PSAvoidUsingWriteHost', 'PSProvideCommentHelp', 'PSAvoidTrailingWhitespace'
    $scripts = Get-ChildItem $moduleRoot -Include *.ps1, *.psm1, *.psd1 -Recurse | Where-Object fullname -notmatch 'classes'

    foreach ( $Script in $scripts ) {
        $results = Invoke-ScriptAnalyzer -Path $script.FullName -includeRule $Rules -ExcludeRule $ExcludedRules
        if ($results) {
            foreach ($rule in $results) {
                It $rule.RuleName {
                    $message = "{0} Line {1}: {2}" -f $rule.Severity, $rule.Line, $rule.message
                    $message | Should Be ""
                }
            }
        }
        else {
            It "Script '$($script.Name)' should not fail any rules" {
                $results | Should BeNullOrEmpty
            }
        }
    }
}