Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upTest-Path: Return $false when given an empty or $null -Path/-LiteralPath value #8080
Conversation
vexx32
added some commits
Oct 19, 2018
vexx32
requested review from
adityapatwardhan and
daxian-dbw
as
code owners
Oct 19, 2018
iSazonov
reviewed
Oct 19, 2018
| @@ -156,49 +162,60 @@ protected override void ProcessRecord() | |||
| { | |||
| CmdletProviderContext currentContext = CmdletProviderContext; | |||
|
|
|||
| foreach (string path in _paths) | |||
| if (_paths != null && _paths.Length != 0) | |||
This comment has been minimized.
This comment has been minimized.
iSazonov
Oct 19, 2018
Collaborator
We usually use short code path:
| if (_paths != null && _paths.Length != 0) | |
| if (_paths == null || _paths.Length == 0) | |
| { | |
| return false; | |
| } |
This comment has been minimized.
This comment has been minimized.
vexx32
Oct 19, 2018
Author
Contributor
This is in the ProcessRecord() method, so returning false is not an option. I suppose WriteObject(false); return; is the most appropriate short-cut method here. Thank you!
This comment has been minimized.
This comment has been minimized.
|
@SteveL-MSFT Is this a breaking change? |
SteveL-MSFT
added
Breaking-Change
Review - Committee
labels
Oct 19, 2018
vexx32
changed the title
Add empty / null path handling to Test-Path
Test-Path: Return $false when given an empty or $null -Path/-LiteralPath value
Oct 19, 2018
This comment has been minimized.
This comment has been minimized.
|
@PowerShell/powershell-committee reviewed this, we believe the correct behavior is to accept null/nullcollection, but return non-terminating error at runtime (instead of exception or $false) |
SteveL-MSFT
added
Committee-Reviewed
and removed
Review - Committee
labels
Oct 24, 2018
This comment has been minimized.
This comment has been minimized.
|
@vexx32 Please continue and open new issue in PowerShell-Docs. |
iSazonov
self-assigned this
Oct 25, 2018
This comment has been minimized.
This comment has been minimized.
|
Added non-terminating errors for the following cases, per PowerShell committee recommendation:
Docs issue: MicrosoftDocs/PowerShell-Docs#3165 |
This comment has been minimized.
This comment has been minimized.
|
@vexx32 Please add tests for new behaviors. |
vexx32
added some commits
Oct 25, 2018
iSazonov
reviewed
Oct 25, 2018
|
|
||
| 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 |
This comment has been minimized.
This comment has been minimized.
iSazonov
Oct 25, 2018
Collaborator
Seems we should remove this. I am not sure that it tests "non-terminating"
This comment has been minimized.
This comment has been minimized.
vexx32
Oct 25, 2018
Author
Contributor
That's what I thought, too. Surprisingly, though, -ErrorAction SilentlyContinue will still trigger a | should -throw if and only if the error is terminating to start with (either throw or ThrowTerminatingError())
@indented-automation had to explain that one to me. :)
This comment has been minimized.
This comment has been minimized.
iSazonov
Oct 25, 2018
Collaborator
@adityapatwardhan @JamesWTruher @SteveL-MSFT Can we use the pattern to test non-terminating errors?
This comment has been minimized.
This comment has been minimized.
SteveL-MSFT
Oct 25, 2018
Member
Since this change is explicitly to be a non-terminating error, this pattern seems fine. But should probably be followed up with -ErrorAction Stop to verify the non-terminating error is correct.
This comment has been minimized.
This comment has been minimized.
vexx32
Oct 25, 2018
Author
Contributor
I'm not sure I follow? I don't see a need to double-check it with -ErrorAction Stop
This comment has been minimized.
This comment has been minimized.
| } | ||
|
|
||
| It 'Should write a non-terminating error when given a null path' { | ||
| { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
vexx32
Oct 30, 2018
Author
Contributor
Done. Also had to modify some ErrorPosition tests as they were causing infinite loops when the cmldet simply wrote a non-terminating error.
vexx32
added some commits
Oct 30, 2018
This comment has been minimized.
This comment has been minimized.
We'll get $false without |
This comment has been minimized.
This comment has been minimized.
|
We'll get a non-terminating error per the recommendation. In terms of output this will effectively be treated as Would explicitly outputting |
iSazonov
approved these changes
Oct 31, 2018
This comment has been minimized.
This comment has been minimized.
|
@vexx32 Could you please continue? Update: Sorry, seems my comments was addressed. |
This comment has been minimized.
This comment has been minimized.
|
@SteveL-MSFT @adityapatwardhan Could you please review the small PR? |
vexx32
added some commits
Nov 16, 2018
iSazonov
added
the
CL-General
label
Nov 16, 2018
This comment has been minimized.
This comment has been minimized.
|
MacOS CI failure appears to be temporary and no related to the PR as far as I can see. |
This comment has been minimized.
This comment has been minimized.
|
Restart MacOs CI. |
vexx32 commentedOct 19, 2018
•
edited by iSazonov
PR Summary
Fix #5717.
Fix #8076.
Test-Path is expected to be almost exclusively a True/False response cmdlet, and the cases where it may error or return an unexpected result are a few too many at present (see above issues). This PR allows
$null/ empty value(s) to be passed to Test-Path without throwing parameter binding exceptions. It also adds additional logic to Test-Path such that passing it$null,'', or' '(or any pure whitespace string) returns$falseinstead of throwing an error or returning$trueon a "path" that is nothing but whitespace.New behaviour:
Error conditions:
These are non-terminating errors.
A small handful of tests predicated on Test-Path failing in these cases have been updated to simply transform the error into terminating via -ErrorAction, as it appears that the important part of the test was not Test-Path itself, but throwing a terminating error in specific code strictures.
Tests for the additional behaviours have also been added.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests