From 7fdec217f712f9be54c822644c440aa00f93e1a2 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 28 Nov 2017 14:37:19 -0800 Subject: [PATCH 1/4] don't add trailing space to last column in table --- .../FormatAndOutput/common/TableWriter.cs | 42 ++++++++++++++----- .../Format-Table.Tests.ps1 | 42 +++++++++---------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index abd9f0a60bb..24455621747 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -244,11 +244,18 @@ private string[] GenerateTableRow(string[] values, int[] alignment, DisplayCells return null; StringCollection[] scArray = new StringCollection[validColumnCount]; + bool addPadding = true; for (int k = 0; k < scArray.Length; k++) { + // for the last column, don't pad it with trailing spaces + if (k == scArray.Length-1) + { + addPadding = false; + } + // obtain a set of tokens for each field scArray[k] = GenerateMultiLineRowField(values[validColumnArray[k]], validColumnArray[k], - alignment[validColumnArray[k]], ds); + alignment[validColumnArray[k]], ds, addPadding); // NOTE: the following padding operations assume that we // pad with a blank (or any character that ALWAYS maps to a single screen cell @@ -283,7 +290,7 @@ private string[] GenerateTableRow(string[] values, int[] alignment, DisplayCells } // add padding for the columns that are shorter - for (int col = 0; col < scArray.Length; col++) + for (int col = 0; col < scArray.Length-1; col++) { int paddingBlanks = _si.columnInfo[validColumnArray[col]].width; if (col > 0) @@ -317,15 +324,18 @@ private string[] GenerateTableRow(string[] values, int[] alignment, DisplayCells return rows; } - private StringCollection GenerateMultiLineRowField(string val, int k, int alignment, DisplayCells dc) + private StringCollection GenerateMultiLineRowField(string val, int k, int alignment, DisplayCells dc, bool addPadding) { StringCollection sc = StringManipulationHelper.GenerateLines(dc, val, _si.columnInfo[k].width, _si.columnInfo[k].width); - // if length is shorter, do some padding - for (int col = 0; col < sc.Count; col++) + if (addPadding) { - if (dc.Length(sc[col]) < _si.columnInfo[k].width) - sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, alignment, dc); + // if length is shorter, do some padding + for (int col = 0; col < sc.Count; col++) + { + if (dc.Length(sc[col]) < _si.columnInfo[k].width) + sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, alignment, dc, addPadding); + } } return sc; } @@ -334,8 +344,15 @@ private string GenerateRow(string[] values, int[] alignment, DisplayCells dc) { StringBuilder sb = new StringBuilder(); + bool addPadding = true; for (int k = 0; k < _si.columnInfo.Length; k++) { + // don't pad the last column + if (k == _si.columnInfo.Length -1) + { + addPadding = false; + } + if (_si.columnInfo[k].width <= 0) { // skip columns that are not at least a single character wide @@ -357,12 +374,12 @@ private string GenerateRow(string[] values, int[] alignment, DisplayCells dc) sb.Append(StringUtil.Padding(_startColumn)); } } - sb.Append(GenerateRowField(values[k], _si.columnInfo[k].width, alignment[k], dc)); + sb.Append(GenerateRowField(values[k], _si.columnInfo[k].width, alignment[k], dc, addPadding)); } return sb.ToString(); } - private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc) + private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc, bool addPadding) { // make sure the string does not have any embedded in it string s = StringManipulationHelper.TruncateAtNewLine(val) ?? ""; @@ -370,7 +387,7 @@ private static string GenerateRowField(string val, int width, int alignment, Dis string currentValue = s; int currentValueDisplayLength = dc.Length(currentValue); - if (currentValueDisplayLength < width) + if (addPadding && currentValueDisplayLength < width) { // the string is shorter than the width of the column // need to pad with with blanks to reach the desired width @@ -499,7 +516,10 @@ private static string GenerateRowField(string val, int width, int alignment, Dis default: { // left align is the default - s += " "; + if (addPadding) + { + s += " "; + } } break; } 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 6a6ab2d61ae..16b24687835 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -139,42 +139,32 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" { It "Format-Table with No Objects for End-To-End should work"{ $p = @{} - $result = $p|Format-Table -Property "foo","bar"|Out-String + $result = $p | Format-Table -Property "foo","bar" | Out-String $result | Should BeNullOrEmpty } It "Format-Table with Null Objects for End-To-End should work"{ $p = $null - $result = $p|Format-Table -Property "foo","bar"|Out-String + $result = $p | Format-Table -Property "foo","bar" | Out-String $result | Should BeNullOrEmpty } - #pending on issue#900 - It "Format-Table with single line string for End-To-End should work" -pending{ + It "Format-Table with single line string for End-To-End should work" { $p = "single line string" - $result = $p|Format-Table -Property "foo","bar"|Out-String - $result | Should BeNullOrEmpty + $result = $p | Format-Table -Property "foo","bar" -Force | Out-String + $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" } - #pending on issue#900 - It "Format-Table with multiple line string for End-To-End should work" -pending{ + It "Format-Table with multiple line string for End-To-End should work" { $p = "Line1\nLine2" - $result = $p|Format-Table -Property "foo","bar"|Out-String - $result | Should BeNullOrEmpty + $result = $p | Format-Table -Property "foo","bar" -Force | Out-String + $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" } - #pending on issue#900 - It "Format-Table with string sequence for End-To-End should work" -pending{ + It "Format-Table with string sequence for End-To-End should work" { $p = "Line1","Line2" - $result = $p|Format-Table -Property "foo","bar"|Out-String - $result | Should BeNullOrEmpty - } - - #pending on issue#900 - It "Format-Table with string sequence for End-To-End should work" -pending{ - $p = "Line1","Line2" - $result = $p|Format-Table -Property "foo","bar"|Out-String - $result | Should BeNullOrEmpty + $result = $p | Format-Table -Property "foo","bar" -Force | Out-String + $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" } It "Format-Table with complex object for End-To-End should work" { @@ -211,4 +201,14 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" { $result3 | Should Match "name\s+sub" $result3 | Should Match "this is name\s+{x 0 y 0, x 1 y 1}" } + + It "Format-Table should not add trailing whitespace to the header" { + $out = "hello" | Format-Table -Property foo -Force | Out-String + $out.Replace([System.Environment]::NewLine, "") | Should BeExactly "foo---" + } + + It "Format-Table should not add trailing whitespace to rows" { + $out = [pscustomobject]@{a=1;b=2} | Format-Table -HideTableHeaders | Out-String + $out.Replace([System.Environment]::NewLine, "") | Should BeExactly "1 2" + } } From 5a1cd966404c128fb96243f030a2dddd0060da1e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 28 Nov 2017 16:00:50 -0800 Subject: [PATCH 2/4] fix out-file tests to new behavior of no padding trailing spaces --- .../Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 index 38704ad782e..6d6b74a061e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 @@ -98,8 +98,8 @@ Describe "Out-File" -Tags "CI" { $actual = Get-Content $testfile $actual[0] | Should Be "" - $actual[1] | Should Be "text " - $actual[2] | Should Be "---- " + $actual[1] | Should Be "text" + $actual[2] | Should Be "----" $actual[3] | Should Be "some te..." } @@ -129,7 +129,7 @@ Describe "Out-File" -Tags "CI" { # reset to not read only so it can be deleted Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false } - + It "Should be able to use the 'Path' alias for the 'FilePath' parameter" { { Out-File -Path $testfile } | Should Not Throw } From 06ba88546d8391dcb8da0eabd91f83cd37fe6add Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 6 Feb 2018 22:42:08 -0800 Subject: [PATCH 3/4] address PR feedback --- .../Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 16b24687835..f721eee39bc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -156,7 +156,7 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" { } It "Format-Table with multiple line string for End-To-End should work" { - $p = "Line1\nLine2" + $p = "Line1`nLine2" $result = $p | Format-Table -Property "foo","bar" -Force | Out-String $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" } From 4b69e9ada1739f60e16bd3b7b1e0eae78f69a0d7 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 13 Feb 2018 14:48:36 -0800 Subject: [PATCH 4/4] address Jim's feedback refactor existing tests to have better description and use -TestCases --- .../Format-Table.Tests.ps1 | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) 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 f721eee39bc..62f83d5e9a4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -137,34 +137,23 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" { $result | Should Match "Jim\s+5678\s+False" } - It "Format-Table with No Objects for End-To-End should work"{ - $p = @{} - $result = $p | Format-Table -Property "foo","bar" | Out-String + It "Format-Table with '' should return `$null" -TestCases @( + @{ testName = "empty array"; testObject = @{} }, + @{ testName = "null" ; testObject = $null } + ) { + param ($testObject) + $result = $testObject | Format-Table -Property "foo","bar" | Out-String $result | Should BeNullOrEmpty } - It "Format-Table with Null Objects for End-To-End should work"{ - $p = $null - $result = $p | Format-Table -Property "foo","bar" | Out-String - $result | Should BeNullOrEmpty - } - - It "Format-Table with single line string for End-To-End should work" { - $p = "single line string" - $result = $p | Format-Table -Property "foo","bar" -Force | Out-String - $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" - } - - It "Format-Table with multiple line string for End-To-End should work" { - $p = "Line1`nLine2" - $result = $p | Format-Table -Property "foo","bar" -Force | Out-String - $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" - } - - It "Format-Table with string sequence for End-To-End should work" { - $p = "Line1","Line2" - $result = $p | Format-Table -Property "foo","bar" -Force | Out-String - $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" + It "Format-Table with '' string and -Force should output table with requested properties" -TestCases @( + @{ testName = "single line"; testString = "single line string" }, + @{ testName = "multi line" ; testString = "line1`nline2" }, + @{ testName = "array" ; testString = "line1","line2" } + ) { + param ($testString) + $result = $testString | Format-Table -Property "foo","bar" -Force | Out-String + $result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------" } It "Format-Table with complex object for End-To-End should work" {