Skip to content

Commit feafe7a

Browse files
[release/v7.4.2] Fix a regression in Format-Table when header label is empty (#21156) (#21420)
1 parent cedb058 commit feafe7a

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Table.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ private TableHeaderInfo GenerateTableHeaderInfoFromDataBaseInfo(PSObject so)
173173
ci.alignment = colHeader.alignment;
174174
if (colHeader.label != null)
175175
{
176-
ci.HeaderMatchesProperty = so.Properties[colHeader.label.text] is not null;
176+
if (colHeader.label.text != string.Empty)
177+
{
178+
ci.HeaderMatchesProperty = so.Properties[colHeader.label.text] is not null;
179+
}
177180

178181
ci.label = this.dataBaseInfo.db.displayResourceManagerCache.GetTextTokenString(colHeader.label);
179182
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,55 @@ A Name B
879879
$numDecimals | Should -Be $expectedDecimals -Because $num
880880
}
881881
}
882+
883+
It 'Works for empty column header label' {
884+
$ps1xml = @'
885+
<Configuration>
886+
<ViewDefinitions>
887+
<View>
888+
<Name>Test.Header.Empty</Name>
889+
<ViewSelectedBy>
890+
<TypeName>Test.Format</TypeName>
891+
</ViewSelectedBy>
892+
<TableControl>
893+
<TableHeaders>
894+
<TableColumnHeader>
895+
<Label></Label>
896+
<Width>4</Width>
897+
</TableColumnHeader>
898+
</TableHeaders>
899+
<TableRowEntries>
900+
<TableRowEntry>
901+
<TableColumnItems>
902+
<TableColumnItem>
903+
<PropertyName>Prop</PropertyName>
904+
</TableColumnItem>
905+
</TableColumnItems>
906+
</TableRowEntry>
907+
</TableRowEntries>
908+
</TableControl>
909+
</View>
910+
</ViewDefinitions>
911+
</Configuration>
912+
'@
913+
914+
$ps1xmlPath = Join-Path -Path $TestDrive -ChildPath 'empty.format.ps1xml'
915+
Set-Content -Path $ps1xmlPath -Value $ps1xml
916+
$object = [pscustomobject]@{Prop = '123'}
917+
# run in own runspace so not affect global sessionstate
918+
$ps = [powershell]::Create()
919+
$ps.AddScript( {
920+
param($ps1xmlPath, $object)
921+
Update-FormatData -AppendPath $ps1xmlPath
922+
$object.PSObject.TypeNames.Insert(0, 'Test.Format')
923+
$object | Format-Table | Out-String
924+
} ).AddArgument($ps1xmlPath).AddArgument($object) | Out-Null
925+
$output = $ps.Invoke()
926+
$expected = @"
927+
Prop----123
928+
"@
929+
$output.Replace("`n","").Replace("`r","") | Should -BeExactly $expected
930+
}
882931
}
883932

884933
Describe 'Table color tests' -Tag 'CI' {

0 commit comments

Comments
 (0)