Skip to content

Commit dd0f4c0

Browse files
authored
Update Error-state and counts (#1220)
1 parent a46a0f5 commit dd0f4c0

3 files changed

Lines changed: 57 additions & 26 deletions

File tree

powershell/internal/ConvertTo-MtMaesterResult.ps1

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,21 @@ function ConvertTo-MtMaesterResult {
133133
$severity = $testSetting.Severity
134134
}
135135

136-
# Setting Result to Error, Overwriting the Skipped state
137-
if($testResultDetail.TestSkipped -eq "Error" ) {
138-
$result = "Error"
139-
} else {
140-
$result = $test.Result
136+
# Setting Result to Error, Overwriting the Skipped state
137+
if ($testResultDetail.TestSkipped -eq "Error" ) {
138+
$result = "Error"
139+
} elseif ((
140+
$test -and
141+
$test.ErrorRecord -and
142+
$test.ErrorRecord.Count -gt 0 -and
143+
$test.ErrorRecord[0].CategoryInfo -and
144+
$test.ErrorRecord[0].CategoryInfo.Reason) -and
145+
(@("RuntimeException","ParameterBindingValidationException","HttpRequestException","TaskCanceledException") -Contains $test.ErrorRecord[0].CategoryInfo.Reason )) {
146+
Write-Verbose "Setting result=Error $($name) because: $($test.ErrorRecord[0].CategoryInfo.Reason)"
147+
$result = "Error"
148+
}
149+
else {
150+
$result = $test.Result
141151
}
142152

143153
$timeSpanFormat = 'hh\:mm\:ss'
@@ -147,7 +157,7 @@ function ConvertTo-MtMaesterResult {
147157
Title = $testTitle
148158
Name = $name
149159
HelpUrl = $helpUrl
150-
Severity = $severity
160+
Severity = $severity
151161
Tag = @($test.Block.Tag + $test.Tag | Select-Object -Unique)
152162
Result = $result
153163
ScriptBlock = $test.ScriptBlock.ToString()
@@ -159,41 +169,57 @@ function ConvertTo-MtMaesterResult {
159169
}
160170
$mtTests += $mtTestInfo
161171
}
172+
# Count all Passed, Failed, Skipped, Error, NotRun and Total results
173+
$Recount = [PSCustomObject]@{
174+
FailedCount = 0
175+
PassedCount = 0
176+
SkippedCount = 0
177+
NotRunCount = 0
178+
ErrorCount = 0
179+
TotalCount = 0
180+
}
181+
$Recount.FailedCount = @($mtTests | Where-Object { $_.Result -eq 'Failed' }).Count
182+
$Recount.PassedCount = @($mtTests | Where-Object { $_.Result -eq 'Passed' }).Count
183+
$Recount.ErrorCount = @($mtTests | Where-Object { $_.Result -eq 'Error' }).Count
184+
$Recount.SkippedCount = @($mtTests | Where-Object { $_.Result -eq 'Skipped' }).Count
185+
$Recount.NotRunCount = @($mtTests | Where-Object { $_.Result -eq 'NotRun' }).Count
186+
$Recount.TotalCount = $mtTests.Count
187+
Write-Verbose "Recount: $($Recount | Out-String)"
162188

163189
$mtBlocks = @()
164190
foreach ($container in $PesterResults.Containers) {
165191

166192
foreach ($block in $container.Blocks) {
167193
$mtBlockInfo = $mtBlocks | Where-Object { $_.Name -eq $block.Name }
168194
if ($null -eq $mtBlockInfo) {
195+
Write-Verbose "Recalculating block: $($block.Name)"
169196
$mtBlockInfo = [PSCustomObject]@{
170197
Name = $block.Name
171198
Result = $block.Result
172-
FailedCount = $block.FailedCount
173-
PassedCount = $block.PassedCount
174-
SkippedCount = $block.SkippedCount
175-
NotRunCount = $block.NotRunCount
176-
TotalCount = $block.TotalCount
199+
FailedCount = @($mtTests | Where-Object { $_.Result -eq 'Failed' -and $_.Block -eq $block.name }).Count
200+
PassedCount = @($mtTests | Where-Object { $_.Result -eq 'Passed' -and $_.Block -eq $block.name }).Count
201+
ErrorCount = @($mtTests | Where-Object { $_.Result -eq 'Error' -and $_.Block -eq $block.name }).Count
202+
SkippedCount = @($mtTests | Where-Object { $_.Result -eq 'Skipped' -and $_.Block -eq $block.name }).Count
203+
NotRunCount = @($mtTests | Where-Object { $_.Result -eq 'NotRun' -and $_.Block -eq $block.name }).Count
204+
TotalCount = @($mtTests | Where-Object { $_.Block -eq $block.name }).Count
177205
Tag = $block.Tag
178206
}
179207
$mtBlocks += $mtBlockInfo
180-
} else {
181-
$mtBlockInfo.FailedCount += $block.FailedCount
182-
$mtBlockInfo.PassedCount += $block.PassedCount
183-
$mtBlockInfo.SkippedCount += $block.SkippedCount
184-
$mtBlockInfo.NotRunCount += $block.NotRunCount
185-
$mtBlockInfo.TotalCount += $block.TotalCount
208+
}
209+
else {
210+
# We already seen and counted all blocks
186211
}
187212
}
188213
}
189214

190215
$mtTestResults = [PSCustomObject]@{
191216
Result = $PesterResults.Result
192-
FailedCount = $PesterResults.FailedCount
193-
PassedCount = $PesterResults.PassedCount
194-
SkippedCount = $PesterResults.SkippedCount
195-
NotRunCount = $PesterResults.NotRunCount
196-
TotalCount = $PesterResults.TotalCount
217+
FailedCount = $Recount.FailedCount
218+
PassedCount = $Recount.PassedCount
219+
ErrorCount = $Recount.ErrorCount
220+
SkippedCount = $Recount.SkippedCount
221+
NotRunCount = $Recount.NotRunCount
222+
TotalCount = $Recount.TotalCount
197223
ExecutedAt = GetFormattedDate($PesterResults.ExecutedAt)
198224
TotalDuration = $PesterResults.Duration.ToString($timeSpanFormat)
199225
UserDuration = $PesterResults.UserDuration.ToString($timeSpanFormat)

powershell/public/Add-MtTestResultDetail.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ function Add-MtTestResultDetail {
103103
if ($SkippedError) {
104104
$SkippedReason += "`n`n" + '```' + "`n`n" + ($SkippedError | Out-String) + "`n`n" + '```' + "`n`n"
105105
}
106+
if ([string]::IsNullOrEmpty($Result)) {
107+
$Result = "Error. $SkippedReason"
108+
}
106109
} else {
107110
$SkippedReason = Get-MtSkippedReason $SkippedBecause
108111
}

powershell/public/Invoke-Maester.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,12 @@ function Invoke-Maester {
488488

489489
if ($Verbosity -eq 'None') {
490490
# Show final summary.
491-
Write-Host "`nTests Passed ✅: $($pesterResults.PassedCount), " -NoNewline -ForegroundColor Green
492-
Write-Host "Failed ❌: $($pesterResults.FailedCount), " -NoNewline -ForegroundColor Red
493-
Write-Host "Skipped ⚫: $($pesterResults.SkippedCount) " -NoNewline -ForegroundColor DarkGray
494-
Write-Host "Not Run ⚫: $($pesterResults.NotRunCount)`n" -ForegroundColor DarkGray
491+
Write-Host "`nTests Passed ✅: $($maesterResults.PassedCount), " -NoNewline -ForegroundColor Green
492+
Write-Host "Failed ❌: $($maesterResults.FailedCount), " -NoNewline -ForegroundColor Red
493+
Write-Host "Skipped ⚫: $($maesterResults.SkippedCount), " -NoNewline -ForegroundColor DarkGray
494+
Write-Host "Error ⚠️: $($maesterResults.ErrorCount), " -NoNewline -ForegroundColor DarkGray
495+
Write-Host "Not Run ⚫: $($maesterResults.NotRunCount), " -NoNewline -ForegroundColor DarkGray
496+
Write-Host "Total ⭐: $($maesterResults.TotalCount)`n"
495497
}
496498

497499
if (-not $SkipVersionCheck -and 'Next' -ne $version) {

0 commit comments

Comments
 (0)