Skip to content

Commit 51d2523

Browse files
SteveL-MSFTAndrew
authored and
Andrew
committed
Update formatter to not write newlines if content is empty (PowerShell#11193)
1 parent 2ee1760 commit 51d2523

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,24 @@ internal void WriteObject(List<FormatValue> formatValueList)
4949
// we always start with no indentation
5050
_indentationManager.Clear();
5151

52+
bool hasContent = false;
5253
foreach (FormatEntry fe in formatValueList)
5354
{
54-
// operate on each directive inside the list,
55-
// carrying the indentation from invocation to invocation
56-
GenerateFormatEntryDisplay(fe, 0);
55+
if (fe.formatValueList.Count > 0)
56+
{
57+
hasContent = true;
58+
59+
// operate on each directive inside the list,
60+
// carrying the indentation from invocation to invocation
61+
GenerateFormatEntryDisplay(fe, 0);
62+
}
5763
}
64+
5865
// make sure that, if we have pending text in the buffer it gets flushed
59-
WriteToScreen();
66+
if (hasContent)
67+
{
68+
WriteToScreen();
69+
}
6070
}
6171

6272
/// <summary>

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,10 @@ private void ExecuteFormatTokenList(TraversalInfo level,
281281
// do the formatting and we will be done
282282
if (cpt.control == null || cpt.control is FieldControlBody)
283283
{
284-
// Since it is a leaf node we just consider it an empty string and go
285-
// on with formatting
286284
if (val == null)
287285
{
288-
val = string.Empty;
286+
// Since it is a leaf node we just ignore it
287+
return;
289288
}
290289

291290
FieldFormattingDirective fieldFormattingDirective = null;
@@ -780,4 +779,3 @@ private List<FormatValue> AddIndentationLevel(List<FormatValue> formatValueList)
780779
private int _enumerationLimit;
781780
}
782781
}
783-

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

+66
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,69 @@ SelectScriptBlock
438438
$ps.Streams.Error | Should -BeNullOrEmpty
439439
}
440440
}
441+
442+
Describe "Custom formatting returning nothing" -Tags "CI" {
443+
BeforeAll {
444+
$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
445+
$xmlContent = @'
446+
<Configuration>
447+
<ViewDefinitions>
448+
<View>
449+
<Name>Test</Name>
450+
<ViewSelectedBy>
451+
<TypeName>MyTestObject</TypeName>
452+
</ViewSelectedBy>
453+
<CustomControl>
454+
<CustomEntries>
455+
<CustomEntry>
456+
<CustomItem>
457+
<ExpressionBinding>
458+
<ScriptBlock>
459+
$null
460+
</ScriptBlock>
461+
</ExpressionBinding>
462+
</CustomItem>
463+
</CustomEntry>
464+
</CustomEntries>
465+
</CustomControl>
466+
</View>
467+
</ViewDefinitions>
468+
</Configuration>
469+
'@
470+
471+
Set-Content -Path $formatFilePath -Value $xmlContent
472+
$ps = [powershell]::Create()
473+
$iss = [initialsessionstate]::CreateDefault2()
474+
$iss.Formats.Add($formatFilePath)
475+
$rs = [runspacefactory]::CreateRunspace($iss)
476+
$rs.Open()
477+
$ps.Runspace = $rs
478+
}
479+
480+
AfterAll {
481+
$rs.Dispose()
482+
$ps.Dispose()
483+
}
484+
485+
It 'Newlines are not written if nothing is returned by custom format' {
486+
$script = {
487+
[PSCustomObject]@{
488+
PSTypeName = 'MyTestObject'
489+
Name = 'testing'
490+
}
491+
}
492+
493+
$null = $ps.AddScript($script).AddCommand('Out-String')
494+
$ps.Streams.Error.Clear()
495+
496+
# one newline for start, one for grouping, and one for end
497+
$expectedOutput = @'
498+
499+
500+
501+
'@ -replace '\r?\n', "^"
502+
503+
$ps.Invoke() -replace '\r?\n', "^" | Should -BeExactly $expectedOutput
504+
$ps.Streams.Error | Should -BeNullOrEmpty
505+
}
506+
}

0 commit comments

Comments
 (0)