pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.

Home Page:https://pester.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite loop for `FileInfo` and `DirectoryInfo` due to `Root` properties in `DirectoryInfo`. Should we restrict depth as a general fix in addition to specialcasing `DirectoryInfo` in `Get-DisplayProperty2`?

nohwnd opened this issue · comments

Infinite loop for FileInfo and DirectoryInfo due to Root properties in DirectoryInfo. Should we restrict depth as a general fix in addition to specialcasing DirectoryInfo in Get-DisplayProperty2?

Originally posted by @fflaten in #2428 (comment)

fflaten wrote:

Experimented a little with this. Example:

# root of Format2.ps1
$script:defaultDisplayPropertyCache = [System.Collections.Generic.Dictionary[type,string[]]]::new()

function Get-DisplayProperty2 ($Object) {
    $propertyMap = @{
        [System.Diagnostics.Process] = 'Id', 'Name'
    }

    $type = $Object.GetType()

    $properties = $propertyMap[$type]
    if ($null -ne $properties) { return $properties }

    # Fallback to DefaultDisplayPropertySet if defined for type
    if ($script:defaultDisplayPropertyCache.TryGetValue($type,[ref]$properties)) { return $properties }

    # Alterantive to enable inheritance: Loop $Object.PSTypeNames with (Get-TypeData $typename).DefaultDisplayPropertySet.ReferencedProperties
    # Would be slower and not sure if any types work this way
    if (($standardMembers = $Object.psobject.Members['PSStandardMembers']) -and
        ($defaultDisplayPropSet = $standardMembers.psobject.Members['DefaultDisplayPropertySet'])) {
        # Is this a live reference we need to break? CopyTo new stringarray?
        $properties = $defaultDisplayPropSet.ReferencedPropertyNames -as [string[]]
    }
    # Always cache to skip repeating PSStandardMembers check
    $script:defaultDisplayPropertyCache.Add($type, $properties)
    $properties
}

Thoughts:

  • Types like DirectoryInfo do not have standard member set, so needs to go in the $propertyMap to avoid infinite loop due to Root-property.
  • Is it a problem that Get-DisplayProperty2 could exclude properties that were different? Should be visible with -Verbose but still?