From f0133bed8e658d200c3301f397bd1de366c3adc6 Mon Sep 17 00:00:00 2001 From: kwkam Date: Sat, 1 Jun 2019 01:14:39 +0800 Subject: [PATCH] Fix `Get-ChildItem -Path` with wildcard char (#9257) Unescape non-literal, non-glob path before existence checking. --- .../engine/SessionStateContainer.cs | 4 ++-- .../Get-ChildItem.Tests.ps1 | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/SessionStateContainer.cs b/src/System.Management.Automation/engine/SessionStateContainer.cs index 66c210d822..aa32e15122 100644 --- a/src/System.Management.Automation/engine/SessionStateContainer.cs +++ b/src/System.Management.Automation/engine/SessionStateContainer.cs @@ -1478,7 +1478,7 @@ namespace System.Management.Automation string originalPath = path; path = Globber.GetProviderPath( - path, + context.SuppressWildcardExpansion ? path : WildcardPattern.Unescape(path), context, out provider, out drive); @@ -2441,7 +2441,7 @@ namespace System.Management.Automation string providerPath = Globber.GetProviderPath( - path, + context.SuppressWildcardExpansion ? path : WildcardPattern.Unescape(path), context, out provider, out drive); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 index 6a1d791ed2..fa91e5a132 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 @@ -196,6 +196,22 @@ Describe "Get-ChildItem" -Tags "CI" { } } +Describe "Get-ChildItem with special path" -Tags "CI" { + + BeforeAll { + $bracketDirName = "Test[Dir]" + $bracketDir = "Test``[Dir``]" + $bracketPath = Join-Path $TestDrive $bracketDir + $null = New-Item -Path $TestDrive -Name $bracketDirName -ItemType Directory -Force + } + + It "Should list files in directory with name containing bracket char" { + $null = New-Item -Path $bracketPath -Name file1.txt -ItemType File + $null = New-Item -Path $bracketPath -Name file2.txt -ItemType File + Get-ChildItem -Path $bracketPath | Should -HaveCount 2 + } +} + Describe 'FileSystem Provider Formatting' -Tag "CI","RequireAdminOnWindows" { BeforeAll {