Skip to content

Fix #2649: Catch ToString() exceptions in NUnit3 output#2697

Merged
nohwnd merged 1 commit into
pester:mainfrom
nohwnd:fix-2649-nunit3-tostring
Jun 16, 2026
Merged

Fix #2649: Catch ToString() exceptions in NUnit3 output#2697
nohwnd merged 1 commit into
pester:mainfrom
nohwnd:fix-2649-nunit3-tostring

Conversation

@nohwnd

@nohwnd nohwnd commented Apr 3, 2026

Copy link
Copy Markdown
Member

Fix #2649

NUnit3 report writing crashed when a test output object's ToString() method throws. Wrapped in try/catch with a descriptive fallback string.

Copilot-generated fix.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

@nohwnd

nohwnd commented Apr 3, 2026

Copy link
Copy Markdown
Member Author

tests

@nohwnd nohwnd force-pushed the fix-2649-nunit3-tostring branch from f9f70da to d19716e Compare April 3, 2026 10:19
@nohwnd nohwnd marked this pull request as ready for review April 3, 2026 11:40
@nohwnd

nohwnd commented Apr 3, 2026

Copy link
Copy Markdown
Member Author

🤖 Tests added in latest push — verifies NUnit3 report survives ToString() that throws.

Comment on lines +966 to +975
$typeAdded = try { [BrokenToString] } catch { $false }
if (-not $typeAdded) {
Add-Type -TypeDefinition '
public class BrokenToString {
public override string ToString() {
throw new System.InvalidOperationException("ToString failed");
}
}
'
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Replace with psobject or New-MockObject?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done at c019e53 — dropped the Add-Type C# class in favour of a [PSCustomObject] with Add-Member -MemberType ScriptMethod -Name ToString -Force whose body throws. Same test, no extra type registered per run.

@nohwnd nohwnd force-pushed the fix-2649-nunit3-tostring branch from d19716e to 1003748 Compare June 12, 2026 11:29
Copilot-generated fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd force-pushed the fix-2649-nunit3-tostring branch from 1003748 to c019e53 Compare June 12, 2026 13:17
@nohwnd nohwnd merged commit 8aaf860 into pester:main Jun 16, 2026
13 checks passed
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.

Error during handling of output-element with state based ToString() Implementation

2 participants