Skip to content

Optimize formatting for template strings and numbers#2866

Draft
fflaten wants to merge 8 commits into
pester:mainfrom
fflaten:format-templates
Draft

Optimize formatting for template strings and numbers#2866
fflaten wants to merge 8 commits into
pester:mainfrom
fflaten:format-templates

Conversation

@fflaten

@fflaten fflaten commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

PR Summary

Restricts template strings to one level of object recursion in formatting to avoid hangs on large complex objects.

To address the issue above, this PR also:

  • Refactors Format2 to use a decreasing and internally configurable -MaxDepth instead of increasing -Depth with hard limit. Easier to restrict something a max 1 level than start at depth 9.
  • Uses Format-Numbers* for integers and not just decimals. Integers was formatted as ValueType, requring -MaxDepth 2 for formatting simple arrays.
    • Same applies to [char] vs [string] etc, but not fixed for now.

Fix #2865

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

Comment thread src/Format2.ps1
Comment on lines +180 to +181
# Limit recursion to simple arrays and hashtable to avoid hangs and complex test names/paths.
Format-Nicely2 -Value $Value -MaxDepth 1

@fflaten fflaten Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This will still print a massive multi-line name for something like (Get-Command Invoke-Pester), but at least it doesn't hang. Questions:

  • Should we special case CommandInfo to just return the name like v5? Or let it be so the author will identify a bad practice (not referencing the name property) and fix it.
  • Should tests names expand arrays/dictionaries etc in general?
  • Should tests names allow multi-line at all? Messy console output. Trim or run through [Pester.Formatter]::EscapeControlChars()?

Comment thread src/TypeClass.ps1 Outdated

function Is-IntegralNumber ($Value) {
# Note: Using int16/uint16 as short/ushort names doesn't exist in PS 5.1
$Value -is [int] -or $Value -is [long] -or $Value -is [int16] -or $Value -is [sbyte] -or $Value -is [uint] -or $Value -is [ulong] -or $Value -is [uint16] -or $Value -is [byte]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Use .NET names. Some C# names are missing in 5.1. PowerShell/PowerShell@8f4b66a

@fflaten fflaten force-pushed the format-templates branch from 8e5120e to b224e8e Compare July 14, 2026 09:47
Comment thread src/TypeClass.ps1

function Is-DecimalNumber ($Value) {
$Value -is [float] -or $Value -is [single] -or $Value -is [double] -or $Value -is [decimal]
$Value -is [Single] -or $Value -is [Double] -or $Value -is [Decimal]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Just cosmetic to get consistent naming in code and tests <Type.Name> for both PS 5.1 and 7+

@fflaten fflaten force-pushed the format-templates branch from b224e8e to a510080 Compare July 14, 2026 10:06
@fflaten fflaten requested a review from nohwnd July 14, 2026 10:21
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 expansion hangs on complex objects like CommandInfo

1 participant