Skip to content

Commit

Permalink
Handle when exceptions have no error messages. (pester#1382)
Browse files Browse the repository at this point in the history
* Handle when exceptions have no error messages.
  • Loading branch information
rasmusjelsgaard authored and nohwnd committed Oct 8, 2019
1 parent b466a53 commit 2f5b40c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion Functions/Output.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Set-StrictMode -Version Latest
Set-StrictMode -Version Latest

InModuleScope -ModuleName Pester -ScriptBlock {
Describe 'Has-Flag' -Fixture {
Expand Down Expand Up @@ -367,6 +367,39 @@ InModuleScope -ModuleName Pester -ScriptBlock {
}
}
}

Context 'Exceptions with no error message property set' {
$powershellVersion = $($PSVersionTable.PSVersion.Major)
try {
$exceptionWithNullMessage = New-Object -TypeName "System.Management.Automation.ParentContainsErrorRecordException"
throw $exceptionWithNullMessage
}
catch {
$exception = $_
}
$result = $exception | ConvertTo-FailureLines

if ($powershellVersion -lt 3) {
# Necessary because Microsoft changed the behaviour of System.Management.Automation.ParentContainsErrorRecordException at this point.
It 'produces correct message lines' {
$result.Message.Length | Should -Be 2
}

It 'produces correct trace line' {
$result.Trace.Count | Should -Be 1
}
}
else {
It 'produces correct message lines' {
$result.Message.Length | Should -Be 0
}

It 'produces correct trace line' {
$result.Trace.Count | Should -Be 1
}
}
}

}
}
}
2 changes: 1 addition & 1 deletion Functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function ConvertTo-FailureLines {
while ($exception) {
$exceptionName = $exception.GetType().Name
$thisLines = $exception.Message.Split([string[]]($([System.Environment]::NewLine), "\n", "`n"), [System.StringSplitOptions]::RemoveEmptyEntries)
if ($ErrorRecord.FullyQualifiedErrorId -ne 'PesterAssertionFailed') {
if ($ErrorRecord.FullyQualifiedErrorId -ne 'PesterAssertionFailed' -and $thisLines.Length -gt 0) {
$thisLines[0] = "$exceptionName`: $($thisLines[0])"
}
[array]::Reverse($thisLines)
Expand Down

0 comments on commit 2f5b40c

Please sign in to comment.