forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ps1
159 lines (127 loc) · 4.67 KB
/
test.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/pwsh
<#
.SYNOPSIS
Used to run the tests locally for Pester development.
.PARAMETER CI
Builds the module using the inlined mode.
Exits after run. Enables test results and code coverage on `/src/*`.
Enables exit with non-zero exit code if tests don't pass. Forces P Tests
to fail when `dt` is left in the tests. `dt` only runs the specified test,
so leaving it in code would run only one test from the file on the server.
.PARAMETER SkipPTests
Skips Passthrough P tests. Skip the tests written using the P module, Unit
Tests for the Runtime, and Acceptance Tests for Pester
.PARAMETER NoBuild
Skips running build.ps1. Do not build the underlying csharp components.
Used in CI pipeline since a clean build has already been run prior to Test.
.PARAMETER File
If specified, set file path to test file, otherwise set to /tst folder.
Pass the file to run Pester (not P) tests from.
*/demo/*, */examples/*, */testProjects/* are excluded from tests.
.PARAMETER Inline
Forces inlining the module into a single file. This is how real build is
done, but makes local debugging difficult. When -CI is used, inlining is
forced.
.NOTES
Tests are excluded with Tags VersionChecks, StyleRules, Help.
#>
param (
# force P to fail when I leave `dt` in the tests
[switch] $CI,
[switch] $SkipPTests,
[switch] $NoBuild,
[switch] $Inline,
[string[]] $File
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# assigning error view explicitly to change it from the default on PowerShell 7
$ErrorView = "NormalView"
"Using PS: $($PsVersionTable.PSVersion)"
"In path: $($pwd.Path)"
if (-not $NoBuild) {
if ($CI) {
& "$PSScriptRoot/build.ps1" -Inline
}
else {
& "$PSScriptRoot/build.ps1" -Inline:$Inline
}
}
# remove pester because we will be reimporting it in multiple other places
Get-Module Pester | Remove-Module
if (-not $SkipPTests) {
$result = @(Get-ChildItem $PSScriptRoot/tst/*.ts.ps1 -Recurse |
foreach {
$r = & $_.FullName -PassThru -NoBuild:$NoBuild
if ($r.Failed -gt 0) {
[PSCustomObject]@{
FullName = $_.FullName
Count = $r.Failed
}
}
})
if (0 -lt $result.Count) {
Write-Host -ForegroundColor Red "P tests failed!"
foreach ($r in $result) {
Write-Host -ForegroundColor Red "$($r.Count) tests failed in '$($r.FullName)'."
}
if ($CI) {
exit 1
}
else {
return
}
}
else {
Write-Host -ForegroundColor Green "P tests passed!"
}
}
Get-Module Pester | Remove-Module
Import-Module $PSScriptRoot/bin/Pester.psd1 -ErrorAction Stop
Import-Module $PSScriptRoot/tst/axiom/Axiom.psm1 -DisableNameChecking
# reset pester and all preferences
$PesterPreference = [PesterConfiguration]::Default
# add our own in module scope because the implementation
# pester relies on being in different sesstion state than
# the module scope target
Get-Module TestHelpers | Remove-Module
New-Module -Name TestHelpers -ScriptBlock {
function InPesterModuleScope {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock
)
$module = Get-Module -Name Pester -ErrorAction Stop
. $module $ScriptBlock
}
} | Out-Null
$configuration = [PesterConfiguration]::Default
$configuration.Output.Verbosity = "Normal"
$configuration.Debug.WriteDebugMessages = $false
$configuration.Debug.WriteDebugMessagesFrom = 'CodeCoverage'
$configuration.Debug.ShowFullErrors = $false
$configuration.Debug.ShowNavigationMarkers = $false
if ($null -ne $File -and 0 -lt @($File).Count) {
$configuration.Run.Path = $File
}
else {
$configuration.Run.Path = "$PSScriptRoot/tst"
}
$configuration.Run.ExcludePath = '*/demo/*', '*/examples/*', '*/testProjects/*'
$configuration.Run.PassThru = $true
$configuration.Filter.ExcludeTag = 'VersionChecks', 'StyleRules'
if ($CI) {
$configuration.Run.Exit = $true
# not using code coverage, it is still very slow
$configuration.CodeCoverage.Enabled = $false
$configuration.CodeCoverage.Path = "$PSScriptRoot/src/*"
# experimental, uses the Profiler based tracer to do code coverage without using breakpoints
$configuration.CodeCoverage.UseBreakpoints = $false
$configuration.TestResult.Enabled = $true
}
$r = Invoke-Pester -Configuration $configuration
if ("Failed" -eq $r.Result) {
throw "Run failed!"
}