Skip to content

Commit 2f5333a

Browse files
nohwndCopilot
andauthored
Fix #2474: DirectoryInfo/FileInfo display property map prevents infinite loop (#2692)
Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d07855a commit 2f5333a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/Format2.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ function Get-DisplayProperty2 ([Type]$Type) {
157157
# and for types that do not exist
158158

159159
$propertyMap = @{
160-
'System.Diagnostics.Process' = 'Id', 'Name'
160+
'System.Diagnostics.Process' = 'Id', 'Name'
161+
# DirectoryInfo and FileInfo have circular references (Root, Directory) that cause infinite recursion
162+
'System.IO.DirectoryInfo' = 'Name', 'FullName'
163+
'System.IO.FileInfo' = 'Name', 'FullName', 'Length'
161164
}
162165

163166
$propertyMap[$Type.FullName]

tst/Format2.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ InPesterModuleScope {
165165
) {
166166
Format-Nicely2 -Value $Value | Verify-Equal $Expected
167167
}
168+
169+
# Regression test for https://github.com/pester/Pester/issues/2474
170+
# DirectoryInfo has circular references (Root -> DirectoryInfo) that caused
171+
# infinite recursion. Verify formatting completes without hanging.
172+
It "Formats DirectoryInfo without infinite recursion" {
173+
$dir = [System.IO.DirectoryInfo]::new($TestDrive)
174+
$job = Start-Job -ScriptBlock {
175+
param($modulePath, $dirPath)
176+
Import-Module $modulePath
177+
$d = [System.IO.DirectoryInfo]::new($dirPath)
178+
& (Get-Module Pester) { Format-Nicely2 -Value $args[0] } $d
179+
} -ArgumentList (Get-Module Pester).Path, $TestDrive
180+
$result = $job | Wait-Job -Timeout 10 | Receive-Job
181+
$job | Remove-Job -Force
182+
$result | Should -Not -BeNullOrEmpty
183+
}
168184
}
169185

170186
Describe "Get-DisplayProperty2" {
@@ -175,6 +191,19 @@ InPesterModuleScope {
175191
$Actual = Get-DisplayProperty2 -Type $Type
176192
"$Actual" | Verify-Equal "$Expected"
177193
}
194+
195+
# Regression tests for https://github.com/pester/Pester/issues/2474
196+
# DirectoryInfo and FileInfo have circular references (Root, Directory)
197+
# that cause infinite recursion without an explicit property map.
198+
It "Returns 'Name FullName' for DirectoryInfo" {
199+
$Actual = Get-DisplayProperty2 -Type ([System.IO.DirectoryInfo])
200+
"$Actual" | Verify-Equal "Name FullName"
201+
}
202+
203+
It "Returns 'Name FullName Length' for FileInfo" {
204+
$Actual = Get-DisplayProperty2 -Type ([System.IO.FileInfo])
205+
"$Actual" | Verify-Equal "Name FullName Length"
206+
}
178207
}
179208

180209
Describe "Format-Type2" {

0 commit comments

Comments
 (0)