tests/_scriptAnalyer.tests.ps1

$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace('tests', '')
$scriptsModules = Get-ChildItem $here -Include *.psd1, *.psm1, *.ps1 -Exclude *.tests.ps1 -Recurse

Describe 'General - Testing all scripts and modules against the Script Analyzer Rules' {
    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
        }
    }

    $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
                }
            }
        }
    }
}