From 2ce2226dd50c8b3f8b2e1193e507999793a66c38 Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Fri, 23 Jan 2015 16:27:54 -0800 Subject: [PATCH 1/4] Add one-line snippet from failure to provide a context --- Functions/Assertions/Should.ps1 | 9 ++++++--- Functions/It.ps1 | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Functions/Assertions/Should.ps1 b/Functions/Assertions/Should.ps1 index 6667ee85e..aed14ac43 100644 --- a/Functions/Assertions/Should.ps1 +++ b/Functions/Assertions/Should.ps1 @@ -59,12 +59,13 @@ 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.ErrorDetails = @{message = $Message; line = $line; linetext = $LineText} | ConvertTo-Json -Compress + #"$Message failed at line: $line : $LineText" $errorRecord } @@ -83,11 +84,13 @@ function Should { $testFailed = Get-TestResult $parsedArgs $value if ($testFailed) { + $ShouldExceptionLineText = $MyInvocation.Line $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..16e0e57a4 100644 --- a/Functions/It.ps1 +++ b/Functions/It.ps1 @@ -274,9 +274,12 @@ function Get-PesterResult { if ($exception.FullyQualifiedErrorID -eq 'PesterAssertionFailed') { - $failureMessage = $exception.exception.message + $details = $exception.ErrorDetails.message | ConvertFrom-Json + + $failureMessage = $details.message $file = $test.File - $line = if ( $exception.ErrorDetails.message -match "\d+$" ) { $matches[0] } + $line = $details.line + $lineText = $details.linetext } else { $failureMessage = $exception.ToString() @@ -285,7 +288,7 @@ function Get-PesterResult { } $testResult.failureMessage = $failureMessage -replace "Exception calling", "Assert failed on" - $testResult.stackTrace = "at line: $line in $file" + $testResult.stackTrace = "at line: $line in $file`n`n$line`:$linetext" return $testResult } From faf87ddc915ba67a1dea0dc60bc49e46cac4cb3c Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Sun, 25 Jan 2015 12:42:47 -0800 Subject: [PATCH 2/4] Use TargetObject for failure details to avoid serialization, which is not available on PS v2 --- Functions/Assertions/Should.ps1 | 12 ++++++------ Functions/It.ps1 | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Functions/Assertions/Should.ps1 b/Functions/Assertions/Should.ps1 index aed14ac43..3e5827bab 100644 --- a/Functions/Assertions/Should.ps1 +++ b/Functions/Assertions/Should.ps1 @@ -63,11 +63,11 @@ 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 = $Message; line = $line; linetext = $LineText} | ConvertTo-Json -Compress - #"$Message failed at line: $line : $LineText" - - $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 + $errorRecord.ErrorDetails = "$Message failed at line: $line : $LineText" + return $errorRecord } function Should { @@ -84,7 +84,7 @@ function Should { $testFailed = Get-TestResult $parsedArgs $value if ($testFailed) { - $ShouldExceptionLineText = $MyInvocation.Line + $ShouldExceptionLineText = $MyInvocation.Line.TrimEnd("`n") $ShouldExceptionLine = $MyInvocation.ScriptLineNumber $failureMessage = Get-FailureMessage $parsedArgs $value diff --git a/Functions/It.ps1 b/Functions/It.ps1 index 16e0e57a4..9821dca24 100644 --- a/Functions/It.ps1 +++ b/Functions/It.ps1 @@ -274,7 +274,8 @@ function Get-PesterResult { if ($exception.FullyQualifiedErrorID -eq 'PesterAssertionFailed') { - $details = $exception.ErrorDetails.message | ConvertFrom-Json + # we use TargetObject to pass structured information about the error. + $details = $exception.TargetObject $failureMessage = $details.message $file = $test.File @@ -288,7 +289,7 @@ function Get-PesterResult { } $testResult.failureMessage = $failureMessage -replace "Exception calling", "Assert failed on" - $testResult.stackTrace = "at line: $line in $file`n`n$line`:$linetext" + $testResult.stackTrace = "at line: $line in $file`n$line`: $linetext" return $testResult } From 95c7af19c1366bd1bfba2b129e096ae025af6305 Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Sun, 25 Jan 2015 13:14:06 -0800 Subject: [PATCH 3/4] Remove unused ErrorDetails --- Functions/Assertions/Should.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Functions/Assertions/Should.ps1 b/Functions/Assertions/Should.ps1 index 3e5827bab..c611e6018 100644 --- a/Functions/Assertions/Should.ps1 +++ b/Functions/Assertions/Should.ps1 @@ -66,7 +66,6 @@ function New-ShouldException ($Message, $Line, $LineText) { # 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 - $errorRecord.ErrorDetails = "$Message failed at line: $line : $LineText" return $errorRecord } From 8c3fcbe792fc2fa2f5f1d941cecad23a0aa1b81f Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Mon, 26 Jan 2015 11:43:44 -0800 Subject: [PATCH 4/4] fix non-Should exception error reporting --- Functions/It.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Functions/It.ps1 b/Functions/It.ps1 index 9821dca24..0ee571067 100644 --- a/Functions/It.ps1 +++ b/Functions/It.ps1 @@ -280,16 +280,17 @@ function Get-PesterResult { $failureMessage = $details.message $file = $test.File $line = $details.line - $lineText = $details.linetext + $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`n$line`: $linetext" + $testResult.stackTrace = "at line: $line in ${file}${lineText}" return $testResult }