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

Fix a bug where the NUnit-Report fails to generate if the test output contains Virtual Terminal Sequences #2511

Merged
merged 13 commits into from
Jul 1, 2024
18 changes: 17 additions & 1 deletion src/functions/TestResults.NUnit3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,27 @@
}

function Write-NUnit3OutputElement ($Output, [System.Xml.XmlWriter] $XmlWriter) {
# The characters in the range 0x01 to 0x19 are invalid for CData
# (with the exception of the characters 0x09, 0x0A and 0x0D)
# We convert each of these using the unicode printable version,
# which is obtained by adding 0x2400
[int]$unicodeControlPictures = 0x2400
[int[]]$validChars = (0x09,0x0A,0x0D)
$outputString = @(foreach ($o in $Output) {
if ($null -eq $o) {
[string]::Empty
} else {
$o.ToString()
$result = ""
0..($o.Length-1) |% {
$s = $o[$_]
$char = [int]$s
if ((0x20 -gt $char) -and (0x00 -lt $char) -and (-not ($validChars -contains $char))) {
$result += [char]($char + $unicodeControlPictures)
} else {
$result += $s
}
}
$result
}
}) -join [System.Environment]::NewLine

Expand Down
29 changes: 29 additions & 0 deletions tst/Pester.RSpec.TestResults.NUnit3.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,35 @@ i -PassThru:$PassThru {
$xmlResult.Validate({ throw $args[1].Exception })
}

t "handles virtual terminal escape sequences" {
$sb = {
Describe 'Describe VT Sequences' {
It "Successful" {
$esc = [char][int]0x1B
$bell = [char][int]0x07
$testCases = (
"$esc[32mHello`tWorld$esc[0m",
"Ring the bell$bell")
$testCases |% {
Write-Output $_
}
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })

$xmlResult = [xml] ($r | ConvertTo-NUnitReport -Format NUnit3)
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
$xmlDescribe = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTest = $xmlDescribe.'test-case'
$message = $xmlTest.output.'#cdata-section' -split "`n"
$message[0] | Verify-Equal "␛[32mHello`tWorld␛[0m"
$message[1] | Verify-Equal "Ring the bell␇"
}

t 'should use TestResult.TestSuiteName configuration value as name-attribute for run and root Assembly test-suite' {
$sb = {
Describe 'Describe' {
Expand Down