Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Functions/Assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -83,11 +83,13 @@ function Should {
$testFailed = Get-TestResult $parsedArgs $value

if ($testFailed) {
$ShouldExceptionLineText = $MyInvocation.Line.TrimEnd("`n")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output looks pretty good now. Do you think we should just call .Trim() instead of only trimming the trailing newlines? At the moment, the line's indentation is creating some space (which may not be consistent between tests):

Describing Testing
 [-] Fails 2.08s
   Expected: {False}
   But was:  {True}
   at line: 3 in C:\Users\dlwya_000\desktop\test.Tests.ps1
   3:         $true | Should Be $false

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it and I think preserving original indent is a good thing.
One of the problems in current implementation is multi-line statement

It "should foo" {
   Invoke-Foo -ScriptBlock {
   ...
   } | Should be 'foo'
}

One possible improvement is to detect this the whole pipeline statement and show multi-line. Then preserving indents will be mandatory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. In the short term, I plan to refactor any tests that have long, multi-line Should assertions, and assign the result to a descriptive variable name. That way my output will always be tidy: ##: $somethingMeaningful | Should Be $somethingCorrect

$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)
}
Expand Down
11 changes: 8 additions & 3 deletions Functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down