diff --git a/Functions/Assertions/Should.ps1 b/Functions/Assertions/Should.ps1 index 6667ee85e..c611e6018 100644 --- a/Functions/Assertions/Should.ps1 +++ b/Functions/Assertions/Should.ps1 @@ -59,14 +59,14 @@ function Get-FailureMessage($shouldArgs, $value) { return (& $failureMessageFunction $value $shouldArgs.ExpectedValue) } -function New-ShouldException ($Message,$Line) { +function New-ShouldException ($Message, $Line, $LineText) { $exception = New-Object Exception $Message $errorID = 'PesterAssertionFailed' $errorCategory = [Management.Automation.ErrorCategory]::InvalidResult - $errorRecord = New-Object Management.Automation.ErrorRecord $exception, $errorID, $errorCategory, $null - $errorRecord.ErrorDetails = "$Message failed at line: $line" - - $errorRecord + # we use ErrorRecord.TargetObject to pass structured information about the error to a reporting system. + $targetObject = @{message = $Message; line = $line; linetext = $LineText} + $errorRecord = New-Object Management.Automation.ErrorRecord $exception, $errorID, $errorCategory, $targetObject + return $errorRecord } function Should { @@ -83,11 +83,13 @@ function Should { $testFailed = Get-TestResult $parsedArgs $value if ($testFailed) { + $ShouldExceptionLineText = $MyInvocation.Line.TrimEnd("`n") $ShouldExceptionLine = $MyInvocation.ScriptLineNumber + $failureMessage = Get-FailureMessage $parsedArgs $value - throw ( New-ShouldException -Message $failureMessage -Line $ShouldExceptionLine ) + throw ( New-ShouldException -Message $failureMessage -Line $ShouldExceptionLine -LineText $ShouldExceptionLineText) } } until ($input.MoveNext() -eq $false) } diff --git a/Functions/It.ps1 b/Functions/It.ps1 index fe0922c3a..0ee571067 100644 --- a/Functions/It.ps1 +++ b/Functions/It.ps1 @@ -274,18 +274,23 @@ function Get-PesterResult { if ($exception.FullyQualifiedErrorID -eq 'PesterAssertionFailed') { - $failureMessage = $exception.exception.message + # we use TargetObject to pass structured information about the error. + $details = $exception.TargetObject + + $failureMessage = $details.message $file = $test.File - $line = if ( $exception.ErrorDetails.message -match "\d+$" ) { $matches[0] } + $line = $details.line + $lineText = "`n$line`: $($details.linetext)" } else { $failureMessage = $exception.ToString() $file = $Exception.InvocationInfo.ScriptName $line = $Exception.InvocationInfo.ScriptLineNumber + $lineText = '' } $testResult.failureMessage = $failureMessage -replace "Exception calling", "Assert failed on" - $testResult.stackTrace = "at line: $line in $file" + $testResult.stackTrace = "at line: $line in ${file}${lineText}" return $testResult }