-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunTests.ps1
124 lines (111 loc) · 4.1 KB
/
RunTests.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
param(
# Enable code coverage reporting.
[Switch]$Coverage,
# Enable tests requiring internet access on local builds.
[Switch]$Online
)
<#
.SYNOPSIS
Validate the PowerShell environment and run unit tests on functions.
.DESCRIPTION
Run all unit tests for the PowerShell scripts, upload results to the
AppVeyor test console, and upload code-coverage to codecov.io.
.EXAMPLE
.\RunTests.ps1
#>
Set-StrictMode -Version Latest
[String]$local:BuildName = 'PowerShell v' +
$PSVersionTable.PSVersion.ToString()
if (!$env:APPVEYOR) {
$BuildName += ' local'
}
$Flag = 'ci_scripts'
##====--------------------------------------------------------------------====##
# Check manifest files
##====--------------------------------------------------------------------====##
Import-Module "${PSScriptRoot}\AppVeyor\Send-Message.psd1"
( Resolve-Path ".\*.psd1", ".\*\*.psd1" |
ForEach-Object -Process {
if (-not ($_.Path -match '.*PSScriptAnalyzerSettings\.psd1$') ) {
Test-ModuleManifest -Path $_
}
} | Format-Table -Wrap -AutoSize | Out-String
).Trim() -replace '[ ]*(\r?\n)','$1' |
Send-Message -Info -Message 'Detected Modules:'
Import-Module "${PSScriptRoot}\AppVeyorHelpers.psd1" -Force
( (Test-ModuleManifest "${PSScriptRoot}\AppVeyorHelpers.psd1"
).ExportedFunctions.Values |
Format-Table -Property Name -HideTableHeaders | Out-String
).Trim() -replace '[ ]*(\r?\n)','$1' |
Send-Message -Info -Message 'AppVeyorHelpers - Exported Functions:'
##====--------------------------------------------------------------------====##
# Pester Configuration
##====--------------------------------------------------------------------====##
Import-Module "${PSScriptRoot}\local\CI.psd1"
# Specifies the test files run by Pester.
# Here: Only run dedicated test-files in subdirectories relative to this script.
[Object[]]$Script = "${PSScriptRoot}\*\*.Tests.ps1"
# Runs only tests in Describe blocks named to match this pattern.
[String[]]$TestName = @('*')
[String[]]$Tag = ''
[String[]]$ExcludeTag = @('AlwaysExclude')
if (-not ((Assert-CI) -or $Online) ) {
$ExcludeTag += 'online'
}
[String]$OutputFile = 'ScriptTestResults.xml'
[String]$OutputFormat = 'NUnitXML'
[String]$OutputFormatUpload = 'NUnit'
[Object[]]$CodeCoverage = @(
@{Path="${PSScriptRoot}\*\*.ps1"; IncludeTests=$false},
@{Path="${PSScriptRoot}\*\*.psm1"; IncludeTests=$false}
)
[String]$CodeCoverageOutputFile = 'ScriptCodeCoverage.xml'
[String]$CodeCoverageOutputFileFormat = 'JaCoCo'
# Report configuration.
# 'Default','All','Passed','Failed','Pending','Skipped',
# 'Inconclusive','Describe','Context','Summary','Header','Fails'
# Do not use 'none' !
[String[]]$Show = 'All'
##====--------------------------------------------------------------------====##
# Run Unit tests
##====--------------------------------------------------------------------====##
Write-Verbose 'Run Pester unit tests ...'
if ($Coverage) {
$result = Invoke-Pester `
-Script $Script -TestName $TestName `
-Tag $Tag -ExcludeTag $ExcludeTag `
-OutputFormat $OutputFormat -OutputFile $OutputFile `
-CodeCoverage $CodeCoverage `
-CodeCoverageOutputFile $CodeCoverageOutputFile `
-CodeCoverageOutputFileFormat $CodeCoverageOutputFileFormat `
-Show $Show -PassThru
} else {
$result = Invoke-Pester `
-Script $Script -TestName $TestName `
-Tag $Tag -ExcludeTag $ExcludeTag `
-OutputFormat $OutputFormat -OutputFile $OutputFile `
-Show $Show -PassThru
}
if (-not $env:AppVeyor) {
Write-Verbose 'No AppVeyor environment detected. Uploads disabled.'
$UseWhatif = $true
} else {
$UseWhatif = $false
}
Send-TestResult -File $OutputFile -Format $OutputFormatUpload `
-WhatIf:$UseWhatif
if ($Coverage) {
try {
Send-Codecov -File $CodeCoverageOutputFile -BuildName:$BuildName `
-Flag:$Flag -Whatif:$UseWhatif
} catch {
# ignore exit-code
Write-Verbose 'Send-Codecov failed'
}
}
Write-Verbose 'Run Pester unit tests ... done'
if ($result.FailedCount -gt 0) {
throw "$($result.FailedCount) tests failed."
} else {
Write-Output 'RunTests: success'
}