From a67f83a1d0b747319bb8549d55b71f41c8eaa947 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sat, 20 May 2023 18:06:08 +1000 Subject: [PATCH 1/5] Added Select-Object CaseInsensitive parameter and tests --- .../commands/utility/Select-Object.cs | 9 ++++++++- .../Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs index 1c45fea699d..e7be96185b7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs @@ -110,6 +110,13 @@ public SwitchParameter Unique private bool _unique; + /// + /// Gets or sets case insensitive switch for string comparison. + /// Used in combination with Unique switch parameter. + /// + [Parameter] + public SwitchParameter CaseInsensitive { get; set; } + /// /// /// @@ -632,7 +639,7 @@ private void FilteredWriteObject(PSObject obj, List addedNotePro bool isObjUnique = true; foreach (UniquePSObjectHelper uniqueObj in _uniques) { - ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, true); + ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, !CaseInsensitive.IsPresent); if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) && (uniqueObj.NotePropertyCount == addedNoteProperties.Count)) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 index 04284453435..d60795cbec3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 @@ -58,6 +58,13 @@ Describe "Select-Object" -Tags "CI" { $result | Should -Be $expected } + It "Should work work correctly with Unique and CaseInsensitive parameter" { + $result = "abc", "Abc" | Select-Object -Unique -CaseInsensitive + + $result.Count | Should -Be 1 + $result | Should -Be "abc" + } + It "Should return correct object with Skip parameter" { $result = $dirObject | Select-Object -Skip $TestLength From 336cc44503c6de9324c90286c922352a4c19bbd1 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sat, 20 May 2023 18:06:38 +1000 Subject: [PATCH 2/5] Added Get-Unique CaseInsensitive parameter and tests --- .../commands/utility/GetUnique.cs | 13 ++++++++-- .../Get-Unique.Tests.ps1 | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs index bcf7e31ccf1..fa0abfef553 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs @@ -50,6 +50,13 @@ public SwitchParameter OnType } private bool _onType = false; + + /// + /// Gets or sets case insensitive switch for string comparison. + /// + [Parameter] + public SwitchParameter CaseInsensitive { get; set; } + #endregion Parameters #region Overrides @@ -77,7 +84,9 @@ protected override void ProcessRecord() if (string.Equals( inputString, _lastObjectAsString, - StringComparison.CurrentCulture)) + CaseInsensitive.IsPresent + ? StringComparison.CurrentCultureIgnoreCase + : StringComparison.CurrentCulture)) { isUnique = false; } @@ -91,7 +100,7 @@ protected override void ProcessRecord() _comparer ??= new ObjectCommandComparer( true, // ascending (doesn't matter) CultureInfo.CurrentCulture, - true); // case-sensitive + !CaseInsensitive.IsPresent); // case-sensitive isUnique = (_comparer.Compare(InputObject, _lastObject) != 0); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 index fa859c9b91b..037120c1074 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 @@ -1,8 +1,12 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. Describe "Get-Unique DRT Unit Tests" -Tags "CI" { - It "Command get-unique works with AsString switch" { + + BeforeAll { $inputArray = "aa","aa","Aa","ba","BA","BA" + } + + It "Command get-unique works with AsString switch" { $results = $inputArray | Get-Unique -AsString $results.Length | Should -Be 4 @@ -17,6 +21,18 @@ Describe "Get-Unique DRT Unit Tests" -Tags "CI" { $results[2] | Should -BeOfType System.String $results[3] | Should -BeOfType System.String } + + It "Command get-unique works with AsString and CaseInsensitive switches" { + $results = $inputArray | Get-Unique -AsString -CaseInsensitive + + $results.Length | Should -Be 2 + + $results[0] | Should -BeExactly "aa" + $results[1] | Should -BeExactly "ba" + + $results[0] | Should -BeOfType System.String + $results[1] | Should -BeOfType System.String + } } Describe "Get-Unique" -Tags "CI" { @@ -26,6 +42,8 @@ Describe "Get-Unique" -Tags "CI" { $expectedOutput1 = 1,2,3,4,5 $collection = "a", "b", "b", "d" $expectedOutput2 = "a", "b", "d" + $collection2 = "a","A", "b", "B" + $expectedOutput3 = "a", "b" } It "Should be able to use the Get-Unique cmdlet without error with inputObject switch" { @@ -61,4 +79,9 @@ Describe "Get-Unique" -Tags "CI" { $actual = $collection | Get-Unique $(Compare-Object $actual $expectedOutput2 -SyncWindow 0).Length | Should -Be 0 } + + It "Should get the unique strings when CaseInsensitive switch is used" { + $actual = $collection2 | Get-Unique -CaseInsensitive + $(Compare-Object $actual $expectedOutput3 -SyncWindow 0).Length | Should -Be 0 + } } From 35f743b778c799f2e97706937eeb0f1266c297e6 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sat, 20 May 2023 18:26:29 +1000 Subject: [PATCH 3/5] Fix codefactor issue --- .../commands/utility/GetUnique.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs index fa0abfef553..52821a4eefb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs @@ -84,9 +84,7 @@ protected override void ProcessRecord() if (string.Equals( inputString, _lastObjectAsString, - CaseInsensitive.IsPresent - ? StringComparison.CurrentCultureIgnoreCase - : StringComparison.CurrentCulture)) + CaseInsensitive.IsPresent ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture)) { isUnique = false; } From 67957a203edb02d630afbe96028b60b515dec0ef Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sun, 21 May 2023 08:57:06 +1000 Subject: [PATCH 4/5] Add named arguements --- .../commands/utility/GetUnique.cs | 4 ++-- .../commands/utility/Select-Object.cs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs index 52821a4eefb..6fd69b65693 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs @@ -96,9 +96,9 @@ protected override void ProcessRecord() else // compare as objects { _comparer ??= new ObjectCommandComparer( - true, // ascending (doesn't matter) + ascending: true, // ascending (doesn't matter) CultureInfo.CurrentCulture, - !CaseInsensitive.IsPresent); // case-sensitive + caseSensitive: !CaseInsensitive.IsPresent); // case-sensitive isUnique = (_comparer.Compare(InputObject, _lastObject) != 0); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs index e7be96185b7..cf39a20a068 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs @@ -639,7 +639,11 @@ private void FilteredWriteObject(PSObject obj, List addedNotePro bool isObjUnique = true; foreach (UniquePSObjectHelper uniqueObj in _uniques) { - ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, !CaseInsensitive.IsPresent); + ObjectCommandComparer comparer = new( + ascending: true, + CultureInfo.CurrentCulture, + caseSensitive: !CaseInsensitive.IsPresent); + if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) && (uniqueObj.NotePropertyCount == addedNoteProperties.Count)) { From f407fe4d525621c0b6735dea4b425006b93efe0b Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sun, 21 May 2023 20:53:48 +1000 Subject: [PATCH 5/5] Remove comments --- .../commands/utility/GetUnique.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs index 6fd69b65693..09bc78d9693 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs @@ -96,9 +96,9 @@ protected override void ProcessRecord() else // compare as objects { _comparer ??= new ObjectCommandComparer( - ascending: true, // ascending (doesn't matter) + ascending: true, CultureInfo.CurrentCulture, - caseSensitive: !CaseInsensitive.IsPresent); // case-sensitive + caseSensitive: !CaseInsensitive.IsPresent); isUnique = (_comparer.Compare(InputObject, _lastObject) != 0); }