From 4a43c10a98bc0f2b32cb803665ad6dfc327e53f9 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 11 Mar 2019 12:57:51 +0500 Subject: [PATCH] Add suppoert empty NoteProperty in Group-Object --- .../commands/utility/ObjectCommandComparer.cs | 9 +++++++-- .../Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs index 95aeeff24e1..95b6bb0d289 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs @@ -113,12 +113,17 @@ public override bool Equals(Object inputObject) public override int GetHashCode() { if (PropertyValue == null) + { return 0; + } object baseObject = PSObject.Base(PropertyValue); - IComparable baseObjectComparable = baseObject as IComparable; + if (baseObject == null) + { + return 0; + } - if (baseObjectComparable != null) + if (baseObject is IComparable) { return baseObject.GetHashCode(); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 index becb17b3ff2..09ddcbe74a0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -123,6 +123,13 @@ Describe "Group-Object" -Tags "CI" { $result['Successful'].ErrorMessage.Count | Should -Be 4 $result['Successful'].ErrorMessage[0] | Should -Be '' } + + It "Should understand empty NoteProperty" { + $result = "dummy" | Select-Object -Property @{Name = 'X'; Expression = {}} | Group-Object X + $result.Count | Should -Be 1 + $result[0].Name | Should -Be "" + $result[0].Group | Should -Be '@{X=}' + } } Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort-Object, Compare-Object)" -Tags "CI" {