Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ internal static IEnumerable<ExtendedTypeDefinition> 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());
Expand Down Expand Up @@ -1062,11 +1070,11 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_Deserialized_Microsoft_
ListControl.Create()
.StartEntry()
.AddItemProperty(@"Count")
.AddItemProperty(@"Average")
.AddItemProperty(@"Sum")
.AddItemProperty(@"Maximum")
.AddItemProperty(@"Minimum")
.AddItemProperty(@"Property")
.AddItemPropertyIfSet(@"Average")
.AddItemPropertyIfSet(@"Sum")
.AddItemPropertyIfSet(@"Maximum")
.AddItemPropertyIfSet(@"Minimum")
.AddItemPropertyIfSet(@"Property")
.EndEntry()
.EndList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,27 @@ public DisplayEntry(string value, DisplayEntryValueType type)
ValueType = type;
}

/// <summary/>
/// <summary>
/// Creates a DisplayEntry for the given scriptblock.
/// </summary>
/// <param name="scriptblock">The content of the scriptblock.</param>
/// <returns>A <see cref="DisplayEntry"/> for the <paramref name="scriptblock"/>.</returns>
public static DisplayEntry CreateScriptBlockEntry(string scriptblock)
{
return new DisplayEntry(scriptblock, DisplayEntryValueType.ScriptBlock);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulHigin should review this new public api for any security implications in creating script blocks that might circumvent the language mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulHigin reviewed this and told me he doesn't see a security issue. @PaulHigin can you confirm?

}

/// <summary>
/// Creates a DisplayEntry for the given property name.
/// </summary>
/// <param name="property">The name of the property.</param>
/// <returns>A <see cref="DisplayEntry"/> for the <paramref name="property"/>.</returns>
public static DisplayEntry CreatePropertyEntry(string property)
{
return new DisplayEntry(property, DisplayEntryValueType.Property);
}

/// <inheritdoc />
public override string ToString()
{
return (ValueType == DisplayEntryValueType.Property ? "property: " : "script: ") + Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,31 +370,57 @@ internal ListEntryBuilder(ListControlBuilder listBuilder, ListControlEntry listE
_listEntry = listEntry;
}

private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format)
private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format, DisplayEntry itemSelectionCondition)
{
if (string.IsNullOrEmpty(value))
{
throw PSTraceSource.NewArgumentNullException("property");
}

_listEntry.Items.Add(new ListControlEntryItem
{
DisplayEntry = new DisplayEntry(value, kind),
Label = label,
FormatString = format
FormatString = format,
ItemSelectionCondition = itemSelectionCondition,
});

return this;
}

/// <summary></summary>
public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null)
/// <summary>Adds a scriptblock list entry.</summary>
/// <param name="scriptBlock">The content of the scriptblock to add.</param>
/// <param name="label">A label for the entry.</param>
/// <param name="format">A format string for the scriptblock result.</param>
/// <param name="itemSelectionCondition">A condition that has to be met for the entry to be displayed.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null, DisplayEntry itemSelectionCondition = null)
{
return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format);
return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format, itemSelectionCondition);
}

/// <summary></summary>
public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null)
/// <summary>Adds a property list entry.</summary>
/// <param name="property">The property to add.</param>
/// <param name="label">A label for the entry.</param>
/// <param name="format">A format string for the property value.</param>
/// <param name="itemSelectionCondition">A condition that has to be met for the entry to be displayed.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null, DisplayEntry itemSelectionCondition = null)
{
return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition);
}

/// <summary>
/// Adds a property that is displayed if it the property has a value.
/// </summary>
/// <param name="property">The property to add.</param>
/// <param name="label">A label for the entry.</param>
/// <param name="format">A format string for the property value.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public ListEntryBuilder AddItemPropertyIfSet(string property, string label = null, string format = null)
{
return AddItem(property, label, DisplayEntryValueType.Property, format);
var itemSelectionCondition = DisplayEntry.CreatePropertyEntry(property);
return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition);
}

/// <summary></summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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"}
)
}

Expand Down Expand Up @@ -85,18 +85,31 @@ 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
Copy link
Collaborator

@iSazonov iSazonov Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we skipped

$text -match 'Sum' | Should -BeFalse

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@powercode Please address the comment.

$text -match 'Sum' | Should -BeFalse
}
}
}