fix detection of whether -LiteralPath was used to suppress wildcardexpansion for navigation cmdlets (#5038)

* [feature]
fix detection of whether `-LiteralPath` was used to suppress wildcardexpansion

* [feature]
skip -literalpath test with asterisk in filename as that's not valid on Windows

* [feature]
added more variations of tests
This commit is contained in:
Steve Lee 2017-10-09 09:33:22 -07:00 committed by Aditya Patwardhan
parent 1d5c310897
commit ff59be3c61
2 changed files with 33 additions and 3 deletions

View file

@ -3107,7 +3107,7 @@ namespace Microsoft.PowerShell.Commands
try
{
resolvedPSPaths = SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext);
if (null != LiteralPath && 0 == resolvedPSPaths.Count)
if (true == SuppressWildcardExpansion && 0 == resolvedPSPaths.Count)
{
ItemNotFoundException pathNotFound =
new ItemNotFoundException(

View file

@ -407,10 +407,25 @@ Describe "Handling of globbing patterns" -Tags "CI" {
Copy-Item -LiteralPath $file.FullName -Destination $newPath
Test-Path -LiteralPath $newPath | Should Be $true
}
}
It "Remove-Item -LiteralPath should fail if it contains asterisk" {
Context "Handle asterisks in name" {
It "Remove-Item -LiteralPath should fail if it contains asterisk and file doesn't exist" {
{ Remove-Item -LiteralPath ./foo*.txt -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
}
It "Remove-Item -LiteralPath should succeed for file with asterisk in name" -Skip:($IsWindows) {
$testPath = "$testdrive\foo*"
$testPath2 = "$testdrive\foo*2"
New-Item -Path $testPath -ItemType File
New-Item -Path $testPath2 -ItemType File
Test-Path -LiteralPath $testPath | Should Be $true
Test-Path -LiteralPath $testPath2 | Should Be $true
{ Remove-Item -LiteralPath $testPath } | Should Not Throw
Test-Path -LiteralPath $testPath | Should Be $false
# make sure wildcard wasn't applied so this file should still exist
Test-Path -LiteralPath $testPath2 | Should Be $true
}
}
}
@ -1026,7 +1041,7 @@ Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature
It "Verify Filter" {
Remove-Item "TestDrive:\*" -Filter "*.txt"
$result = Get-Item "*.txt"
$result = Get-Item "TestDrive:\*.txt"
$result | Should BeNullOrEmpty
}
@ -1046,6 +1061,21 @@ Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature
$file1 | Should BeNullOrEmpty
$file2.Name | Should Be $testFile2
}
It "Verify Path can accept wildcard" {
Remove-Item "TestDrive:\*.txt" -Recurse -Force
$result = Get-ChildItem "TestDrive:\*.txt"
$result | Should BeNullOrEmpty
}
It "Verify no error if wildcard doesn't match: <path>" -TestCases @(
@{path="TestDrive:\*.foo"},
@{path="TestDrive:\[z]"},
@{path="TestDrive:\z.*"}
) {
param($path)
{ Remove-Item $path } | Should Not Throw
}
}
Context "Valdiate Set-Content parameters" {