Skip to content

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

@johlju

Description

@johlju
Contributor

Checklist

  • Issue has a meaningful title
    I have searched the existing issues. See all issues
    I have tested using the latest version of Pester. See Installation and update guide.

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:

␀␇␈␉␌␍␊␛

Activity

nohwnd

nohwnd commented on Oct 3, 2024

@nohwnd
Member

yes that would be good, I've been using lately for ANSI terminal testing and it has been a lifesaver to have it as printable character.

added this to the 6.0.0 milestone on Jan 30, 2025
linked a pull request that will close this issue on Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @nohwnd@johlju

      Issue actions

        `Should-BeString`: Doesn't handle escape character correctly in error message · Issue #2561 · pester/Pester