-
-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExpectedMessage comparison fails #1793
Comments
Maybe because the comparison is using -like and [] is some kind of wildcard? |
Yep that's the one. { @(1, 2) - 1 } | Should -Throw -ExpectedMessage "Method invocation failed because ``[System.Object``[``]``] does not contain a method named 'op_Subtraction'." Hmm. |
FWIW this is a breaking change between Pester v4 and Pester v5. Might be worth calling out more specifically than just the blurb of:
That already exists on the breaking changes page here: https://pester-docs.netlify.app/docs/migrations/breaking-changes-in-v5 This is the root cause of this person's issue on StackOverflow: https://stackoverflow.com/questions/65219744/pester-v5-1-should-throw-with-message-containing-square-brackets-fails Pester v4Import-Module Pester -MaximumVersion '4.10.1'
function Test-Throw {
throw "I should throw a [message]"
}
Describe 'PesterThrowBug' {
It 'Should Throw a Message' {
{ Test-Throw } | Should -Throw -ExpectedMessage 'I should throw a [message]'
}
} Output:
Pester v5Import-Module Pester -MinimumVersion '5.0.0'
function Test-Throw {
throw "I should throw a [message]"
}
Describe 'PesterThrowBug' {
It 'Should Throw a Message' {
{ Test-Throw } | Should -Throw -ExpectedMessage 'I should throw a [message]'
}
} Output:
The error message is particularly unhelpful because the Work AroundAs mentioned above escaping the Import-Module Pester -MinimumVersion '5.0.0'
function Test-Throw {
throw "I should throw a [message]"
}
Describe 'PesterThrowBug' {
It 'Should Throw a Message' {
{ Test-Throw } | Should -Throw -ExpectedMessage 'I should throw a `[message`]'
}
} Output:
|
Here's a search I wrote up to identify affected locations that might be useful to someone else looking to perform a lift: function Get-ExpectedMessageUsage {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
$Path
)
process {
$scriptContent = Get-Content -Path $Path -Raw
$scriptAst = [ScriptBlock]::Create($scriptContent).Ast
$possibleAffectedLocations = $scriptAst.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true) |
Where-Object { $_.GetCommandName() -eq 'Should' } |
Where-Object { $null -ne ($_ | Select-Object -ExpandProperty CommandElements | Select-Object -ExpandProperty ParameterName -ErrorAction SilentlyContinue | Where-Object { $_ -eq 'ExpectedMessage' }) }
# It is only an affected location if it contains a Wild Card Character
# as defined here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-7.1
# that is not escaped.
$affectedLocations = $possibleAffectedLocations |
Select-Object -ExpandProperty CommandElements |
Where-Object { ($_.StringConstantType -eq 'DoubleQuoted' -or $_.StringConstantType -eq 'SingleQuoted') -and ([Regex]::Match($($_.Value), '[^`][\[\]\*\?]')) }
[PSCustomObject]@{
Path = $Path
AffectedCount = ($affectedLocations | Measure-Object).Count
}
}
} |
Reopening this because there are multiple comments, and I don't think I can move them all into a new comment. There are few options we have:
|
My bad :( I waffled back and forth between opening a new issue or not.
Sounds great to me.
I completely agree with this; even more so for
I would vote for this personally, while more verbose also avoids the footgun. |
Correct,
One disadvantage with parameter-alias in our case is that they're not linked to parameter-sets, so they would also apply for other operators using the same I'd personally vote for the wildcardpattern-check + diag-log mentioning it and how to escape Update: Or add a |
General summary of the issue
Error message comparison fails.
Describe your environment
Steps to reproduce
Expected Behavior
The assertion should be successful.
Using try / catch to perform the same comparison is successful:
Current Behavior
The assertion fails.
The text was updated successfully, but these errors were encountered: