GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Test-Path: Return $false when given an empty or $null -Path/-LiteralPath value by vexx32 · Pull Request #8080 · PowerShell/PowerShell Conversation
|
| CmdletProviderContext currentContext = CmdletProviderContext; | ||
|
|
||
| foreach (string path in _paths) | ||
| if (_paths != null && _paths.Length != 0) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We usually use short code path:
| if (_paths != null && _paths.Length != 0) | |
| if (_paths == null || _paths.Length == 0) | |
| { | |
| return false; | |
| } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Steve Lee (@SteveL-MSFT) Is this a breaking change? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Rain Sallow (/u/ta11ow) (@vexx32) Please continue and open new issue in PowerShell-Docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
$false will be output if it encounters a non-null but empty string.
There was an error while loading. Please reload this page.
|
Added non-terminating errors for the following cases, per PowerShell committee recommendation:
Docs issue: MicrosoftDocs/PowerShell-Docs#3165 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Rain Sallow (/u/ta11ow) (@vexx32) Please add tests for new behaviors. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
| 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 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Seems we should remove this. I am not sure that it tests "non-terminating"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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())
Chris Dent (@indented-automation) had to explain that one to me. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Aditya Patwardhan (@adityapatwardhan) James Truher (@JamesWTruher) Steve Lee (@SteveL-MSFT) Can we use the pattern to test non-terminating errors?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow? I don't see a need to double-check it with -ErrorAction Stop
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to add a comment to describe the pattern.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| It 'Should write a non-terminating error when given a null path' { | ||
| { Test-Path -Path $null -ErrorAction Stop } | Should -Throw -ErrorId 'NullPathNotPermitted' |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I believe we should tests here Test-Path $null, $null;.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Done. Also had to modify some ErrorPosition tests as they were causing infinite loops when the cmldet simply wrote a non-terminating error.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Add comment to confusing but necessary test pattern
We'll get $false without |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
We'll get a non-terminating error per the recommendation. In terms of output this will effectively be treated as Would explicitly outputting |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Rain Sallow (/u/ta11ow) (@vexx32) Could you please continue? Update: Sorry, seems my comments was addressed. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Steve Lee (@SteveL-MSFT) Aditya Patwardhan (@adityapatwardhan) Could you please review the small PR? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
MacOS CI failure appears to be temporary and no related to the PR as far as I can see. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Restart MacOs CI. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Is this change due to be released in PowerShell 7? I haven't seen it in the release notes of the previews released to date, just wanted to check that it's still on course to be included. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
ZenoArrow The fix is in current 7.0 Preview5 and will be in 7.0 GA too. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ilya (@iSazonov) The behaviour seems to be fixed for empty path, but the behaviour is different from what I'd expect for $null values... Am I missing something? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
ZenoArrow apologies! Looks like I neglected to update the PR description properly. The PS committee's decision was that we should maintain the error for $null values. I didn't agree, but that is that they decided and so that is how it ended up being implemented. If you disagree with their decision there (I'm right there with you really) I'd recommend opening an issue so that they can see that the behaviour isn't as desired and we can have them reconvene and reassess. 🙂 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Rain Sallow (/u/ta11ow) (@vexx32) Thanks for the heads up. I can raise a new issue, but it'd effectively just be a duplicate of the issue raised by Kevin Marquette (@KevinMarquette) last year... Regarding returning the non-terminating error, I understand this to a degree, but what I can't understand is why Test-Path can't return more than just the error. Correct me if I'm wrong, but usually error stream output doesn't interfere with the standard output, so in my mind there's no reason not to return both (an error message to the error stream and a "False" value to the standard stream). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It certainly could! But it's not strictly necessary. If you're using Test-Path as a conditional in most cases the non-respose will be treated as $false. For example: if (Test-Path $null) { "true!" } Else { "false!" } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Rain Sallow (/u/ta11ow) (@vexx32) Good to know. In that case the current improvements to Test-Path should cover what I was hoping for. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Test-Path -Path $null should return $false Test-Path returns $true for ' ' on Windows
There was an error while loading. Please reload this page.