From 93c661020d666c7c07577c46b8ad756b8aa74aec Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Mon, 13 May 2024 09:57:36 +0200 Subject: [PATCH] Fix possible NRE in NUnit3 export (#2444) * Fix possible NRE in NUnit3 export * Fix tests on Windows --- src/functions/TestResults.NUnit3.ps1 | 8 +++++++- tst/Pester.RSpec.TestResults.NUnit3.ts.ps1 | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/functions/TestResults.NUnit3.ps1 b/src/functions/TestResults.NUnit3.ps1 index ecb5d6fa4..c97fbd8c6 100644 --- a/src/functions/TestResults.NUnit3.ps1 +++ b/src/functions/TestResults.NUnit3.ps1 @@ -539,7 +539,13 @@ function Write-NUnit3TestCaseAttributes { } function Write-NUnit3OutputElement ($Output, [System.Xml.XmlWriter] $XmlWriter) { - $outputString = @(foreach ($o in $Output) { $o.ToString() }) -join [System.Environment]::NewLine + $outputString = @(foreach ($o in $Output) { + if ($null -eq $o) { + [string]::Empty + } else { + $o.ToString() + } + }) -join [System.Environment]::NewLine $XmlWriter.WriteStartElement('output') $XmlWriter.WriteCData($outputString) diff --git a/tst/Pester.RSpec.TestResults.NUnit3.ts.ps1 b/tst/Pester.RSpec.TestResults.NUnit3.ts.ps1 index c4d2d239e..27dbb5951 100644 --- a/tst/Pester.RSpec.TestResults.NUnit3.ts.ps1 +++ b/tst/Pester.RSpec.TestResults.NUnit3.ts.ps1 @@ -409,6 +409,8 @@ i -PassThru:$PassThru { } It 'Test' { 'test output' + $null # Should not throw but leave blank line + 123 $true | Should -Be $true } } @@ -423,7 +425,10 @@ i -PassThru:$PassThru { $xmlTest = $xmlDescribe.'test-case' $xmlTest.name | Verify-Equal 'Describe.Test' - $xmlTest.output.'#cdata-section' | Verify-Equal 'test output' + $message = $xmlTest.output.'#cdata-section' -split "`n" + $message[0] | Verify-Equal 'test output' + $message[1] | Verify-Equal '' + $message[2] | Verify-Equal '123' } t 'should add site-attribute to identity failure location' {