From 13130954c6751cacdd3fa668f112856cf5fc2a6a Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 18 Oct 2018 22:48:21 -0400 Subject: [PATCH 01/15] Add empty / null path handling to Test-Path --- .../commands/management/PingPathCommand.cs | 81 +++++++++++-------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index 3d50ea95737..3c6ba088741 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -41,6 +41,9 @@ public class TestPathCommand : CoreCommandWithCredentialsBase /// [Parameter(Position = 0, ParameterSetName = "Path", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] + [AllowNull] + [AllowEmptyCollection] + [AllowEmptyString] public string[] Path { get { return _paths; } @@ -53,6 +56,9 @@ public string[] Path [Parameter(ParameterSetName = "LiteralPath", Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)] [Alias("PSPath", "LP")] + [AllowNull] + [AllowEmptyCollection] + [AllowEmptyString] public string[] LiteralPath { get { return _paths; } @@ -156,49 +162,60 @@ protected override void ProcessRecord() { CmdletProviderContext currentContext = CmdletProviderContext; - foreach (string path in _paths) + if (_paths != null && _paths.Length != 0) { - bool result = false; - - try + foreach (string path in _paths) { - if (IsValid) - { - result = SessionState.Path.IsValid(path, currentContext); - } - else + bool result = false; + + if (!string.IsNullOrWhiteSpace(path)) { - if (this.PathType == TestPathType.Container) + try { - result = InvokeProvider.Item.IsContainer(path, currentContext); + if (IsValid) + { + result = SessionState.Path.IsValid(path, currentContext); + } + else + { + if (this.PathType == TestPathType.Container) + { + result = InvokeProvider.Item.IsContainer(path, currentContext); + } + else if (this.PathType == TestPathType.Leaf) + { + result = + InvokeProvider.Item.Exists(path, currentContext) && + !InvokeProvider.Item.IsContainer(path, currentContext); + } + else + { + result = InvokeProvider.Item.Exists(path, currentContext); + } + } } - else if (this.PathType == TestPathType.Leaf) + + // Any of the known exceptions means the path does not exist. + catch (PSNotSupportedException) { - result = - InvokeProvider.Item.Exists(path, currentContext) && - !InvokeProvider.Item.IsContainer(path, currentContext); } - else + catch (DriveNotFoundException) + { + } + catch (ProviderNotFoundException) + { + } + catch (ItemNotFoundException) { - result = InvokeProvider.Item.Exists(path, currentContext); } } - } - // Any of the known exceptions means the path does not exist. - catch (PSNotSupportedException) - { - } - catch (DriveNotFoundException) - { - } - catch (ProviderNotFoundException) - { - } - catch (ItemNotFoundException) - { - } - WriteObject(result); + WriteObject(result); + } + } + else + { + WriteObject(false); } } // ProcessRecord #endregion Command code From 7d5735a2f0a2b4726f207065284517dd93317ec6 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 18 Oct 2018 23:14:41 -0400 Subject: [PATCH 02/15] Update tests to fail correctly under the sole remaining erroring condition --- .../powershell/Language/Scripting/ErrorPosition.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 b/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 index 62777bda32e..c2d6add45d0 100644 --- a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 +++ b/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 @@ -17,7 +17,7 @@ for ($null[0]; '@ $for_pipeline_initializer_script = @' $test = 1 -for (Test-Path $null; +for (Test-Path $null, $null; $test -gt 1;) { } '@ $for_expression_condition_script = @' @@ -27,7 +27,7 @@ for (;$null[0];) '@ $for_pipeline_condition_script = @' $test = 1 -for (;Test-Path $null;) +for (;Test-Path $null, $null;) { } '@ $do_while_expression_condition_script = @' @@ -38,7 +38,7 @@ while ($null[0]) $do_while_pipeline_condition_script = @' $test = 1 do {} -while (Test-Path $null) +while (Test-Path $null, $null) '@ $do_until_expression_condition_script = @' $test = 1 @@ -48,7 +48,7 @@ until ($null[0]) $do_until_pipeline_condition_script = @' $test = 1 do {} -until (Test-Path $null) +until (Test-Path $null, $null) '@ $testCases = @( From 2f41783737cbd4c8e69162c92a30a750f870538b Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 19 Oct 2018 08:29:36 -0400 Subject: [PATCH 03/15] Use quick-exit code paths instead of lengthy if/else --- .../commands/management/PingPathCommand.cs | 85 ++++++++++--------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index 3c6ba088741..c7f50ee1c20 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -160,63 +160,66 @@ internal override object GetDynamicParameters(CmdletProviderContext context) /// protected override void ProcessRecord() { + if (_paths == null || _paths.Length == 0) + { + WriteObject(false); + return; + } + CmdletProviderContext currentContext = CmdletProviderContext; - if (_paths != null && _paths.Length != 0) + foreach (string path in _paths) { - foreach (string path in _paths) + bool result = false; + + if (string.IsNullOrWhiteSpace(path)) { - bool result = false; + WriteObject(result); + continue; + } - if (!string.IsNullOrWhiteSpace(path)) + try + { + if (IsValid) { - try - { - if (IsValid) - { - result = SessionState.Path.IsValid(path, currentContext); - } - else - { - if (this.PathType == TestPathType.Container) - { - result = InvokeProvider.Item.IsContainer(path, currentContext); - } - else if (this.PathType == TestPathType.Leaf) - { - result = - InvokeProvider.Item.Exists(path, currentContext) && - !InvokeProvider.Item.IsContainer(path, currentContext); - } - else - { - result = InvokeProvider.Item.Exists(path, currentContext); - } - } - } - - // Any of the known exceptions means the path does not exist. - catch (PSNotSupportedException) - { - } - catch (DriveNotFoundException) + result = SessionState.Path.IsValid(path, currentContext); + } + else + { + if (this.PathType == TestPathType.Container) { + result = InvokeProvider.Item.IsContainer(path, currentContext); } - catch (ProviderNotFoundException) + else if (this.PathType == TestPathType.Leaf) { + result = + InvokeProvider.Item.Exists(path, currentContext) && + !InvokeProvider.Item.IsContainer(path, currentContext); } - catch (ItemNotFoundException) + else { + result = InvokeProvider.Item.Exists(path, currentContext); } } + } - WriteObject(result); + // Any of the known exceptions means the path does not exist. + catch (PSNotSupportedException) + { } + catch (DriveNotFoundException) + { + } + catch (ProviderNotFoundException) + { + } + catch (ItemNotFoundException) + { + } + + WriteObject(result); } - else - { - WriteObject(false); - } + } // ProcessRecord #endregion Command code From 44417f5d806a894fba23ec6dbedb6cec77a8491c Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 25 Oct 2018 00:14:04 -0400 Subject: [PATCH 04/15] Add non-terminating errors for null input $false will be output if it encounters a non-null but empty string. --- .../commands/management/PingPathCommand.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index c7f50ee1c20..931ebb2ec6a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Management.Automation; using Dbg = System.Management.Automation; @@ -130,7 +131,7 @@ internal override object GetDynamicParameters(CmdletProviderContext context) if (this.PathType == TestPathType.Any && !IsValid) { - if (Path != null && Path.Length > 0) + if (Path != null && Path.Length > 0 && Path[0] != null) { result = InvokeProvider.Item.ItemExistsDynamicParameters(Path[0], context); } @@ -139,6 +140,7 @@ internal override object GetDynamicParameters(CmdletProviderContext context) result = InvokeProvider.Item.ItemExistsDynamicParameters(".", context); } } + return result; } // GetDynamicParameters @@ -162,7 +164,12 @@ protected override void ProcessRecord() { if (_paths == null || _paths.Length == 0) { - WriteObject(false); + WriteError(new ErrorRecord( + new ArgumentNullException("The path was null or an empty collection."), + "NullPath", + ErrorCategory.InvalidArgument, + Path)); + return; } @@ -172,6 +179,16 @@ protected override void ProcessRecord() { bool result = false; + if (path == null) + { + WriteError(new ErrorRecord( + new ArgumentNullException("The path was null or an empty collection."), + "NullPath", + ErrorCategory.InvalidArgument, + Path)); + continue; + } + if (string.IsNullOrWhiteSpace(path)) { WriteObject(result); From 63e1b8ff29716a31e4aa7cc450dc057005adf38e Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 25 Oct 2018 10:48:31 -0400 Subject: [PATCH 05/15] Reformat test file for readability --- .../Test-Path.Tests.ps1 | 88 +++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 694a7ae8606..bd0c6b9267f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -2,123 +2,123 @@ # Licensed under the MIT License. Describe "Test-Path" -Tags "CI" { BeforeAll { - $testdirectory = $TestDrive - $testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force + $testdirectory = $TestDrive + $testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force - # populate with additional files - New-Item -Path $testdirectory -Name datestfile -value 1 -ItemType file | Out-Null - New-Item -Path $testdirectory -Name gatestfile -value 1 -ItemType file | Out-Null - New-Item -Path $testdirectory -Name usr -value 1 -ItemType directory | Out-Null + # populate with additional files + New-Item -Path $testdirectory -Name datestfile -value 1 -ItemType file | Out-Null + New-Item -Path $testdirectory -Name gatestfile -value 1 -ItemType file | Out-Null + New-Item -Path $testdirectory -Name usr -value 1 -ItemType directory | Out-Null - $nonExistentDir = Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin - $nonExistentPath = Join-Path -Path (Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin) -ChildPath error + $nonExistentDir = Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin + $nonExistentPath = Join-Path -Path (Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin) -ChildPath error } It "Should be called on an existing path without error" { - { Test-Path $testdirectory } | Should -Not -Throw - { Test-Path -Path $testdirectory } | Should -Not -Throw - { Test-Path -LiteralPath $testdirectory } | Should -Not -Throw + { Test-Path $testdirectory } | Should -Not -Throw + { Test-Path -Path $testdirectory } | Should -Not -Throw + { Test-Path -LiteralPath $testdirectory } | Should -Not -Throw } It "Should allow piping objects to it" { - { $testdirectory | Test-Path } | Should -Not -Throw + { $testdirectory | Test-Path } | Should -Not -Throw - $testdirectory | Test-Path | Should -BeTrue - $nonExistentDir | Test-Path | Should -BeFalse + $testdirectory | Test-Path | Should -BeTrue + $nonExistentDir | Test-Path | Should -BeFalse } It "Should be called on a nonexistent path without error" { - { Test-Path -Path $nonExistentPath } | Should -Not -Throw + { Test-Path -Path $nonExistentPath } | Should -Not -Throw } It "Should return false for a nonexistent path" { - Test-Path -Path $nonExistentPath | Should -BeFalse + Test-Path -Path $nonExistentPath | Should -BeFalse } It "Should return true for an existing path" { - { Test-Path -Path $testdirectory } | Should -BeTrue + { Test-Path -Path $testdirectory } | Should -BeTrue } It "Should be able to accept a regular expression" { - { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") } | Should -Not -Throw - { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") } | Should -Not -Throw + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") } | Should -Not -Throw + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") } | Should -Not -Throw } It "Should be able to return the correct result when a regular expression is used" { - Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeTrue - Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]*") | Should -BeTrue + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeTrue + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]*") | Should -BeTrue - Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "aoeu*") | Should -BeFalse - Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[A-Z]") | Should -BeFalse + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "aoeu*") | Should -BeFalse + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[A-Z]") | Should -BeFalse } It "Should return false when the Leaf pathtype is used on a directory" { - Test-Path -Path $testdirectory -PathType Leaf | Should -BeFalse + Test-Path -Path $testdirectory -PathType Leaf | Should -BeFalse } It "Should return true when the Leaf pathtype is used on an existing endpoint" { - Test-Path -Path $testfilename -PathType Leaf | Should -BeTrue + Test-Path -Path $testfilename -PathType Leaf | Should -BeTrue } It "Should return false when the Leaf pathtype is used on a nonexistent file" { - Test-Path -Path "aoeu" -PathType Leaf | Should -BeFalse + Test-Path -Path "aoeu" -PathType Leaf | Should -BeFalse } It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" { - Test-Path -Path $testfilename -Type Leaf | Should -BeTrue + Test-Path -Path $testfilename -Type Leaf | Should -BeTrue } It "Should be able to search multiple regular expressions using the include switch" { - { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Include t* } | Should -BeTrue + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Include t* } | Should -BeTrue } It "Should be able to exclude a regular expression using the exclude switch" { - { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Exclude v* } | Should -BeTrue + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Exclude v* } | Should -BeTrue } It "Should be able to exclude multiple regular expressions using the exclude switch" { - # tests whether there's any files in the usr directory that don't start with 'd' or 'g' - { Test-Path -Path $testfilename -Exclude d*, g* } | Should -BeTrue + # tests whether there's any files in the usr directory that don't start with 'd' or 'g' + { Test-Path -Path $testfilename -Exclude d*, g* } | Should -BeTrue } It "Should return true if the syntax of the path is correct when using the IsValid switch" { - { Test-Path -Path $nonExistentPath -IsValid } | Should -BeTrue + { Test-Path -Path $nonExistentPath -IsValid } | Should -BeTrue } It "Should return false if the syntax of the path is incorrect when using the IsValid switch" { - $badPath = " :;!@#$%^&*(){}?+|_-" - Test-Path -Path $badPath -IsValid | Should -BeFalse + $badPath = " :;!@#$%^&*(){}?+|_-" + Test-Path -Path $badPath -IsValid | Should -BeFalse } It "Should return true on paths containing spaces when the path is surrounded in quotes" { - Test-Path -Path "/totally a valid/path" -IsValid | Should -BeTrue + Test-Path -Path "/totally a valid/path" -IsValid | Should -BeTrue } It "Should throw on paths containing spaces when the path is not surrounded in quotes" { - { Test-Path -Path /a path/without quotes/around/it -IsValid } | Should -Throw -ErrorId "PositionalParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand" + { Test-Path -Path /a path/without quotes/around/it -IsValid } | Should -Throw -ErrorId "PositionalParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand" } It "Should return true if a directory leads or trails with a space when surrounded by quotes" { - Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should -BeTrue + Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should -BeTrue } It "Should return true on a valid path when the LiteralPath switch is used" { - Test-Path -LiteralPath $testfilename | Should -BeTrue + Test-Path -LiteralPath $testfilename | Should -BeTrue } It "Should return false if regular expressions are used with the LiteralPath switch" { - Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeFalse - Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should -BeFalse + Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeFalse + Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should -BeFalse } It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" { - Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeFalse - Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should -BeFalse + Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should -BeFalse + Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should -BeFalse } It "Should return true if used on components other than filesystem objects" { - Test-Path Alias:\gci | Should -BeTrue - Test-Path Env:\PATH | Should -BeTrue + Test-Path Alias:\gci | Should -BeTrue + Test-Path Env:\PATH | Should -BeTrue } } From d793658efe8101a6637c72d40572264dfad0430a Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 25 Oct 2018 11:18:04 -0400 Subject: [PATCH 06/15] Use more descriptive error ID --- .../commands/management/PingPathCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index 931ebb2ec6a..492562c1b7f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -166,7 +166,7 @@ protected override void ProcessRecord() { WriteError(new ErrorRecord( new ArgumentNullException("The path was null or an empty collection."), - "NullPath", + "NullPathNotPermitted", ErrorCategory.InvalidArgument, Path)); From 794e78ba4bae8163c269af3f59d860f117644761 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 25 Oct 2018 11:18:13 -0400 Subject: [PATCH 07/15] Add Tests for new behaviour --- .../Test-Path.Tests.ps1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index bd0c6b9267f..47108dfc03f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -39,6 +39,19 @@ Describe "Test-Path" -Tags "CI" { { Test-Path -Path $testdirectory } | Should -BeTrue } + It 'Should return false for an empty string' { + Test-Path -Path '' | Should -BeFalse + } + + It 'Should return false for a whitespace string' { + Test-Path -Path ' ' | Should -BeFalse + } + + It 'Should write a non-terminating error when given a null path' { + { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' + { Test-Path -Path $null -ErrorAction SilentlyContinue } | Should -Not -Throw + } + It "Should be able to accept a regular expression" { { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") } | Should -Not -Throw { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") } | Should -Not -Throw From cae5294b03a39ca295e65789485076f380d21a6b Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 25 Oct 2018 11:23:36 -0400 Subject: [PATCH 08/15] Remove unnecessary blank line per StyleCop --- .../commands/management/PingPathCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index 492562c1b7f..d1bfd9c6131 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -236,7 +236,6 @@ protected override void ProcessRecord() WriteObject(result); } - } // ProcessRecord #endregion Command code From 60810ca085948377dd419b715fae724c5e4a9104 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 30 Oct 2018 16:30:27 -0400 Subject: [PATCH 09/15] Add new tests Add comment to confusing but necessary test pattern --- .../Microsoft.PowerShell.Management/Test-Path.Tests.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 47108dfc03f..488a9ee1de9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -48,8 +48,15 @@ Describe "Test-Path" -Tags "CI" { } It 'Should write a non-terminating error when given a null path' { - { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' + # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null -ErrorAction SilentlyContinue } | Should -Not -Throw + { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' + } + + It 'Should write a non-terminating error when given a null path' { + # This ensures the error is non-terminating; a terminating error would fail the first test + { Test-Path -Path $null, $null -ErrorAction SilentlyContinue } | Should -Not -Throw + { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' } It "Should be able to accept a regular expression" { From e731fabfbf2a52d37dd64645121054cfafb3b54f Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 30 Oct 2018 16:30:51 -0400 Subject: [PATCH 10/15] Clarify it name --- .../Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 488a9ee1de9..6e2f0d45cbd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -53,7 +53,7 @@ Describe "Test-Path" -Tags "CI" { { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' } - It 'Should write a non-terminating error when given a null path' { + It 'Should write a non-terminating error when given an array of null paths' { # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null, $null -ErrorAction SilentlyContinue } | Should -Not -Throw { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' From 2008e5174ca6028086a693874ee23768050d7d31 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 30 Oct 2018 16:44:29 -0400 Subject: [PATCH 11/15] Fix endless loop caused by change of error type in tests --- .../powershell/Language/Scripting/ErrorPosition.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 b/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 index c2d6add45d0..1853e9cba46 100644 --- a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 +++ b/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 @@ -17,7 +17,7 @@ for ($null[0]; '@ $for_pipeline_initializer_script = @' $test = 1 -for (Test-Path $null, $null; +for (Test-Path $null -ErrorAction Stop; $test -gt 1;) { } '@ $for_expression_condition_script = @' @@ -27,7 +27,7 @@ for (;$null[0];) '@ $for_pipeline_condition_script = @' $test = 1 -for (;Test-Path $null, $null;) +for (;Test-Path $null -ErrorAction Stop;) { } '@ $do_while_expression_condition_script = @' @@ -38,7 +38,7 @@ while ($null[0]) $do_while_pipeline_condition_script = @' $test = 1 do {} -while (Test-Path $null, $null) +while (Test-Path $null -ErrorAction Stop) '@ $do_until_expression_condition_script = @' $test = 1 @@ -48,7 +48,7 @@ until ($null[0]) $do_until_pipeline_condition_script = @' $test = 1 do {} -until (Test-Path $null, $null) +until (Test-Path $null -ErrorAction Stop) '@ $testCases = @( From 28cc91f9225f2f950c436344244bdb0005234eff Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 30 Oct 2018 20:26:50 -0400 Subject: [PATCH 12/15] Fix FullyQualifiedErrorID --- .../Microsoft.PowerShell.Management/Test-Path.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 6e2f0d45cbd..dcc2397ea6f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -50,13 +50,13 @@ Describe "Test-Path" -Tags "CI" { It 'Should write a non-terminating error when given a null path' { # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null -ErrorAction SilentlyContinue } | Should -Not -Throw - { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' + { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPath,Microsoft.PowerShell.Commands.TestPathCommand' } It 'Should write a non-terminating error when given an array of null paths' { # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null, $null -ErrorAction SilentlyContinue } | Should -Not -Throw - { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' + { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPath,Microsoft.PowerShell.Commands.TestPathCommand' } It "Should be able to accept a regular expression" { From 0be7bf1f5a56edede09cc0d001b1b1e7e6cd4bfb Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 30 Oct 2018 21:01:39 -0400 Subject: [PATCH 13/15] Consolidate ErrorIDs to match --- .../commands/management/PingPathCommand.cs | 2 +- .../Microsoft.PowerShell.Management/Test-Path.Tests.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index d1bfd9c6131..3ae97a77a5b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -183,7 +183,7 @@ protected override void ProcessRecord() { WriteError(new ErrorRecord( new ArgumentNullException("The path was null or an empty collection."), - "NullPath", + "NullPathNotPermitted", ErrorCategory.InvalidArgument, Path)); continue; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index dcc2397ea6f..48a80d8e0b8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -50,13 +50,13 @@ Describe "Test-Path" -Tags "CI" { It 'Should write a non-terminating error when given a null path' { # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null -ErrorAction SilentlyContinue } | Should -Not -Throw - { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPath,Microsoft.PowerShell.Commands.TestPathCommand' + { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted,Microsoft.PowerShell.Commands.TestPathCommand' } It 'Should write a non-terminating error when given an array of null paths' { # This ensures the error is non-terminating; a terminating error would fail the first test { Test-Path -Path $null, $null -ErrorAction SilentlyContinue } | Should -Not -Throw - { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPath,Microsoft.PowerShell.Commands.TestPathCommand' + { Test-Path -Path $null, $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted,Microsoft.PowerShell.Commands.TestPathCommand' } It "Should be able to accept a regular expression" { From 890fcb12b5437384183b34afe3ac48aad4fd816d Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 15 Nov 2018 21:34:22 -0500 Subject: [PATCH 14/15] Add .resx file for TestPath --- .../resources/TestPathResources.resx | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/Microsoft.PowerShell.Commands.Management/resources/TestPathResources.resx diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/TestPathResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/TestPathResources.resx new file mode 100644 index 00000000000..c60c4318a64 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Management/resources/TestPathResources.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + The provided Path argument was null or an empty collection. + + From cddfb790d796ab53057ec07f5620c0664ac0c724 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 15 Nov 2018 21:34:38 -0500 Subject: [PATCH 15/15] Use resource file for error string --- .../commands/management/PingPathCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs index 3ae97a77a5b..e76695a9047 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/PingPathCommand.cs @@ -165,7 +165,7 @@ protected override void ProcessRecord() if (_paths == null || _paths.Length == 0) { WriteError(new ErrorRecord( - new ArgumentNullException("The path was null or an empty collection."), + new ArgumentNullException(TestPathResources.PathIsNullOrEmptyCollection), "NullPathNotPermitted", ErrorCategory.InvalidArgument, Path)); @@ -182,7 +182,7 @@ protected override void ProcessRecord() if (path == null) { WriteError(new ErrorRecord( - new ArgumentNullException("The path was null or an empty collection."), + new ArgumentNullException(TestPathResources.PathIsNullOrEmptyCollection), "NullPathNotPermitted", ErrorCategory.InvalidArgument, Path));