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
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
$packages = get-package
if ($packages.Name  -contains "PSScriptAnalyzer") {
    #PSScriptAnalyzer is installed on the system
} else {
    Write-Output "Installing latest version of PSScriptAnalyzer"

    #install PSScriptAnalyzer
    Install-Package PSScriptAnalyzer -Force -Scope CurrentUser
}

$script:ModuleName = 'cSpeculationControlFixes'

$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path

# Get the list of Pester Tests we are going to skip
$PesterTestExceptions = Get-Content -Path "$ModuleBase\Project.Exceptions.txt"

# For tests in .\Tests subdirectory
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
    $ModuleBase = Split-Path $ModuleBase -Parent
}

Describe "PSScriptAnalyzer rule-sets" -Tag Build , ScriptAnalyzer {

    $Rules = Get-ScriptAnalyzerRule
    $scripts = Get-ChildItem $ModuleBase -Include *.ps1, *.psm1, *.psd1 -Recurse # | Where-Object fullname -notmatch 'Examples'

    foreach ( $Script in $scripts )
    {
        Context "Script '$($script.FullName)'" {

            foreach ( $rule in $rules )
            {
                # Skip all rules that are on the exclusions list
                if ($PesterTestExceptions -contains $rule.RuleName) { continue }
                It "Rule [$rule]" {

                    (Invoke-ScriptAnalyzer -Path $script.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0
                }
            }
        }
    }
}


Describe "General project validation: $moduleName" -Tags Build {
    BeforeAll {
        Get-Module $ModuleName | Remove-Module
    }
    It "Module '$moduleName' can import cleanly" {
        {Import-Module $ModuleBase\$ModuleName.psd1 -force } | Should Not Throw
    }
}