New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test-Path -IsValid -PathType Leaf doesn't correctly recognize a leaf #8607

Closed
fullenw1 opened this Issue Jan 8, 2019 · 6 comments

Comments

Projects
None yet
3 participants
@fullenw1
Copy link

fullenw1 commented Jan 8, 2019

This issue is about an incorrect validation of the Test-Path cmdlet.

When a path ends with a backslash it is supposed to be a container.

Furthermore, according to the official documentation:

-IsValid
Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist. This cmdlet returns $True if the path syntax is valid and $False if it is not.

and also

"Leaf. An element that does not contain other elements, such as a file."

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-6

However, when using the -IsValid parameter combined to the -PathType Leaf parameter in order to validate the syntax of a file path, it is incorrectly validated as a leaf.

For example, the following command line returns $true instead of $false.

PS C:\> Test-Path -IsValid -PathType Leaf -Path 'C:\Something\'
True
PS C:\>

It should return false because a leaf path cannot end with a backslash.

This behavior has been tested on PowerShell 5.1 and 6.1.1.

@fullenw1 fullenw1 changed the title Test-Path -IsValid -PathType Leaf doesn't recognize a leaf Test-Path -IsValid -PathType Leaf doesn't correctly recognize a leaf Jan 8, 2019

@BrucePay

This comment has been minimized.

Copy link
Collaborator

BrucePay commented Jan 8, 2019

Hi @fullenw1 There are a couple of things to be aware of here. First, if -IsValid is specified, the -PathType switch is simply ignored and syntactic path validation is the only thing that's done:

PSCore  >  Test-Path -IsValid foo -PathType leaf
True
PSCore  >  Test-Path -IsValid foo -PathType Container
True

Second, when it is used by itself, -PathType tests the type of the object the path resolves to, regardless of the path string:

PSCore  >  test-path -PathType Container $profile/
False
PSCore  >  test-path -PathType Leaf $profile/
True

Likewise, when using the Split-Path cmdlet, the trailing slash is ignored.

PSCore  >  Split-Path -Leaf a\b\c\
c

Arguably -IsValid and -PathType should have been in separate parameter sets but that isn't how it was implemented and it would be a breaking change to do it now.

@fullenw1

This comment has been minimized.

Copy link
Author

fullenw1 commented Jan 8, 2019

Thank you for the explanation.
Until now we could assume that both can be used together, and when not, we had to find out which one is overtaking the other.

The breaking change (puting them in separate parameter sets) in the next version would probably be the logical way to go.
And if not, at least the documentation should be updated to avoid the confusion.

@iSazonov

This comment has been minimized.

Copy link
Collaborator

iSazonov commented Jan 11, 2019

For future breaking change reference #6745

@iSazonov

This comment has been minimized.

Copy link
Collaborator

iSazonov commented Jan 11, 2019

@fullenw1 Please open issue in PowerShell-Docs repo.

@fullenw1

This comment has been minimized.

Copy link
Author

fullenw1 commented Jan 13, 2019

I have created pull request #3537 in the PowerShell-Docs repository.

@iSazonov

This comment has been minimized.

Copy link
Collaborator

iSazonov commented Jan 13, 2019

Close until we want the breaking change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment