diff --git a/Functions/Output.Tests.ps1 b/Functions/Output.Tests.ps1 index e5f375fc9..b54db0342 100644 --- a/Functions/Output.Tests.ps1 +++ b/Functions/Output.Tests.ps1 @@ -1,4 +1,4 @@ -Set-StrictMode -Version Latest +Set-StrictMode -Version Latest InModuleScope -ModuleName Pester -ScriptBlock { Describe 'Has-Flag' -Fixture { @@ -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 + } + } + } + } } } diff --git a/Functions/Output.ps1 b/Functions/Output.ps1 index 449150343..16627f26a 100644 --- a/Functions/Output.ps1 +++ b/Functions/Output.ps1 @@ -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)