Skip to content

Commit 577a963

Browse files
authored
Fix Should-Throw/Should -Throw to handle expected message with escaped wildcard (#2559)
* Fix Should-Throw to handle expected message with escaped wildcard * Fix Should -Throw to handle expected message with escaped wildcard * Fix other tests to test for word 'like' * Correctly pass value to Format-Nicely * Fix escaping outside of string * Fix missing part in one test * Fix test to call -replace correctly * Fix test similar to other
1 parent 0d3dd66 commit 577a963

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/functions/assert/Exception/Should-Throw.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function Should-Throw {
2525
```powershell
2626
{ throw 'error' } | Should-Throw
2727
{ throw 'error' } | Should-Throw -ExceptionMessage 'error'
28+
{ throw 'wildcard character []' } | Should-Throw -ExceptionMessage '*character `[`]'
2829
{ throw 'error' } | Should-Throw -ExceptionType 'System.Management.Automation.RuntimeException'
2930
{ throw 'error' } | Should-Throw -FullyQualifiedErrorId 'RuntimeException'
3031
{ throw 'error' } | Should-Throw -FullyQualifiedErrorId '*Exception'
@@ -100,7 +101,7 @@ function Should-Throw {
100101

101102
$filterOnMessage = -not ([string]::IsNullOrWhiteSpace($ExceptionMessage))
102103
if ($filterOnMessage) {
103-
$filters += "with message '$ExceptionMessage'"
104+
$filters += "with message like '$([System.Management.Automation.WildcardPattern]::Unescape($ExceptionMessage))'"
104105
if ($err.ExceptionMessage -notlike $ExceptionMessage) {
105106
$buts += "the message was '$($err.ExceptionMessage)'"
106107
}

src/functions/assertions/PesterThrow.ps1

+11-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
# this is for Should -Not -Throw. Once *any* exception was thrown we should fail the assertion
7171
# there is no point in filtering the exception, because there should be none
7272
$succeeded = -not $actualExceptionWasThrown
73-
if ($true -eq $succeeded) { return [Pester.ShouldResult]@{Succeeded = $succeeded } }
73+
if ($true -eq $succeeded) {
74+
return [Pester.ShouldResult]@{Succeeded = $succeeded }
75+
}
7476

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

9799
$filterOnMessage = -not [string]::IsNullOrWhitespace($ExpectedMessage)
98100
if ($filterOnMessage) {
99-
$filters += "message like $(Format-Nicely $ExpectedMessage)"
101+
$unescapedExpectedMessage = [System.Management.Automation.WildcardPattern]::Unescape($ExpectedMessage)
102+
$filters += "message like $(Format-Nicely $unescapedExpectedMessage)"
100103
if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) {
101104
$buts += "the message was $(Format-Nicely $actualExceptionMessage)"
102105
}
@@ -120,7 +123,12 @@
120123
$failureMessage = "Expected an exception$(if($filter) { " with $filter" }) to be thrown,$(Format-Because $Because) but $but. $actualExceptionLine".Trim()
121124

122125
$ActualValue = $actualExceptionMessage
123-
$ExpectedValue = if ($filterOnExceptionType) { "type $(Format-Nicely $ExceptionType)" } else { 'any exception' }
126+
$ExpectedValue = if ($filterOnExceptionType) {
127+
"type $(Format-Nicely $ExceptionType)"
128+
}
129+
else {
130+
'any exception'
131+
}
124132

125133
return [Pester.ShouldResult] @{
126134
Succeeded = $false

tst/functions/assert/Exception/Should-Throw.Tests.ps1

+13-4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Describe "Should-Throw" {
5353
It "Fails when exception does not match the message with wildcard" {
5454
{ { throw [ArgumentException]"A is null!" } | Should-Throw -ExceptionMessage '*flabbergasted*' } | Verify-AssertionFailed
5555
}
56+
57+
It "Passes when exception match the message with escaped wildcard" {
58+
{ throw [ArgumentException]"[]" } | Should-Throw -ExceptionMessage '`[`]'
59+
}
5660
}
5761

5862
Context "Filtering with FullyQualifiedErrorId" {
@@ -86,7 +90,7 @@ Describe "Should-Throw" {
8690

8791
It "Given exception that does not match on message it returns the correct message" {
8892
$err = { { throw [ArgumentException]"fail!" } | Should-Throw -ExceptionMessage 'halt!' } | Verify-AssertionFailed
89-
$err.Exception.Message | Verify-Equal "Expected an exception, with message 'halt!' to be thrown, but the message was 'fail!'."
93+
$err.Exception.Message | Verify-Equal "Expected an exception, with message like 'halt!' to be thrown, but the message was 'fail!'."
9094
}
9195

9296
It "Given exception that does not match on FullyQualifiedErrorId it returns the correct message" {
@@ -96,7 +100,7 @@ Describe "Should-Throw" {
96100

97101
It "Given exception that does not match on type and message it returns the correct message" {
98102
$err = { { throw [ArgumentException]"fail!" } | Should-Throw -ExceptionType ([System.InvalidOperationException]) -ExceptionMessage 'halt!' } | Verify-AssertionFailed
99-
$err.Exception.Message | Verify-Equal "Expected an exception, of type [InvalidOperationException], with message 'halt!' to be thrown, but the exception type was [ArgumentException] and the message was 'fail!'."
103+
$err.Exception.Message | Verify-Equal "Expected an exception, of type [InvalidOperationException], with message like 'halt!' to be thrown, but the exception type was [ArgumentException] and the message was 'fail!'."
100104
}
101105

102106
It "Given exception that does not match on type and FullyQualifiedErrorId it returns the correct message" {
@@ -106,12 +110,17 @@ Describe "Should-Throw" {
106110

107111
It "Given exception that does not match on message and FullyQualifiedErrorId it returns the correct message" {
108112
$err = { { throw [ArgumentException]"halt!" } | Should-Throw -ExceptionMessage 'fail!' -FullyQualifiedErrorId 'fail!' } | Verify-AssertionFailed
109-
$err.Exception.Message | Verify-Equal "Expected an exception, with message 'fail!', with FullyQualifiedErrorId 'fail!' to be thrown, but the message was 'halt!' and the FullyQualifiedErrorId was 'halt!'."
113+
$err.Exception.Message | Verify-Equal "Expected an exception, with message like 'fail!', with FullyQualifiedErrorId 'fail!' to be thrown, but the message was 'halt!' and the FullyQualifiedErrorId was 'halt!'."
110114
}
111115

112116
It "Given exception that does not match on type, message and FullyQualifiedErrorId it returns the correct message" {
113117
$err = { { throw [ArgumentException]"halt!" } | Should-Throw -ExceptionType ([System.InvalidOperationException]) -ExceptionMessage 'fail!' -FullyQualifiedErrorId 'fail!' } | Verify-AssertionFailed
114-
$err.Exception.Message | Verify-Equal "Expected an exception, of type [InvalidOperationException], with message 'fail!' and with FullyQualifiedErrorId 'fail!' to be thrown, but the exception type was [ArgumentException], the message was 'halt!' and the FullyQualifiedErrorId was 'halt!'."
118+
$err.Exception.Message | Verify-Equal "Expected an exception, of type [InvalidOperationException], with message like 'fail!' and with FullyQualifiedErrorId 'fail!' to be thrown, but the exception type was [ArgumentException], the message was 'halt!' and the FullyQualifiedErrorId was 'halt!'."
119+
}
120+
121+
It "Given exception that does not match on a message with escaped wildcard it returns the correct message" {
122+
$err = { { throw [ArgumentException]"[!]" } | Should-Throw -ExceptionMessage '`[`]' } | Verify-AssertionFailed
123+
$err.Exception.Message | Verify-Equal "Expected an exception, with message like '[]' to be thrown, but the message was '[!]'."
115124
}
116125
}
117126

tst/functions/assertions/PesterThrow.Tests.ps1

+12
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ InPesterModuleScope {
164164
$err.Exception.Message | Verify-Equal "Expected an exception with FullyQualifiedErrorId 'id' to be thrown, but no exception was thrown."
165165
}
166166

167+
It "returns the correct assertion message when message filter is used and contain escaped wildcard character" {
168+
$testDrive = (Get-PSDrive TestDrive).Root
169+
$testScriptPath = Join-Path $testDrive test.ps1
170+
Set-Content -Path $testScriptPath -Value "throw [ArgumentException]'[!]'"
171+
172+
# use the real path of the script, because we don't know it beforehand
173+
$assertionMessage = "Expected an exception with message like '[]' to be thrown, but the message was '[!]'. from ##path##:1 char:" -replace "##path##", $testScriptPath
174+
175+
$err = { { & $testScriptPath } | Should -Throw -ExpectedMessage '`[`]' } | Verify-AssertionFailed
176+
$err.Exception.Message -replace "(`r|`n)" -replace '\s+', ' ' -replace '(char:).*$', '$1' | Verify-Equal $assertionMessage
177+
}
178+
167179
It 'returns the correct assertion message when exceptions messages differ' {
168180
$testDrive = (Get-PSDrive TestDrive).Root
169181
$testScriptPath = Join-Path $testDrive test.ps1

0 commit comments

Comments
 (0)