Skip to content

Commit d19716e

Browse files
nohwndCopilot
andcommitted
Fix #2649: Catch ToString() exceptions in NUnit3 output
Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d5f7c87 commit d19716e

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/functions/TestResults.NUnit3.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,12 @@ function Format-CDataString ($Output) {
554554
$linesCount = $out.Length
555555
$o = for ($i = 0; $i -lt $linesCount; $i++) {
556556
# The input is array of objects, convert them to strings.
557-
$line = if ($null -eq $out[$i]) { [String]::Empty } else { $out[$i].ToString() }
557+
$line = if ($null -eq $out[$i]) {
558+
[String]::Empty
559+
}
560+
else {
561+
try { $out[$i].ToString() } catch { "<Output object ToString() failed: $($_.Exception.Message)>" }
562+
}
558563

559564
if (0 -gt $line.IndexOfAny($script:invalidCDataChars)) {
560565
# No special chars that need replacing.

tst/Pester.RSpec.TestResults.NUnit3.ts.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,50 @@ i -PassThru:$PassThru {
955955
$xmlResult.Validate({ throw $args[1].Exception })
956956
}
957957
}
958+
959+
# Regression test for https://github.com/pester/Pester/issues/2649
960+
# When a test outputs an object whose ToString() throws, the NUnit3 report
961+
# writer (Format-CDataString) would crash, losing the entire report.
962+
# The fix wraps ToString() in try/catch and uses a fallback string.
963+
b "NUnit3 report handles objects with broken ToString()" {
964+
t "should produce valid report when test output object throws on ToString" {
965+
# Create a class whose ToString() throws
966+
$typeAdded = try { [BrokenToString] } catch { $false }
967+
if (-not $typeAdded) {
968+
Add-Type -TypeDefinition '
969+
public class BrokenToString {
970+
public override string ToString() {
971+
throw new System.InvalidOperationException("ToString failed");
972+
}
973+
}
974+
'
975+
}
976+
977+
$sb = {
978+
Describe 'Describe' {
979+
It 'Outputs broken object' {
980+
# Output an object whose ToString() will throw
981+
[BrokenToString]::new()
982+
$true | Should -Be $true
983+
}
984+
}
985+
}
986+
987+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
988+
Run = @{ ScriptBlock = $sb; PassThru = $true }
989+
Output = @{ Verbosity = 'None' }
990+
})
991+
992+
# The test itself should pass
993+
$r.Result | Verify-Equal 'Passed'
994+
995+
# Converting to NUnit3 report should NOT throw
996+
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
997+
998+
# The output section should contain the fallback message
999+
$xmlTest = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
1000+
$xmlTest.result | Verify-Equal 'Passed'
1001+
$xmlTest.output.'#cdata-section' | Verify-Like '*ToString() failed*'
1002+
}
1003+
}
9581004
}

0 commit comments

Comments
 (0)