pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.08k stars 470 forks source link

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

Open johlju opened 3 weeks ago

johlju commented 3 weeks ago

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?

fflaten commented 3 weeks ago

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.

johlju commented 3 weeks ago

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 commented 3 weeks ago

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