From e3126e3dc664201b3132c8a2ac7a18dc9eeb199e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Oct 2017 09:43:28 -0700 Subject: [PATCH 1/3] [feature] fix detection of whether `-LiteralPath` was used to suppress wildcardexpansion --- .../commands/management/Navigation.cs | 2 +- .../FileSystem.Tests.ps1 | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs index 3f80cc59bb3..4b22d9150bc 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs @@ -3107,7 +3107,7 @@ protected override void ProcessRecord() try { resolvedPSPaths = SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext); - if (null != LiteralPath && 0 == resolvedPSPaths.Count) + if (true == SuppressWildcardExpansion && 0 == resolvedPSPaths.Count) { ItemNotFoundException pathNotFound = new ItemNotFoundException( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 909cc24de08..45b9df7988e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -370,10 +370,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" { + $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 + } } } @@ -989,7 +1004,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 } @@ -1009,6 +1024,16 @@ 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" { + { Remove-Item "TestDrive:\*.foo" } | Should Not Throw + } } Context "Valdiate Set-Content parameters" { From ceb9efa328e7469eca2e871004c783d4700c7e77 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Oct 2017 10:50:06 -0700 Subject: [PATCH 2/3] [feature] skip -literalpath test with asterisk in filename as that's not valid on Windows --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 45b9df7988e..1e2b54dc005 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -377,7 +377,7 @@ Describe "Handling of globbing patterns" -Tags "CI" { { 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" { + 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 From d6cdf0daa0f953f565e1ad965fc1997be91e47b5 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Oct 2017 11:35:14 -0700 Subject: [PATCH 3/3] [feature] added more variations of tests --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 1e2b54dc005..1e5262afac1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -1031,8 +1031,13 @@ Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature $result | Should BeNullOrEmpty } - It "Verify no error if wildcard doesn't match" { - { Remove-Item "TestDrive:\*.foo" } | Should Not Throw + It "Verify no error if wildcard doesn't match: " -TestCases @( + @{path="TestDrive:\*.foo"}, + @{path="TestDrive:\[z]"}, + @{path="TestDrive:\z.*"} + ) { + param($path) + { Remove-Item $path } | Should Not Throw } }