PesterTestScriptSources/PSScriptAnalyzer.Tests.ps1

#Requires -Modules RVR.ScriptAnalyzerRules

<#
   .SYNOPSIS
   The Pester tests for the test-modulePipline module
 
   .DESCRIPTION
   The Pester tests for the test-modulePipline module
 
   Run this via Pester
 
   .PARAMETER ModuleName
   The name of the module
 
#>


param(
    [parameter()]
    [ValidateNotNull()]
    $ModuleName
)

Describe "PS ScriptAnalyser" {

    $PSScripAnalyzerSettings = @{
        # Roberts custom rule
        CustomRulePath      = @("$($(Get-InstalledModule RVR.ScriptAnalyzerRules).InstalledLocation)\*.psm1")
    
        # If not set then only the custom rule is applied
        IncludeDefaultRules = $true
    
        # Enable two rules I like
        Rules               = @{
            PSProvideCommentHelp = @{
                Enable                  = $true
                ExportedOnly            = $false
                BlockComment            = $true
                VSCodeSnippetCorrection = $false
                Placement               = "before"
            }
    
            PSUseCorrectCasing   = @{
                Enable = $true
            }
        }
        ExcludeRules        = @('PSAvoidTrailingWhitespace')
    }

    It "Script analyser should be installed" {
        [boolean](Get-Command -Module PSScriptAnalyzer) | Should Be $true
    }

    # Invoke the script analyzer. Please be aware it uses implicit settings.
    $ScriptAnalyzerResults = Invoke-ScriptAnalyzer -Path "./$ModuleName" -Recurse -Settings $PSScripAnalyzerSettings

    It "No ScriptAnalyzer rule should be violated. Please see the violoations below" {
        $ScriptAnalyzerResults | Should BeNullOrEmpty
    }

    foreach ($Result in $ScriptAnalyzerResults) {
        It "ScriptAnalyzer rule violation $($Result.RuleName) on Line $($Result.Line) ($($result.ScriptName))" {
            $Result | Should BeNullOrEmpty
        }
    }
}