forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ps1
216 lines (175 loc) · 6.88 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#! /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] $CC,
[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 ($CI) {
$Inline = $true
}
if (-not $NoBuild) {
& "$PSScriptRoot/build.ps1" -Inline:$Inline
}
Import-Module $PSScriptRoot/bin/Pester.psd1 -ErrorAction Stop
if ($CC) {
Write-Host "Running Code Coverage"
$env:PESTER_CC_DEBUG = 0
$env:PESTER_CC_IN_CC = 1
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$here = {}
$bp = Set-PSBreakpoint -Script $PSCommandPath -Line $here.StartPosition.StartLine -Action {}
$null = $bp | Disable-PSBreakpoint
$Enter_CoverageAnalysis = & (Get-Module Pester) { Get-Command Enter-CoverageAnalysis }
if ($Inline) {
$breakpoints = & $Enter_CoverageAnalysis -CodeCoverage "$PSScriptRoot/bin/Pester*" -UseBreakpoints $false
}
else {
$breakpoints = & $Enter_CoverageAnalysis -CodeCoverage "$PSScriptRoot/src/*" -UseBreakpoints $false
}
$Start_TraceScript = & (Get-Module Pester) { Get-Command Start-TraceScript }
$patched, $tracer = & $Start_TraceScript $breakpoints
}
# 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-Object {
if ($CC -and $_.Name -eq 'Pester.RSpec.Coverage.ts.ps1') {
# these tests are turning off cc by Set-Trace -Off,
# so we can't run them with cc
}
else {
$r = & $_.FullName -PassThru -NoBuild:$true
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
}
function New-Dictionary ([hashtable]$Hashtable) {
$d = [System.Collections.Generic.Dictionary[string, object]]::new()
$Hashtable.GetEnumerator() | ForEach-Object { $d.Add($_.Key, $_.Value) }
$d
}
function Clear-WhiteSpace ($Text) {
"$($Text -replace "(`t|`n|`r)"," " -replace "\s+"," ")".Trim()
}
} | 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 pester code coverage, because we measure it externally, see CC switch
$configuration.CodeCoverage.Enabled = $false
$configuration.TestResult.Enabled = $true
}
$r = Invoke-Pester -Configuration $configuration
if ($CC) {
try {
$Write_CoverageReport = & (Get-Module Pester) { Get-Command Write-CoverageReport }
$Stop_TraceScript = & (Get-Module Pester) { Get-Command Stop-TraceScript }
$Get_CoverageReport = & (Get-Module Pester) { Get-Command Get-CoverageReport }
$Get_JaCoCoReportXml = & (Get-Module Pester) { Get-Command Get-JaCoCoReportXml }
& $Stop_TraceScript -Patched $patched
$measure = $tracer.Hits
$coverageReport = & $Get_CoverageReport -CommandCoverage $breakpoints -Measure $measure
}
finally {
if ($null -ne $bp) {
$bp | Remove-PSBreakpoint
}
}
[xml] $jaCoCoReport = [xml] (& $Get_JaCoCoReportXml -CommandCoverage $breakpoints -TotalMilliseconds $sw.ElapsedMilliseconds -CoverageReport $coverageReport -Format "JaCoCo")
$jaCoCoReport.OuterXml | Set-Content -Path $PSScriptRoot/coverage.xml
& $Write_CoverageReport -CoverageReport $coverageReport
}
if ("Failed" -eq $r.Result) {
throw "Run failed!"
}