diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index 6886eb761cf..bd5780db45f 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -371,7 +371,7 @@ private StringCollection GenerateMultiLineRowField(string val, int k, int alignm { StringCollection sc = StringManipulationHelper.GenerateLines(dc, val, _si.columnInfo[k].width, _si.columnInfo[k].width); - if (addPadding) + if (addPadding || alignment == TextAlignment.Right) { // if length is shorter, do some padding for (int col = 0; col < sc.Count; col++) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 index 8be57d9fdda..acd2f1ef754 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -711,6 +711,49 @@ abc bcd +"@ } + ) { + param($obj, $expectedTable) + $output = $obj | Format-Table | Out-String + $output.Replace("`r","").Replace(" ",".").Replace("`n","^") | Should -BeExactly $expectedTable.Replace("`r","").Replace(" ",".").Replace("`n","^") + } + + It "Should render header correctly where header is shorter than column width with justification: " -TestCases @( + @{ variation = "left/right"; obj = [PSCustomObject]@{a="abc";b=123}; expectedTable = @" + +a b +- - +abc 123 + + + +"@ }, + @{ variation = "left/left"; obj = [PSCustomObject]@{a="abc";b="abc"}; expectedTable = @" + +a b +- - +abc abc + + + +"@ }, + @{ variation = "right/left"; obj = [PSCustomObject]@{a=123;b="abc"}; expectedTable = @" + + a b + - - +123 abc + + + +"@ }, + @{ variation = "right/right"; obj = [PSCustomObject]@{a=123;b=123}; expectedTable = @" + + a b + - - +123 123 + + + "@ } ) { param($obj, $expectedTable)