From 4e5af528db95e09a1bf81a29d6ca9bf6affc447e Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Sat, 9 Nov 2019 23:52:24 -0500 Subject: [PATCH 1/4] :sparkles: :white_check_mark: Add case sensitivity - Group-Object -AsHashtable -CaseSensitive returns a case-sensitive hash instead of erroring out when given keys differing only by case. - Added tests --- .../commands/utility/Group-Object.cs | 11 ++++++++++- .../Group-Object.Tests.ps1 | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs index 3b9a558663c..97d7dcd2733 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs @@ -518,7 +518,16 @@ protected override void EndProcessing() { if (AsHashTable) { - Hashtable hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable(); + Hashtable hashtable; + if (CaseSensitive.IsPresent) + { + hashtable = new Hashtable(); + } + else + { + hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable(); + } + try { if (AsString) 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 09ddcbe74a0..bc46d376b42 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -173,6 +173,23 @@ Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort- $testCulture = "1049" } - {$testObject | Group-Object -Culture $testCulture } | Should -Not -Throw + { $testObject | Group-Object -Culture $testCulture } | Should -Not -Throw + } + + It 'should not throw a key duplication error with -CaseSensitive -AsHashtable' { + $script = { + $capitonyms = @( + [PSCustomObject]@{ + Capitonym = 'Bill' + } + [PSCustomObject]@{ + Capitonym = 'bill' + } + ) + + $capitonyms | Group-Object Capitonym -AsHashTable -CaseSensitive + } + + $script | Should -Not -Throw } } From 1e510504fc053131174b61907c496ea693b3c013 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Sun, 10 Nov 2019 15:55:38 -0500 Subject: [PATCH 2/4] :recycle: Refactor GroupObjectCommand - Consolidate hashtable creation - Better consistency of handling switches in conditionals --- .../commands/utility/Group-Object.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs index 97d7dcd2733..2759083121e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs @@ -516,18 +516,12 @@ protected override void EndProcessing() s_tracer.WriteLine(_groups.Count); if (_groups.Count > 0) { - if (AsHashTable) + if (AsHashTable.IsPresent) { - Hashtable hashtable; - if (CaseSensitive.IsPresent) - { - hashtable = new Hashtable(); - } - else - { - hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable(); - } - + StringComparer comparer = CaseSensitive.IsPresent + ? StringComparer.CurrentCulture + : StringComparer.CurrentCultureIgnoreCase; + var hashtable = new Hashtable(comparer); try { if (AsString) From c307f2e23f31ac07c4b1a5a0493f43aa8adc55b3 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Sun, 10 Nov 2019 15:55:55 -0500 Subject: [PATCH 3/4] :white_check_mark: Update tests --- .../Group-Object.Tests.ps1 | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) 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 bc46d376b42..f18b6af7785 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -177,19 +177,16 @@ Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort- } It 'should not throw a key duplication error with -CaseSensitive -AsHashtable' { - $script = { - $capitonyms = @( - [PSCustomObject]@{ - Capitonym = 'Bill' - } - [PSCustomObject]@{ - Capitonym = 'bill' - } - ) - - $capitonyms | Group-Object Capitonym -AsHashTable -CaseSensitive - } - - $script | Should -Not -Throw + $capitonyms = @( + [PSCustomObject]@{ + Capitonym = 'Bill' + } + [PSCustomObject]@{ + Capitonym = 'bill' + } + ) + + [hashtable] $Result = $capitonyms | Group-Object -Property Capitonym -AsHashTable -CaseSensitive + $Result.Keys | Should -BeIn @( 'Bill', 'bill' ) } } From f4ad672b3043769add41ddfaa03256c607ec3639 Mon Sep 17 00:00:00 2001 From: "Joel Sallow (/u/ta11ow)" <32407840+vexx32@users.noreply.github.com> Date: Mon, 11 Nov 2019 12:12:23 -0500 Subject: [PATCH 4/4] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 Co-Authored-By: Steve Lee --- .../Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 f18b6af7785..ffca1e254ef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -186,7 +186,8 @@ Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort- } ) - [hashtable] $Result = $capitonyms | Group-Object -Property Capitonym -AsHashTable -CaseSensitive + $Result = $capitonyms | Group-Object -Property Capitonym -AsHashTable -CaseSensitive + $Result | Should -BeOfType [HashTable] $Result.Keys | Should -BeIn @( 'Bill', 'bill' ) } }