psakeBuild.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
55
56
57
58
#Requires -Version 4
#Requires -RunAsAdministrator

[cmdletbinding()]
param(
    [switch]
    $CodeCoverage = $false
)

properties {
    $root = $PSScriptRoot
}

# Default task includes Analyzing and Testing of module
task default -depends Analyze, Test

# Analyze by running Invoke-ScriptAnalyzer. Check script against best known practices
task Analyze {
    $saResults = Invoke-ScriptAnalyzer -Path "$root\Public" -Severity @('Error', 'Warning') -Recurse -ExcludeRule "PSAvoidUsingWriteHost", "PSUseDeclaredVarsMoreThanAssignments" -Verbose:$false
    $saResults += Invoke-ScriptAnalyzer -Path "$root\Private" -Severity @('Error', 'Warning') -Recurse -Verbose:$false
    if ($saResults)
    {
        $saResults | Format-Table
        Write-Error -Message 'One or more Script Analyzer errors/warnings where found. Build cannot continue!'
    }
}

# Run our test to make sure everything is in line
task Test {
    $tests = @(Get-ChildItem -Path $PSScriptRoot\Test\**\*.Test.ps1 -Recurse)

    if ($CodeCoverage)
    {
        $codeCoveragePaths = @()
        foreach ($test in $tests)
        {
            $name = $test.Name -replace ".Test.ps1$", ".ps1"
            $directory = $test.Directory.Name
            $codeCoveragePath = $PSScriptRoot, $directory, $name -join "\"
            if (Test-Path $codeCoveragePath)
            {
                $codeCoveragePaths += $codeCoveragePath
            }
        }

        $testResults = Invoke-Pester -Path @($tests.FullName) -CodeCoverage @($codeCoveragePaths) -PassThru
    }
    else
    {
        $testResults = Invoke-Pester -Path @($tests.FullName) -PassThru
    }

    if ($testResults.FailedCount -gt 0)
    {
        $testResults.TestResult | Where-Object { $_.Result -ne "Passed" } | Format-List
        Write-Error -Message 'One or more Pester tests failed. Build cannot continue!'
    }
}