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

Handle the case when the failure message contains an escape sequence #1426

Merged
merged 4 commits into from Jan 22, 2020

Conversation

JamesWTruher
Copy link
Contributor

…which may occur with PowerShell 7

1. General summary of the pull request

If the failure message contains an escape sequence (which can happen with PowerShell 7, and the results are going to be serialized to an Nunit file, you may see something like this:

MethodInvocationException: /Users/jimtru/src/github/forks/JamesWTruher/Pester/Functions/TestResults.ps1
Line |
 591 |             $xmlWriter.WriteElementString('message', $TestResult.FailureMessage)
     |             ^ Exception calling "WriteElementString" with "2" argument(s): "', hexadecimal value 0x1B, is an invalid character."

and the xml file created is partial. The escape in the message is not valid for serialization

This escapes the escape character ([char]27) to &27;

Functions/TestResults.ps1 Outdated Show resolved Hide resolved
$TestResults = New-PesterState -Path TestDrive:\
$testResults.EnterTestGroup('Mocked Describe', 'Describe')
$time = [TimeSpan]25000000 #2.5 seconds
$escape = [string][char]27 # escape, also `e in PowerShell 6+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamesWTruher not 100% sure how to repro the error with escapes on my PowerShell 7, please verify the changes.

The cast to string here is unnecessary, but I kept it anyway to keep it same as in the code file. (There it is needed to force use of Replace(string, string) instead of Replace(char, char) which fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll grant you that my test is quite artificial, but here's the way to see it:

PS> Get-Content /tmp/t1.tests.ps1
describe "a simple test" {
    it "can handle an error with an escape sequence" {
       throw  "`e[31m < < , > > `e[0m"
    }
}

when run as follows, you can see the error:

[Pester|master000+0?0] PS> invoke-pester /tmp/t1.tests.ps1 -OutputFile /tmp/xx.xml -OutputFormat NUnitXml
    ____            __
   / __ \___  _____/ /____  _____
  / /_/ / _ \/ ___/ __/ _ \/ ___/
 / ____/  __(__  ) /_/  __/ /
/_/    \___/____/\__/\___/_/
Pester v4.9.0
Executing all tests in '/tmp/t1.tests.ps1'

Executing script /tmp/t1.tests.ps1

  Describing a simple test
    [-] can handle an error with an escape sequence 1ms
      RuntimeException:  < < , > > 
      at <ScriptBlock>, /tmp/t1.tests.ps1: line 3
Tests completed in 24ms
Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0 
MethodInvocationException: /Users/jimtru/src/github/forks/JamesWTruher/Pester/Functions/TestResults.ps1
Line |
 591 |             $xmlWriter.WriteElementString('message', $TestResult.FailureMessage)
     |             ^ Exception calling "WriteElementString" with "2" argument(s): "', hexadecimal value 0x1B, is an invalid character."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nohwnd - I probably should have put that in the test, but it seemed like too much.

@nohwnd nohwnd added this to the 4.10 milestone Jan 19, 2020
@nohwnd nohwnd merged commit d32ae75 into pester:master Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants