Skip to content

Commit

Permalink
Fix Should -Throw to handle expected message with escaped wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Aug 23, 2024
1 parent 0d3dd66 commit 655f934
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/functions/assertions/PesterThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
# this is for Should -Not -Throw. Once *any* exception was thrown we should fail the assertion
# there is no point in filtering the exception, because there should be none
$succeeded = -not $actualExceptionWasThrown
if ($true -eq $succeeded) { return [Pester.ShouldResult]@{Succeeded = $succeeded } }
if ($true -eq $succeeded) {
return [Pester.ShouldResult]@{Succeeded = $succeeded }
}

$failureMessage = "Expected no exception to be thrown,$(Format-Because $Because) but an exception `"$actualExceptionMessage`" was thrown $actualExceptionLine."
return [Pester.ShouldResult] @{
Expand All @@ -96,7 +98,7 @@

$filterOnMessage = -not [string]::IsNullOrWhitespace($ExpectedMessage)
if ($filterOnMessage) {
$filters += "message like $(Format-Nicely $ExpectedMessage)"
$filters += "message like $(Format-Nicely [System.Management.Automation.WildcardPattern]::Unescape($ExpectedMessage))"
if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) {
$buts += "the message was $(Format-Nicely $actualExceptionMessage)"
}
Expand All @@ -120,7 +122,12 @@
$failureMessage = "Expected an exception$(if($filter) { " with $filter" }) to be thrown,$(Format-Because $Because) but $but. $actualExceptionLine".Trim()

$ActualValue = $actualExceptionMessage
$ExpectedValue = if ($filterOnExceptionType) { "type $(Format-Nicely $ExceptionType)" } else { 'any exception' }
$ExpectedValue = if ($filterOnExceptionType) {
"type $(Format-Nicely $ExceptionType)"
}
else {
'any exception'
}

return [Pester.ShouldResult] @{
Succeeded = $false
Expand Down
5 changes: 5 additions & 0 deletions tst/functions/assertions/PesterThrow.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ InPesterModuleScope {
$err.Exception.Message | Verify-Equal "Expected an exception with FullyQualifiedErrorId 'id' to be thrown, but no exception was thrown."
}

It "returns the correct assertion message when message filter is used and contain escaped wildcard character" {
$err = { { throw [ArgumentException]"[!]" } | Should -Throw -ExpectedMessage '`[`]' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal "Expected an exception with message like '[]' to be thrown, but the message was '[!]'."
}

It 'returns the correct assertion message when exceptions messages differ' {
$testDrive = (Get-PSDrive TestDrive).Root
$testScriptPath = Join-Path $testDrive test.ps1
Expand Down

0 comments on commit 655f934

Please sign in to comment.