3
3
This script will invoke Pester tests, then serialize XML results and pull them in appveyor.yml
4
4
5
5
. DESCRIPTION
6
- Internal function that creates SMO server object.
6
+ Internal function that runs pester tests
7
7
8
8
. PARAMETER Finalize
9
9
If Finalize is specified, we collect XML output, upload tests, and indicate build errors
@@ -159,23 +159,13 @@ function Get-CodecovReport($Results, $ModuleBase) {
159
159
$newreport
160
160
}
161
161
162
- function Send-CodecovReport ($CodecovReport ) {
163
- $params = @ { }
164
- $params [' branch' ] = $env: APPVEYOR_REPO_BRANCH
165
- $params [' service' ] = " appveyor"
166
- $params [' job' ] = $env: APPVEYOR_ACCOUNT_NAME
167
- if ($params [' job' ]) { $params [' job' ] += ' /' + $env: APPVEYOR_PROJECT_SLUG }
168
- if ($params [' job' ]) { $params [' job' ] += ' /' + $env: APPVEYOR_BUILD_VERSION }
169
- $params [' build' ] = $env: APPVEYOR_JOB_ID
170
- $params [' pr' ] = $env: APPVEYOR_PULL_REQUEST_NUMBER
171
- $params [' slug' ] = $env: APPVEYOR_REPO_NAME
172
- $params [' commit' ] = $env: APPVEYOR_REPO_COMMIT
173
- Add-Type - AssemblyName System.Web
174
- $CodeCovParams = [System.Web.HttpUtility ]::ParseQueryString([String ]::Empty)
175
- $params.GetEnumerator () | Where-Object Value | ForEach-Object { $CodeCovParams.Add ($_.Name , $_.Value ) }
176
- $Request = [System.UriBuilder ](' https://codecov.io/upload/v2' )
177
- $Request.Query = $CodeCovParams.ToString ()
178
- Invoke-RestMethod - Uri $Request.Uri - Method Post - InFile $CodecovReport - ContentType ' multipart/form-data'
162
+ function Get-PesterTestVersion ($testFilePath ) {
163
+ $testFileContent = Get-Content - Path $testFilePath - Raw
164
+ if ($testFileContent -Like ' *#pester5*' )
165
+ {
166
+ return ' 5'
167
+ }
168
+ return ' 4'
179
169
}
180
170
181
171
@@ -188,15 +178,19 @@ if (-not $Finalize) {
188
178
# Run a test with the current version of PowerShell
189
179
# Make things faster by removing most output
190
180
if (-not $Finalize ) {
191
- Import-Module Pester
192
- Write-Host - Object " appveyor.pester: Running with Pester Version $ ( (Get-Command Invoke-Pester - ErrorAction SilentlyContinue).Version) " - ForegroundColor DarkGreen
193
181
Set-Variable ProgressPreference - Value SilentlyContinue
194
182
if ($AllScenarioTests.Count -eq 0 ) {
195
183
Write-Host - ForegroundColor DarkGreen " Nothing to do in this scenario"
196
184
return
197
185
}
186
+ # Remove any previously loaded pester module
187
+ Remove-Module - Name pester - ErrorAction SilentlyContinue
188
+ # Import pester 4
189
+ Import-Module pester - RequiredVersion 4.4 .2
190
+ Write-Host - Object " appveyor.pester: Running with Pester Version $ ( (Get-Command Invoke-Pester - ErrorAction SilentlyContinue).Version) " - ForegroundColor DarkGreen
198
191
# invoking a single invoke-pester consumes too much memory, let's go file by file
199
192
$AllTestsWithinScenario = Get-ChildItem - File - Path $AllScenarioTests
193
+ # start the round for pester 4 tests
200
194
$Counter = 0
201
195
foreach ($f in $AllTestsWithinScenario ) {
202
196
$Counter += 1
@@ -205,6 +199,13 @@ if (-not $Finalize) {
205
199
' Show' = ' None'
206
200
' PassThru' = $true
207
201
}
202
+ # get if this test should run on pester 4 or pester 5
203
+ $pesterVersionToUse = Get-PesterTestVersion - testFilePath $f.FullName
204
+ if ($pesterVersionToUse -eq ' 5' ) {
205
+ # we're in the "region" of pester 4, so skip
206
+ continue
207
+ }
208
+
208
209
# opt-in
209
210
if ($IncludeCoverage ) {
210
211
$CoverFiles = Get-CoverageIndications - Path $f - ModuleBase $ModuleBase
@@ -218,7 +219,7 @@ if (-not $Finalize) {
218
219
if ($trialNo -eq 1 ) {
219
220
$appvTestName = $f.Name
220
221
} else {
221
- $appvTestName = " $f .Name, attempt #$trialNo "
222
+ $appvTestName = " $ ( $ f.Name ) , attempt #$trialNo "
222
223
}
223
224
Add-AppveyorTest - Name $appvTestName - Framework NUnit - FileName $f.FullName - Outcome Running
224
225
$PesterRun = Invoke-Pester @PesterSplat
@@ -233,6 +234,56 @@ if (-not $Finalize) {
233
234
}
234
235
}
235
236
}
237
+
238
+ # start the round for pester 5 tests
239
+ # Remove any previously loaded pester module
240
+ Remove-Module - Name pester - ErrorAction SilentlyContinue
241
+ # Import pester 4
242
+ Import-Module pester - RequiredVersion 5.6 .1
243
+ Write-Host - Object " appveyor.pester: Running with Pester Version $ ( (Get-Command Invoke-Pester - ErrorAction SilentlyContinue).Version) " - ForegroundColor DarkGreen
244
+ $Counter = 0
245
+ foreach ($f in $AllTestsWithinScenario ) {
246
+ $Counter += 1
247
+
248
+ # get if this test should run on pester 4 or pester 5
249
+ $pesterVersionToUse = Get-PesterTestVersion - testFilePath $f.FullName
250
+ if ($pesterVersionToUse -eq ' 4' ) {
251
+ # we're in the "region" of pester 5, so skip
252
+ continue
253
+ }
254
+ $pester5Config = New-PesterConfiguration
255
+ $pester5Config.Run.Path = $f.FullName
256
+ $pester5config.Run.PassThru = $true
257
+ # opt-in
258
+ if ($IncludeCoverage ) {
259
+ $CoverFiles = Get-CoverageIndications - Path $f - ModuleBase $ModuleBase
260
+ $pester5Config.CodeCoverage.Enabled = $true
261
+ $pester5Config.CodeCoverage.Path = $CoverFiles
262
+ $pester5Config.CodeCoverage.OutputFormat = ' JaCoCo'
263
+ $pester5Config.CodeCoverage.OutputPath = " $ModuleBase \Pester5Coverage$PSVersion$Counter .xml"
264
+ }
265
+
266
+ $trialNo = 1
267
+ while ($trialNo -le 3 ) {
268
+ if ($trialNo -eq 1 ) {
269
+ $appvTestName = $f.Name
270
+ } else {
271
+ $appvTestName = " $ ( $f.Name ) , attempt #$trialNo "
272
+ }
273
+ Add-AppveyorTest - Name $appvTestName - Framework NUnit - FileName $f.FullName - Outcome Running
274
+ $PesterRun = Invoke-Pester - Configuration $pester5config
275
+ $PesterRun | Export-Clixml - Path " $ModuleBase \Pester5Results$PSVersion$Counter .xml"
276
+ $outcome = " Passed"
277
+ if ($PesterRun.FailedCount -gt 0 ) {
278
+ $trialno += 1
279
+ Update-AppveyorTest - Name $appvTestName - Framework NUnit - FileName $f.FullName - Outcome " Failed" - Duration $PesterRun.Time.TotalMilliseconds
280
+ } else {
281
+ Update-AppveyorTest - Name $appvTestName - Framework NUnit - FileName $f.FullName - Outcome " Passed" - Duration $PesterRun.Time.TotalMilliseconds
282
+ break
283
+ }
284
+ }
285
+ }
286
+
236
287
# Gather support package as an artifact
237
288
# New-DbatoolsSupportPackage -Path $ModuleBase - turns out to be too heavy
238
289
try {
@@ -281,9 +332,10 @@ if (-not $Finalize) {
281
332
# $totalcount = $results | Select-Object -ExpandProperty TotalCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
282
333
$failedcount = $results | Select-Object - ExpandProperty FailedCount | Measure-Object - Sum | Select-Object - ExpandProperty Sum
283
334
if ($failedcount -gt 0 ) {
335
+ # pester 4 output
284
336
$faileditems = $results | Select-Object - ExpandProperty TestResult | Where-Object { $_.Passed -notlike $True }
285
337
if ($faileditems ) {
286
- Write-Warning " Failed tests summary:"
338
+ Write-Warning " Failed tests summary (pester 4) :"
287
339
$faileditems | ForEach-Object {
288
340
$name = $_.Name
289
341
[pscustomobject ]@ {
@@ -297,8 +349,30 @@ if (-not $Finalize) {
297
349
throw " $failedcount tests failed."
298
350
}
299
351
}
352
+
353
+
354
+ $results5 = @ (Get-ChildItem - Path " $ModuleBase \Pester5Results*.xml" | Import-Clixml )
355
+ # pester 5 output
356
+ $faileditems = $results5 | Select-Object - ExpandProperty Tests | Where-Object { $_.Passed -notlike $True }
357
+ if ($faileditems ) {
358
+ Write-Warning " Failed tests summary (pester 5):"
359
+ $faileditems | ForEach-Object {
360
+ $name = $_.Name
361
+ [pscustomobject ]@ {
362
+ Path = $_.Path -Join ' /'
363
+ Name = " It $name "
364
+ Result = $_.Result
365
+ Message = $_.ErrorRecord -Join " "
366
+ }
367
+ } | Sort-Object Path, Name, Result, Message | Format-List
368
+ throw " $failedcount tests failed."
369
+ }
370
+
300
371
# opt-in
301
372
if ($IncludeCoverage ) {
373
+ # for now, this manages recreating a codecov-ingestable format for pester 4. Pester 5 uses JaCoCo natively, which
374
+ # codecov accepts ... there's only the small matter that we generate one coverage per run, and there's a run per test file
375
+ # and there's no native-powershelly-way to merge JaCoCo reports. Let's start small, and complicate our lives farther down the line.
302
376
$CodecovReport = Get-CodecovReport - Results $results - ModuleBase $ModuleBase
303
377
$CodecovReport | ConvertTo-Json - Depth 4 - Compress | Out-File - FilePath " $ModuleBase \PesterResultsCoverage.json" - Encoding utf8
304
378
}
0 commit comments