Skip to content

Render the value in a test name, not its type - #3111

Open
nohwnd wants to merge 2 commits into
mainfrom
nohwnd-template-tostring
Open

nohwnd wants to merge 2 commits into
mainfrom
nohwnd-template-tostring

Conversation

@nohwnd

@nohwnd nohwnd commented Dec 3, 2026

Copy link
Copy Markdown
Member

Fix #3102

A <token> in a test name is rendered by the same formatter that writes assertion messages. When the value is an object the formatter does not know, it prints the object's type instead of walking its properties, which is what fixed the hang in #2865. The type name is the same string for every value of that type, so a data-driven test over three versions came out with one name three times:

Describe 'name collision' {
    It 'supports <ver>' -ForEach @(
        @{ ver = [version]'1.0.0' }
        @{ ver = [version]'2.0.0' }
        @{ ver = [version]'3.0.0' }
    ) { $ver.Major | Should -BeGreaterThan 0 }
}
6.2.0                        now
[+] supports [version]       [+] supports 1.0.0
[+] supports [version]       [+] supports 2.0.0
[+] supports [version]       [+] supports 3.0.0

A type that has a short string form of its own can say what the value is, so the formatter now asks for that form first and keeps the type name as the fallback.

Every value from the issue, measured

Same file, same cases, each version in its own pwsh process on macOS:

value 5.9.1 6.2.0 now
'hello' hello hello hello
42 42 42 42
[version]'1.2.3' 1.2.3 [version] 1.2.3
[uri]'https://example.com/a' https://example.com/a [uri] https://example.com/a
[pscustomobject]@{A=1;B='two'} @{A=1; B=two} [PSObject] @{A=1; B=two}
[ipaddress]'127.0.0.1' 127.0.0.1 [ipaddress] 127.0.0.1
@{ ver = [version]'1.0.0' } System.Collections.Hashtable @{ver=[version]} @{ver=1.0.0}
Get-Item /etc/hosts /etc/hosts IO.FileInfo{FullName='/etc/hosts'; Length=213; Name='hosts'} IO.FileInfo{FullName='/etc/hosts'; Name='hosts'}
Get-Command Get-Item Get-Item Management.Automation.CmdletInfo{Name='Get-Item'} unchanged
Get-Process -Id $pid System.Diagnostics.Process (pwsh) Diagnostics.Process{Id=3713; Name='pwsh'} unchanged
@{A=1} System.Collections.Hashtable @{A=1} unchanged
@(1,2,3) 1 2 3 @(1, 2, 3) unchanged
{ 1 + 1 } 1 + 1 { 1 + 1 } unchanged
[guid]'...0001', [timespan], [datetime], [DayOfWeek] the value the value unchanged

The last rows are the point of what does not change: every case where Pester 6 already reads better than Pester 5, the hashtable, the array and the script block, is untouched, because the new text is only used where the output would otherwise be the bare type name.

The nested row is worth one note. @{ ver = [version]'1.0.0' } was a name collision in 5.9.1 too, it just collided on System.Collections.Hashtable instead.

Why the names matter beyond reading them

Test names are the key in the NUnit and JUnit result files, so they are how Azure DevOps, GitHub and any home-grown comparison match a test to its history. Three cases that share one name cannot be told apart in a report, and a name that changes between runs cannot be matched to the same test's earlier results. That is also why Length is out of the FileInfo summary now, in its own commit: it is what the file holds right now, so the test name changed whenever the file did.

What this does not bring back

The hang in pester/Pester#2865 — Name expansion hangs on complex objects like CommandInfo. The property walk is still skipped at a shallow budget, and CommandInfo is still in the registry of types that are summarised by one property, so a whole CommandInfo in a template still renders from its Name and returns promptly. The new text comes from the value's own string form, which PowerShell builds one level deep and which does not walk the property graph.

Values whose string form would be unreadable fall back to the type, so an object with no string form of its own, one longer than 100 characters, or one that spans lines keeps the old output:

Format-Nicely2 -Value ([uri]"https://example.com/$('a' * 200)") -MaxDepth 1   # [uri]
Format-Nicely2 -Value ([InvalidOperationException]::new("one`ntwo")) -MaxDepth 1   # [InvalidOperationException]
Format-Nicely2 -Value ([Collections.Generic.List[string]]@('a')) -MaxDepth 0  # [Collections.Generic.List[string]]

Full assertion messages are unaffected. They start with the full recursion budget, so a value there never reaches the point where it would collapse to a type name in the first place: Format-Nicely2 on a PSCustomObject still prints PSObject{Age=28; Name='Jakub'} as before.

🤖

A type whose own string form is short and on one line can say what the
value is, which is what a test name wants: three cases over three
versions used to come out as "supports [version]" three times, with
nothing in the name to tell them apart. The type name is still the
fallback, for a value that has no string form of its own, or one that
is long or spans lines.

Fix #3102

🤖
Length is what the file holds right now, so a test name built from a
FileInfo changed whenever the file did, and a name that is not stable
across runs cannot be matched to the same test's earlier results in the
NUnit and JUnit reports. Name and FullName identify the file.

🤖
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.

Name template expansion lost the value for Version, Uri and PSObject, so data-driven tests share one name

1 participant