From 65babca814a252b1295e9423fe952e8da2288247 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 11 Sep 2018 08:12:24 +0500 Subject: [PATCH] Revert "Only display properties with values for MeasureInfo (#7104)" This reverts commit 87ccd0aa40a064238567c400b4e4b6a80802211c until security review of the public api has been completed. --- .../PowerShellCore_format_ps1xml.cs | 18 +++----- .../DisplayDatabase/displayDescriptionData.cs | 22 +--------- .../displayDescriptionData_List.cs | 42 ++++-------------- .../DisplayDatabase/typeDataXmlLoader.cs | 5 --- .../object.tests.ps1 | 43 +++++++------------ 5 files changed, 29 insertions(+), 101 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 010bdca7166..12df579f3c0 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -191,14 +191,6 @@ internal static IEnumerable GetFormatData() "System.Management.Automation.Job", ViewsOf_System_Management_Automation_Job()); - yield return new ExtendedTypeDefinition( - "Microsoft.PowerShell.Commands.TextMeasureInfo", - ViewsOf_Deserialized_Microsoft_PowerShell_Commands_TextMeasureInfo()); - - yield return new ExtendedTypeDefinition( - "Microsoft.PowerShell.Commands.GenericMeasureInfo", - ViewsOf_Deserialized_Microsoft_PowerShell_Commands_GenericMeasureInfo()); - yield return new ExtendedTypeDefinition( "Deserialized.Microsoft.PowerShell.Commands.TextMeasureInfo", ViewsOf_Deserialized_Microsoft_PowerShell_Commands_TextMeasureInfo()); @@ -1078,11 +1070,11 @@ private static IEnumerable ViewsOf_Deserialized_Microsoft_ ListControl.Create() .StartEntry() .AddItemProperty(@"Count") - .AddItemPropertyIfSet(@"Average") - .AddItemPropertyIfSet(@"Sum") - .AddItemPropertyIfSet(@"Maximum") - .AddItemPropertyIfSet(@"Minimum") - .AddItemPropertyIfSet(@"Property") + .AddItemProperty(@"Average") + .AddItemProperty(@"Sum") + .AddItemProperty(@"Maximum") + .AddItemProperty(@"Minimum") + .AddItemProperty(@"Property") .EndEntry() .EndList()); } diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs index 320be2a9254..fb0672ea2f1 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs @@ -788,27 +788,7 @@ public DisplayEntry(string value, DisplayEntryValueType type) ValueType = type; } - /// - /// Creates a DisplayEntry for the given scriptblock. - /// - /// The content of the scriptblock. - /// A for the . - public static DisplayEntry CreateScriptBlockEntry(string scriptblock) - { - return new DisplayEntry(scriptblock, DisplayEntryValueType.ScriptBlock); - } - - /// - /// Creates a DisplayEntry for the given property name. - /// - /// The name of the property. - /// A for the . - public static DisplayEntry CreatePropertyEntry(string property) - { - return new DisplayEntry(property, DisplayEntryValueType.Property); - } - - /// + /// public override string ToString() { return (ValueType == DisplayEntryValueType.Property ? "property: " : "script: ") + Value; diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs index e3092cc16fa..eec2379bf69 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs @@ -370,57 +370,31 @@ internal ListEntryBuilder(ListControlBuilder listBuilder, ListControlEntry listE _listEntry = listEntry; } - private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format, DisplayEntry itemSelectionCondition) + private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format) { if (string.IsNullOrEmpty(value)) - { throw PSTraceSource.NewArgumentNullException("property"); - } _listEntry.Items.Add(new ListControlEntryItem { DisplayEntry = new DisplayEntry(value, kind), Label = label, - FormatString = format, - ItemSelectionCondition = itemSelectionCondition, + FormatString = format }); return this; } - /// Adds a scriptblock list entry. - /// The content of the scriptblock to add. - /// A label for the entry. - /// A format string for the scriptblock result. - /// A condition that has to be met for the entry to be displayed. - /// A reference to this instance after the append operation has completed. - public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null, DisplayEntry itemSelectionCondition = null) - { - return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format, itemSelectionCondition); - } - - /// Adds a property list entry. - /// The property to add. - /// A label for the entry. - /// A format string for the property value. - /// A condition that has to be met for the entry to be displayed. - /// A reference to this instance after the append operation has completed. - public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null, DisplayEntry itemSelectionCondition = null) + /// + public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null) { - return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition); + return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format); } - /// - /// Adds a property that is displayed if it the property has a value. - /// - /// The property to add. - /// A label for the entry. - /// A format string for the property value. - /// A reference to this instance after the append operation has completed. - public ListEntryBuilder AddItemPropertyIfSet(string property, string label = null, string format = null) + /// + public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null) { - var itemSelectionCondition = DisplayEntry.CreatePropertyEntry(property); - return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition); + return AddItem(property, label, DisplayEntryValueType.Property, format); } /// diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs index 49d96b7f5d6..736bfa1bf2e 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs @@ -883,11 +883,6 @@ private void LoadListControlItemDefinitionsFromObjectModel(ListControlEntryDefin fpt.expression = expression; fpt.fieldFormattingDirective.formatString = listItem.FormatString; lvid.formatTokenList.Add(fpt); - if (listItem.ItemSelectionCondition != null) - { - var conditionToken = LoadExpressionFromObjectModel(listItem.ItemSelectionCondition, viewIndex, typeName); - lvid.conditionToken = conditionToken; - } } if (!String.IsNullOrEmpty(listItem.Label)) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/object.tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/object.tests.ps1 index 003a8fb1150..a69807c95c4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/object.tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/object.tests.ps1 @@ -8,9 +8,9 @@ Describe "Object cmdlets" -Tags "CI" { } It "AsString returns a string" { - $processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString - $result = $processes.Keys | ForEach-Object {$_.GetType()} - $result[0].Name | Should -Be "String" + $processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString + $result = $processes.Keys | ForEach-Object {$_.GetType()} + $result[0].Name | Should -Be "String" } } @@ -45,18 +45,18 @@ Describe "Object cmdlets" -Tags "CI" { $secondObject | Add-Member -NotePropertyName Header -NotePropertyValue $secondValue $testCases = @( - @{ data = @("abc", "ABC", "Def"); min = "abc"; max = "Def"}, + @{ data = @("abc","ABC","Def"); min = "abc"; max = "Def"}, @{ data = @([datetime]::Today, [datetime]::Today.AddDays(-1)); min = ([datetime]::Today.AddDays(-1)).ToString() ; max = [datetime]::Today.ToString() } - @{ data = @(1, 2, 3, "ABC"); min = 1; max = "ABC"}, - @{ data = @(4, 2, 3, "ABC", 1); min = 1; max = "ABC"}, - @{ data = @(4, 2, 3, "ABC", 1, "DEF"); min = 1; max = "DEF"}, - @{ data = @("111 Test", "19"); min = "111 Test"; max = "19"}, + @{ data = @(1,2,3,"ABC"); min = 1; max = "ABC"}, + @{ data = @(4,2,3,"ABC",1); min = 1; max = "ABC"}, + @{ data = @(4,2,3,"ABC",1,"DEF"); min = 1; max = "DEF"}, + @{ data = @("111 Test","19"); min = "111 Test"; max = "19"}, @{ data = @("19", "111 Test"); min = "111 Test"; max = "19"}, - @{ data = @("111 Test", 19); min = "111 Test"; max = 19}, + @{ data = @("111 Test",19); min = "111 Test"; max = 19}, @{ data = @(19, "111 Test"); min = "111 Test"; max = 19}, - @{ data = @(100, 2, 3, "A", 1); min = 1; max = "A"}, - @{ data = @(4, 2, 3, "ABC", 1, "DEF"); min = 1; max = "DEF"}, - @{ data = @("abc", [Datetime]::Today, "def"); min = [Datetime]::Today.ToString(); max = "def"} + @{ data = @(100,2,3, "A", 1); min = 1; max = "A"}, + @{ data = @(4,2,3, "ABC", 1, "DEF"); min = 1; max = "DEF"}, + @{ data = @("abc",[Datetime]::Today,"def"); min = [Datetime]::Today.ToString(); max = "def"} ) } @@ -85,31 +85,18 @@ Describe "Object cmdlets" -Tags "CI" { } It 'returns a GenericMeasureInfoObject' { - $gmi = 1, 2, 3 | measure-object -max -min + $gmi = 1,2,3 | measure-object -max -min $gmi | Should -BeOfType Microsoft.PowerShell.Commands.GenericMeasureInfo } It 'should return correct error for non-numeric input' { - $gmi = "abc", [Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue + $gmi = "abc",[Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue $err | ForEach-Object { $_.FullyQualifiedErrorId | Should -Be 'NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand' } } It 'should have the correct count' { - $gmi = "abc", [Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue + $gmi = "abc",[Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue $gmi.Count | Should -Be 2 } - - It 'should only display fields that are set' { - $text = 1, 2, 3 | Measure-Object -Sum -Average | Format-List | Out-String - $text -match "min|max" | Should -BeFalse - $text -match 'Sum' | Should -BeTrue - $text -match 'Average' | Should -BeTrue - - $text = 1, 2, 3 | Measure-Object -Minimum -Maximum | Format-List | Out-String - $text -match "min" | Should -BeTrue - $text -match "max" | Should -BeTrue - $text -match 'Average' | Should -BeFalse - $text -match 'Sum' | Should -BeFalse - } } }