Skip to content

Commit

Permalink
Fix for stack trace reporting in Should.
Browse files Browse the repository at this point in the history
Fixes pester#357 .  Instead of reporting the file associated with the script block passed to the It command, the code now uses $MyInvocation.ScriptName in the call to Should.
  • Loading branch information
dlwyatt committed May 22, 2015
1 parent 77cfa31 commit cd383cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Functions/Assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ function Get-FailureMessage($shouldArgs, $value) {

return (& $failureMessageFunction $value $shouldArgs.ExpectedValue)
}
function New-ShouldErrorRecord ([string] $Message, [string] $Line, [string] $LineText) {
function New-ShouldErrorRecord ([string] $Message, [string] $File, [string] $Line, [string] $LineText) {
$exception = New-Object Exception $Message
$errorID = 'PesterAssertionFailed'
$errorCategory = [Management.Automation.ErrorCategory]::InvalidResult
# we use ErrorRecord.TargetObject to pass structured information about the error to a reporting system.
$targetObject = @{Message = $Message; Line = $Line; LineText = $LineText}
$targetObject = @{Message = $Message; File = $File; Line = $Line; LineText = $LineText}
$errorRecord = New-Object Management.Automation.ErrorRecord $exception, $errorID, $errorCategory, $targetObject
return $errorRecord
}
Expand All @@ -85,10 +85,11 @@ function Should {
if ($testFailed) {
$lineText = $MyInvocation.Line.TrimEnd("`n")
$line = $MyInvocation.ScriptLineNumber
$file = $MyInvocation.ScriptName

$failureMessage = Get-FailureMessage $parsedArgs $value

throw ( New-ShouldErrorRecord -Message $failureMessage -Line $line -LineText $lineText)
throw ( New-ShouldErrorRecord -Message $failureMessage -File $file -Line $line -LineText $lineText)
}
} until ($input.MoveNext() -eq $false)
}
Expand Down
13 changes: 7 additions & 6 deletions Functions/It.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
Set-StrictMode -Version Latest

$thisScriptRegex = [regex]::Escape($MyInvocation.ScriptName)

InModuleScope Pester {
Describe 'Get-PesterResult' {
It 'records the correct stack line number of failed tests in the Tests file' {
#the $script scriptblock below is used as a position marker to determine
#on which line the test failed.
$errorRecord = $null
try{'something' | should be 'nothing'}catch{ $errorRecord=$_} ; $script={}
$result = Get-PesterResult $script 0 $errorRecord
$result.Stacktrace | should match "at line: $($script.startPosition.StartLine) in "
$result = Get-PesterResult 0 $errorRecord
$result.Stacktrace | should match "at line: $($script.startPosition.StartLine) in $thisScriptRegex"
}

It 'Does not modify the error message from the original exception' {
Expand All @@ -17,9 +19,9 @@ InModuleScope Pester {
Add-Member -InputObject $object -MemberType ScriptMethod -Name ThrowSomething -Value { throw $message }

$errorRecord = $null
try { $object.ThrowSomething() } catch { $errorRecord = $_ }; $script = {}
try { $object.ThrowSomething() } catch { $errorRecord = $_ }

$pesterResult = Get-PesterResult $script 0 $errorRecord
$pesterResult = Get-PesterResult 0 $errorRecord

$pesterResult.FailureMessage | Should Be $errorRecord.Exception.Message
}
Expand Down Expand Up @@ -199,7 +201,6 @@ InModuleScope Pester {
Describe 'Get-PesterResult - Part 2' {
It 'records the correct stack line number of failed tests in another file' {
$errorRecord = $null
$script = {}

$testPath = Join-Path $TestDrive test.ps1
$escapedTestPath = [regex]::Escape($testPath)
Expand All @@ -219,7 +220,7 @@ Describe 'Get-PesterResult - Part 2' {
# That was throwing off the test results at first, because the error record we got was due to the invalid (null) value being passed to -ErrorAction.

$cmd = InModuleScope Pester { ${function:Get-PesterResult} }
$result = & $cmd $script 0 $errorRecord
$result = & $cmd 0 $errorRecord

$result.Stacktrace | should match "at line: 2 in $escapedTestPath"
}
Expand Down
11 changes: 5 additions & 6 deletions Functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function Invoke-Test
}


$result = Get-PesterResult -Test $ScriptBlock -ErrorRecord $errorRecord
$result = Get-PesterResult -ErrorRecord $errorRecord
$orderedParameters = Get-OrderedParameterDictionary -ScriptBlock $ScriptBlock -Dictionary $Parameters
$Pester.AddTestResult( $result.name, $result.Result, $null, $result.FailureMessage, $result.StackTrace, $ParameterizedSuiteName, $orderedParameters )
Write-Progress -Activity "Running test '$Name'" -Completed -Status Processing
Expand All @@ -288,7 +288,6 @@ function Invoke-Test

function Get-PesterResult {
param(
[ScriptBlock] $Test,
[Nullable[TimeSpan]] $Time,
[System.Management.Automation.ErrorRecord] $ErrorRecord
)
Expand All @@ -314,10 +313,10 @@ function Get-PesterResult {
# we use TargetObject to pass structured information about the error.
$details = $ErrorRecord.TargetObject

$failureMessage = $details.message
$file = $test.File
$line = $details.line
$lineText = "`n$line`: $($details.linetext)"
$failureMessage = $details.Message
$file = $details.File
$line = $details.Line
$lineText = "`n$line`: $($details.LineText)"
}
else
{
Expand Down

0 comments on commit cd383cb

Please sign in to comment.