pester / Pester

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

`Should-BeString`: Doesn't handle escape character correctly in error message #2561

Open johlju opened 2 weeks ago

johlju commented 2 weeks ago

Checklist

What is the issue?

If the actual value or expected value contain escape character (for example an ANSI sequence) the message are not shown correctly.

Expected Behavior

Should show the special character like escape characters. Also ANSI sequences should not be hidden as they are part of the strings being compared.

Steps To Reproduce

Describe 'Something' {
    It 'Escape in expected value should be handled correctly' {
        '31;3;5m' | Should-BeString "`e[313;5m"
    }

    It 'Escape in actual value should be handled correctly' {
        "`e31;3;5m" | Should-BeString '[31;3;5m'
    }

    It 'Escape in both expected and actual value should be handled correctly' {
        "`e31;3;5m" | Should-BeString "`e[31;3;5m"
    }
}

Outputs:

Starting discovery in 1 files.
Discovery found 11 tests in 30ms.
Running tests.
[-] Something.Escape in expected value should be handled correctly 10ms (9ms|2ms)
 Expected [string] '', but got [string] '31;3;5m'.
 at '31;3;5m' | Should-BeString "`e[313;5m", /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/ConvertTo-AnsiSequence.tests.ps1:89
[-] Something.Escape in actual value should be handled correctly 24ms (22ms|2ms)
 Expected [string] '[31;3;5m', but got [string] '1;3;5m'.
 at "`e31;3;5m" | Should-BeString '[31;3;5m', /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/ConvertTo-AnsiSequence.tests.ps1:93
[-] Something.Escape in both expected and actual value should be handled correctly 37ms (30ms|6ms)
 Expected [string] '', but got [string] '1;3;5m'.
 at "`e31;3;5m" | Should-BeString "`e[31;3;5m", /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/ConvertTo-AnsiSequence.tests.ps1:97
Tests completed in 432ms
Tests Passed: 8, Failed: 3, Skipped: 0, Inconclusive: 0, NotRun: 0

Describe your environment

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

Possible Solution?

Maybe convert special characters in actual and expected value to their unicode equivalent?

"`0`a`b`t`f`r`n`e" `
    -replace "`0", '␀' `
    -replace "`a", '␇' `
    -replace "`b", '␈' `
    -replace "`t", '␉' `
    -replace "`f", '␌' `
    -replace "`r", '␍' `
    -replace "`n", '␊' `
    -replace "`e", '␛'

Maybe this should go into the Format-Nicely function, so all strings are parsed? 🤔 Otherwise strings in expected and actual could also affect the output in the console (changing colors etc.)

Outputs:


␀␇␈␉␌␍␊␛