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

Replace escape characters in Format-String2 #2615

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Format2.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function Format-Collection2 ($Value, [switch]$Pretty) {

# 0x1B is escape, the control code "`e" was introduces in Powershell 7, and so we write it directly as the number here.
[char[]] $script:specialChars = foreach ($ch in ("`0", "`a", "`b", "`t", "`f", "`r", "`n", 0x1B)) { [char]$ch }

function Format-Collection2 ($Value, [switch]$Pretty) {
$length = 0
$o = foreach ($v in $Value) {
$formatted = Format-Nicely2 -Value $v -Pretty:$Pretty
Expand Down Expand Up @@ -45,11 +49,35 @@ function Format-Object2 ($Value, $Property, [switch]$Pretty) {
$o
}

function Replace-SpecialCharactersInString ($Value) {

}
function Format-String2 ($Value) {
if ('' -eq $Value) {
return '<empty>'
}


# Special characters like `n or `e should be rendered as their visible versions.
# Especially `e which is used in ANSI codes and can entirely break our output.
#
# We convert each of these using the unicode printable version,
# which is obtained by adding 0x2400
[int]$unicodeControlPictures = 0x2400

if (0 -lt $Value.IndexOfAny($script:specialChars)) {
$chars = [char[]]$Value
$charCount = $chars.Length
for ($j = 0; $j -lt $charCount; $j++) {
$char = $chars[$j]
if ($char -in $script:specialChars) {
$chars[$j] = [char]([int]$char + $unicodeControlPictures)
}
}

$Value = $chars -join ''
}

"'$Value'"
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions/TestResults.NUnit3.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NUnit3 schema docs: https://docs.nunit.org/articles/nunit/technical-notes/usage/Test-Result-XML-Format.html

[char[]] $script:invalidCDataChars = foreach ($ch in (0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F)) { [char]$ch }
[char[]] $script:invalidCDataChars = foreach ($ch in (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F)) { [char]$ch }

function Write-NUnit3Report([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) {
# Write the XML Declaration
Expand Down
35 changes: 34 additions & 1 deletion tst/Format2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,41 @@ InPesterModuleScope {
Format-String2 -Value "" | Verify-Equal '<empty>'
}

It "Formats string to be sorrounded by quotes" {
It "Formats string to be surrounded by quotes" {
Format-String2 -Value "abc" | Verify-Equal "'abc'"
}


It "Replaces ansi escapes with their showable equivalent" -ForEach @(
@{ Value = "`0"; Expected = '␀'; PowerShellVersion = 5 }
@{ Value = "`a"; Expected = '␇'; PowerShellVersion = 5 }
@{ Value = "`b"; Expected = '␈'; PowerShellVersion = 5 }
@{ Value = "`t"; Expected = '␉'; PowerShellVersion = 5 }
@{ Value = "`f"; Expected = '␌'; PowerShellVersion = 5 }
@{ Value = "`r"; Expected = '␍'; PowerShellVersion = 5 }
@{ Value = "`n"; Expected = '␊'; PowerShellVersion = 5 }
# Escape for escape was introduced in PowerShell 7
@{ Value = "`e"; Expected = '␛'; PowerShellVersion = 7 }
) {
if ($PSVersionTable.PSVersion.Major -lt $PowerShellVersion) {
continue
}

Format-String2 -Value "-$value-" | Verify-Equal "'-$expected-'"
}

It "Does not replace non escaped values with escapes" -ForEach @(
@{ Value = "0"; Expected = '0' }
@{ Value = "a"; Expected = 'a' }
@{ Value = "b"; Expected = 'b' }
@{ Value = "t"; Expected = 't' }
@{ Value = "f"; Expected = 'f' }
@{ Value = "r"; Expected = 'r' }
@{ Value = "n"; Expected = 'n' }
@{ Value = "e"; Expected = 'e' }
) {
Format-String2 -Value "-$value-" | Verify-Equal "'-$expected-'"
}

}
}
5 changes: 5 additions & 0 deletions tst/functions/assert/String/Should-BeString.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ Describe "Should-BeString" {
$err = { Should-BeString -Expected "abc" -Actual "bde" } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal "Expected [string] 'abc', but got [string] 'bde'."
}

It "Handles escape character in the error message" {
$err = { Should-BeString -Expected "ee `e ee" -Actual "mmmmm" } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal "Expected [string] 'ee ␛ ee', but got [string] 'mmmmm'."
}
}
Loading