tests/lint/scriptAnalyer.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
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace('tests\lint', '')
$scriptsModules = Get-ChildItem $here -Include *.psd1, *.psm1, *.ps1 -Exclude *.tests.ps1 -Recurse

Describe "Enviroment - Testing there is something to test and CMDLETS available." {
    Context "Checking files to test exist and Invoke-ScriptAnalyzer cmdLet is available" {
        It "Checking files exist to test." {
            $scriptsModules.count | Should Not Be 0
        }
        It "Checking Invoke-ScriptAnalyzer exists." {
            { Get-Command Invoke-ScriptAnalyzer -ErrorAction Stop } | Should Not Throw
        }
    }
}

Describe "Script\Module - Testing all scripts and modules" {
    $scriptAnalyzerRules = Get-ScriptAnalyzerRule

    forEach ($scriptModule in $scriptsModules) {
        switch -wildCard ($scriptModule) { 
            '*.psm1' { $typeTesting = 'Module' } 
            '*.ps1'  { $typeTesting = 'Script' } 
            '*.psd1' { $typeTesting = 'Manifest' } 
        }

        Context "Checking $typeTesting – $($scriptModule) - conforms to Script Analyzer Rules" {
            forEach ($scriptAnalyzerRule in $scriptAnalyzerRules) {
                It "Script Analyzer Rule $scriptAnalyzerRule" {
                    (Invoke-ScriptAnalyzer -Path $scriptModule -IncludeRule $scriptAnalyzerRule).count | Should Be 0
                }
            }
        }
    }
}