Skip to content
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

Should-Throw (and Should -Throw): Backtick is kept in error message when escaping wildcard characters #2558

Closed
3 tasks done
johlju opened this issue Aug 22, 2024 · 3 comments · Fixed by #2559
Closed
3 tasks done
Labels

Comments

@johlju
Copy link
Contributor

johlju commented Aug 22, 2024

Checklist

What is the issue?

When having wildcard characters in the ExceptionMessage (Should-Throw) or ExpectedMessage (Should -Throw) they are not removed from the expected message in the console making it look like the string is expected to actually have backticks. This is a minor issue, but maybe it possible to make it look better in the futuee.

This is related to issue #1793 which might or might not resolve this.

Expected Behavior

Remove backticks from the expected message that is output on the console.

Steps To Reproduce

Describe 'Something' {
    It 'Should output and error' {
        { throw 'int[]' } | Should-Throw -ExceptionMessage 'string`[`]'
    }

    It 'Should output and error' {
        { throw 'int[]' } | Should -Throw -ExpectedMessage 'string`[`]'
    }
}

outputs:

Starting discovery in 1 files.
Discovery found 16 tests in 96ms.
Running tests.
[-] Something.Should output and error 12ms (11ms|1ms)
 Expected an exception, with message 'string`[`]' to be thrown, but the message was 'int[]'.
 at { throw 'int[]' } | Should-Throw -ExceptionMessage 'string`[`]', /Users/johlju/source/Viscalyx.Assert/tests/Unit/Public/Assert-BlockString.tests.ps1:171
[-] Something.Should output and error 16ms (15ms|1ms)
 Expected an exception with message like 'string`[`]' to be thrown, but the message was 'int[]'. from /Users/johlju/source/Viscalyx.Assert/tests/Unit/Public/Assert-BlockString.tests.ps1:175 char:11
     +         { throw 'int[]' } | Should -Throw -ExpectedMessage 'string`[` …
     +           ~~~~~~~~~~~~~
 at { throw 'int[]' } | Should -Throw -ExpectedMessage 'string`[`]', /Users/johlju/source/Viscalyx.Assert/tests/Unit/Public/Assert-BlockString.tests.ps1:175
 at <ScriptBlock>, /Users/johlju/source/Viscalyx.Assert/tests/Unit/Public/Assert-BlockString.tests.ps1:175
Tests completed in 267ms
Tests Passed: 0, Failed: 2, Skipped: 0, Inconclusive: 0, NotRun: 14

Here it outputs Expected an exception, with message 'string`[`]' to be thrown where it would be better if it could have been output as Expected an exception, with message 'string[]' to be thrown.

Describe your environment

Pester version     : 6.0.0-alpha4 /Users/johlju/source/Viscalyx.Assert/output/RequiredModules/Pester/6.0.0/Pester.psm1  
PowerShell version : 7.4.4
OS version         : Unix 14.6.1

Possible Solution?

Maybe just [System.Management.Automation.WildcardPattern]::Unescape('string`[`]') on the expected message?

@johlju johlju changed the title Should-Throw (and `Should -Throw): Backtick is kept in error message when escaping wildcard charcaters Should-Throw (and Should -Throw): Backtick is kept in error message when escaping wildcard charcaters Aug 22, 2024
@johlju johlju changed the title Should-Throw (and Should -Throw): Backtick is kept in error message when escaping wildcard charcaters Should-Throw (and Should -Throw): Backtick is kept in error message when escaping wildcard characters Aug 22, 2024
@fflaten
Copy link
Collaborator

fflaten commented Aug 22, 2024

Maybe just [System.Management.Automation.WildcardPattern]::Unescape('string`[`]') on the expected message?

+1 for this, incl. backport to 5.6.*. Would you like to provide a PR?

I'd also vote for updating the text in Should-Throw to match the "with message like ..." style of Should -Throw as it implies that it's a -like pattern.

@fflaten fflaten added the Bug label Aug 22, 2024
@johlju
Copy link
Contributor Author

johlju commented Aug 23, 2024

I can try doing a PR. It should be fairly small for me to be able to get it done.

@fflaten how is backport done. Two PR's targeting main and ...? respectively?

@fflaten
Copy link
Collaborator

fflaten commented Aug 23, 2024

Thanks 🙏

Yes, main for v6 and rel/5.6.x for backport.

There is a bot comment for this, but probably easier to cherry pick manually as you only need the Should -Throw change for v5 PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants