From e022ced716c56db072dd925c48643f4188538c12 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 28 Sep 2019 13:25:22 -0700 Subject: [PATCH 1/4] Remove [ValidateNotNullOrEmpty] from -InputObject on Get-Random --- .../commands/utility/GetRandomCommand.cs | 1 - .../Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs index 1a7c4b1a8d6..35d59289470 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs @@ -275,7 +275,6 @@ private double ConvertToDouble(object o, double defaultIfNull) /// List from which random elements are chosen. /// [Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)] - [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public object[] InputObject { get; set; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 index d8ee1ee48bf..7125145a62c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 @@ -182,4 +182,10 @@ Describe "Get-Random" -Tags "CI" { It "Should throw an error because the hexadecimal number is to large " { { Get-Random 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32" } + + It "Should accept collection containing empty string for -InputObject" { + 1..10 | ForEach-Object { + Get-Random -InputObject @('a','b','') | Should -BeIn 'a','b','' + } + } } From caa9ae4b91009beb44378a1f97ade76701f20fb0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 29 Sep 2019 09:12:27 -0700 Subject: [PATCH 2/4] address feedback to allow $null for -InputObject --- .../commands/utility/GetRandomCommand.cs | 4 +++- .../Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs index 35d59289470..8be5b404c71 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs @@ -28,6 +28,7 @@ public class GetRandomCommand : PSCmdlet private const string RandomNumberParameterSet = "RandomNumberParameterSet"; private const string RandomListItemParameterSet = "RandomListItemParameterSet"; + private static object[] _nullInArray = new object[] { null }; private enum MyParameterSet { @@ -275,6 +276,7 @@ private double ConvertToDouble(object o, double defaultIfNull) /// List from which random elements are chosen. /// [Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)] + [System.Management.Automation.AllowNull] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public object[] InputObject { get; set; } @@ -490,7 +492,7 @@ protected override void ProcessRecord() { if (EffectiveParameterSet == MyParameterSet.RandomListItem) { - foreach (object item in InputObject) + foreach (object item in InputObject ?? _nullInArray) { if (_numberOfProcessedListItems < Count) // (3) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 index 7125145a62c..fa835478e98 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 @@ -188,4 +188,10 @@ Describe "Get-Random" -Tags "CI" { Get-Random -InputObject @('a','b','') | Should -BeIn 'a','b','' } } + + It "Should accept `$null in collection for -InputObject" { + 1..10 | ForEach-Object { + Get-Random -InputObject @('a','b',$null) | Should -BeIn 'a','b',$null + } + } } From 77687de819519e2c206031a8586547a558957dfe Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 29 Sep 2019 20:38:20 -0700 Subject: [PATCH 3/4] address Ilya's feedback --- .../commands/utility/GetRandomCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs index 8be5b404c71..420f04d952b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs @@ -28,7 +28,7 @@ public class GetRandomCommand : PSCmdlet private const string RandomNumberParameterSet = "RandomNumberParameterSet"; private const string RandomListItemParameterSet = "RandomListItemParameterSet"; - private static object[] _nullInArray = new object[] { null }; + private readonly static object[] _nullInArray = new object[] { null }; private enum MyParameterSet { @@ -492,7 +492,7 @@ protected override void ProcessRecord() { if (EffectiveParameterSet == MyParameterSet.RandomListItem) { - foreach (object item in InputObject ?? _nullInArray) + foreach (object item in InputObject ?? _nullInArray) // this allows for $null to be in an array passed to InputObject { if (_numberOfProcessedListItems < Count) // (3) { From a3d2cf1d39f66bc8fcc219f2d475142e568e9784 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 29 Sep 2019 21:00:37 -0700 Subject: [PATCH 4/4] address CodeFactor issues --- .../commands/utility/GetRandomCommand.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs index 420f04d952b..f621dd3c7b3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs @@ -28,7 +28,7 @@ public class GetRandomCommand : PSCmdlet private const string RandomNumberParameterSet = "RandomNumberParameterSet"; private const string RandomListItemParameterSet = "RandomListItemParameterSet"; - private readonly static object[] _nullInArray = new object[] { null }; + private static readonly object[] _nullInArray = new object[] { null }; private enum MyParameterSet { @@ -492,9 +492,11 @@ protected override void ProcessRecord() { if (EffectiveParameterSet == MyParameterSet.RandomListItem) { - foreach (object item in InputObject ?? _nullInArray) // this allows for $null to be in an array passed to InputObject + // this allows for $null to be in an array passed to InputObject + foreach (object item in InputObject ?? _nullInArray) { - if (_numberOfProcessedListItems < Count) // (3) + // (3) + if (_numberOfProcessedListItems < Count) { Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems"); _chosenListItems.Add(item); @@ -502,9 +504,12 @@ protected override void ProcessRecord() else { Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K"); - if (Generator.Next(_numberOfProcessedListItems + 1) < Count) // (1) + + // (1) + if (Generator.Next(_numberOfProcessedListItems + 1) < Count) { - int indexToReplace = Generator.Next(_chosenListItems.Count); // (2) + // (2) + int indexToReplace = Generator.Next(_chosenListItems.Count); _chosenListItems[indexToReplace] = item; } }