Skip to content

Commit 0083d57

Browse files
authored
Tests/pester5runner (#9521)
1 parent 50b1d2b commit 0083d57

File tree

4 files changed

+114
-31
lines changed

4 files changed

+114
-31
lines changed

appveyor.yml

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ configuration: "Debug"
44

55
build_script:
66
- ps: Set-Service wuauserv -StartupType Manual #otherwise, choco command exits with code 1058
7-
- ps: choco install dotnet-5.0-sdk | Out-String | Out-Null
8-
- ps: choco install dotnetcore-sdk | Out-String | Out-Null
9-
# - ps: Push-Location bin\projects\dbatools; dotnet build ;Pop-Location
107

118
version: 2.1.{build}
129

tests/appveyor.pester.ps1

+96-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This script will invoke Pester tests, then serialize XML results and pull them in appveyor.yml
44
55
.DESCRIPTION
6-
Internal function that creates SMO server object.
6+
Internal function that runs pester tests
77
88
.PARAMETER Finalize
99
If Finalize is specified, we collect XML output, upload tests, and indicate build errors
@@ -159,23 +159,13 @@ function Get-CodecovReport($Results, $ModuleBase) {
159159
$newreport
160160
}
161161

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'
179169
}
180170

181171

@@ -188,15 +178,19 @@ if (-not $Finalize) {
188178
#Run a test with the current version of PowerShell
189179
#Make things faster by removing most output
190180
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
193181
Set-Variable ProgressPreference -Value SilentlyContinue
194182
if ($AllScenarioTests.Count -eq 0) {
195183
Write-Host -ForegroundColor DarkGreen "Nothing to do in this scenario"
196184
return
197185
}
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
198191
# invoking a single invoke-pester consumes too much memory, let's go file by file
199192
$AllTestsWithinScenario = Get-ChildItem -File -Path $AllScenarioTests
193+
#start the round for pester 4 tests
200194
$Counter = 0
201195
foreach ($f in $AllTestsWithinScenario) {
202196
$Counter += 1
@@ -205,6 +199,13 @@ if (-not $Finalize) {
205199
'Show' = 'None'
206200
'PassThru' = $true
207201
}
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+
208209
#opt-in
209210
if ($IncludeCoverage) {
210211
$CoverFiles = Get-CoverageIndications -Path $f -ModuleBase $ModuleBase
@@ -218,7 +219,7 @@ if (-not $Finalize) {
218219
if ($trialNo -eq 1) {
219220
$appvTestName = $f.Name
220221
} else {
221-
$appvTestName = "$f.Name, attempt #$trialNo"
222+
$appvTestName = "$($f.Name), attempt #$trialNo"
222223
}
223224
Add-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome Running
224225
$PesterRun = Invoke-Pester @PesterSplat
@@ -233,6 +234,56 @@ if (-not $Finalize) {
233234
}
234235
}
235236
}
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+
236287
# Gather support package as an artifact
237288
# New-DbatoolsSupportPackage -Path $ModuleBase - turns out to be too heavy
238289
try {
@@ -281,9 +332,10 @@ if (-not $Finalize) {
281332
#$totalcount = $results | Select-Object -ExpandProperty TotalCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
282333
$failedcount = $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
283334
if ($failedcount -gt 0) {
335+
# pester 4 output
284336
$faileditems = $results | Select-Object -ExpandProperty TestResult | Where-Object { $_.Passed -notlike $True }
285337
if ($faileditems) {
286-
Write-Warning "Failed tests summary:"
338+
Write-Warning "Failed tests summary (pester 4):"
287339
$faileditems | ForEach-Object {
288340
$name = $_.Name
289341
[pscustomobject]@{
@@ -297,8 +349,30 @@ if (-not $Finalize) {
297349
throw "$failedcount tests failed."
298350
}
299351
}
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+
300371
#opt-in
301372
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.
302376
$CodecovReport = Get-CodecovReport -Results $results -ModuleBase $ModuleBase
303377
$CodecovReport | ConvertTo-Json -Depth 4 -Compress | Out-File -FilePath "$ModuleBase\PesterResultsCoverage.json" -Encoding utf8
304378
}

tests/appveyor.post.ps1

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
Add-AppveyorTest -Name "appveyor.post" -Framework NUnit -FileName "appveyor.post.ps1" -Outcome Running
22
$sw = [system.diagnostics.stopwatch]::startNew()
3-
Write-Host -Object "appveyor.post: Sending coverage data" -ForeGroundColor DarkGreen
3+
Write-Host -Object "appveyor.post: Sending coverage data (pester 4)" -ForeGroundColor DarkGreen
44
Push-AppveyorArtifact PesterResultsCoverage.json -FileName "PesterResultsCoverage"
55
codecov -f PesterResultsCoverage.json --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null
6+
7+
Write-Host -Object "appveyor.post: Sending coverage data (pester 5)" -ForeGroundColor DarkGreen
8+
$ProjectRoot = $env:APPVEYOR_BUILD_FOLDER,
9+
$ModuleBase = $ProjectRoot,
10+
$pester5CoverageFiles = Get-ChildItem -Path "$ModuleBase\Pester5Coverage*.xml"
11+
foreach($coverageFile in $pester5CoverageFiles)
12+
{
13+
Push-AppveyorArtifact $coverageFile.FullName -FileName $coverageFile.Name
14+
codecov -f $coverageFile.FullName --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null
15+
}
16+
617
$sw.Stop()
718
Update-AppveyorTest -Name "appveyor.post" -Framework NUnit -FileName "appveyor.post.ps1" -Outcome Passed -Duration $sw.ElapsedMilliseconds

tests/appveyor.prep.ps1

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ git clone -q --branch=master --depth=1 https://github.com/dataplat/appveyor-lab.
1919
#Get codecov (to upload coverage results)
2020
Write-Host -Object "appveyor.prep: Install codecov" -ForegroundColor DarkGreen
2121
choco install codecov | Out-Null
22+
#FIXME : read about the new uploader https://docs.codecov.com/docs/codecov-uploader#using-the-uploader
2223

2324
#Get PSScriptAnalyzer (to check warnings)
2425
Write-Host -Object "appveyor.prep: Install PSScriptAnalyzer" -ForegroundColor DarkGreen
@@ -33,10 +34,14 @@ if (-not(Test-Path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library'
3334
}
3435

3536
#Get Pester (to run tests) - choco isn't working onall scenarios, weird
36-
Write-Host -Object "appveyor.prep: Install Pester" -ForegroundColor DarkGreen
37+
Write-Host -Object "appveyor.prep: Install Pester4" -ForegroundColor DarkGreen
3738
if (-not(Test-Path 'C:\Program Files\WindowsPowerShell\Modules\Pester\4.4.2')) {
3839
Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion 4.4.2 | Out-Null
3940
}
41+
Write-Host -Object "appveyor.prep: Install Pester5" -ForegroundColor DarkGreen
42+
if (-not(Test-Path 'C:\Program Files\WindowsPowerShell\Modules\Pester\5.6.1')) {
43+
Install-Module -Name Pester -Force -SkipPublisherCheck -RequiredVersion 5.6.1 | Out-Null
44+
}
4045

4146
#Setup DbatoolsConfig Path.DbatoolsExport path
4247
Write-Host -Object "appveyor.prep: Create Path.DbatoolsExport" -ForegroundColor DarkGreen
@@ -45,10 +50,6 @@ if (-not(Test-Path 'C:\Users\appveyor\Documents\DbatoolsExport')) {
4550
}
4651

4752

48-
#Get opencover.portable (to run DLL tests)
49-
Write-Host -Object "appveyor.prep: Install opencover.portable" -ForegroundColor DarkGreen
50-
choco install opencover.portable | Out-Null
51-
5253
Write-Host -Object "appveyor.prep: Trust SQL Server Cert (now required)" -ForegroundColor DarkGreen
5354
Import-Module dbatools.library
5455
Import-Module C:\github\dbatools\dbatools.psd1

0 commit comments

Comments
 (0)