diff --git a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs index 5f8f245efd9..d43e42ebb2d 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs @@ -49,24 +49,14 @@ internal void WriteObject(List formatValueList) // we always start with no indentation _indentationManager.Clear(); - bool hasContent = false; foreach (FormatEntry fe in formatValueList) { - if (fe.formatValueList.Count > 0) - { - hasContent = true; - - // operate on each directive inside the list, - // carrying the indentation from invocation to invocation - GenerateFormatEntryDisplay(fe, 0); - } + // operate on each directive inside the list, + // carrying the indentation from invocation to invocation + GenerateFormatEntryDisplay(fe, 0); } - // make sure that, if we have pending text in the buffer it gets flushed - if (hasContent) - { - WriteToScreen(); - } + WriteToScreen(); } /// diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs index ff870c996a3..d41a119dde6 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs @@ -281,10 +281,11 @@ private void ExecuteFormatTokenList(TraversalInfo level, // do the formatting and we will be done if (cpt.control == null || cpt.control is FieldControlBody) { + // Since it is a leaf node we just consider it an empty string and go + // on with formatting if (val == null) { - // Since it is a leaf node we just ignore it - return; + val = string.Empty; } FieldFormattingDirective fieldFormattingDirective = null; @@ -779,3 +780,4 @@ private List AddIndentationLevel(List formatValueList) private int _enumerationLimit; } } + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 index 0e1e2dc4d13..a296b5b4590 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 @@ -438,69 +438,3 @@ SelectScriptBlock $ps.Streams.Error | Should -BeNullOrEmpty } } - -Describe "Custom formatting returning nothing" -Tags "CI" { - BeforeAll { - $formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml' - $xmlContent = @' - - - - Test - - MyTestObject - - - - - - - - $null - - - - - - - - - -'@ - - Set-Content -Path $formatFilePath -Value $xmlContent - $ps = [powershell]::Create() - $iss = [initialsessionstate]::CreateDefault2() - $iss.Formats.Add($formatFilePath) - $rs = [runspacefactory]::CreateRunspace($iss) - $rs.Open() - $ps.Runspace = $rs - } - - AfterAll { - $rs.Dispose() - $ps.Dispose() - } - - It 'Newlines are not written if nothing is returned by custom format' { - $script = { - [PSCustomObject]@{ - PSTypeName = 'MyTestObject' - Name = 'testing' - } - } - - $null = $ps.AddScript($script).AddCommand('Out-String') - $ps.Streams.Error.Clear() - - # one newline for start, one for grouping, and one for end - $expectedOutput = @' - - - -'@ -replace '\r?\n', "^" - - $ps.Invoke() -replace '\r?\n', "^" | Should -BeExactly $expectedOutput - $ps.Streams.Error | Should -BeNullOrEmpty - } -}