diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 2c4ade7cb5f..df418d2f22c 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -282,7 +282,7 @@ public static string Escape(string pattern) /// /// True if the string has wild card chars, false otherwise.. /// - /// Currently { '*', '?', '[' } are considered wild card chars and + /// Currently { '*', '?', '[', ']' } are considered wild card chars and /// '`' is the escape character. /// public static bool ContainsWildcardCharacters(string pattern) @@ -293,24 +293,43 @@ public static bool ContainsWildcardCharacters(string pattern) } bool result = false; + bool leftBracket = false; for (int index = 0; index < pattern.Length; ++index) { if (IsWildcardChar(pattern[index])) { + // If '[' or ']' is present alone, then it should not be treated as wildcard chars + // + // Check if both the '[' and the ']' brackets are present. + // If present they should be in order, first '[' and then ']' + // Otherwise, just treat ('[' or ']') as normal literal + + // Check if we got '[' + if (pattern[index] == '[') + { + // got '['. + leftBracket = true; + continue; + } + else if (!leftBracket && pattern[index] == ']') + { + // got the ']' without the '['. Ignore the ']' and continue + continue; + } + result = true; break; } // If it is an escape character then advance past // the next character - if (pattern[index] == escapeChar) { ++index; } } - + return result; } @@ -1347,4 +1366,3 @@ internal static string Parse(WildcardPattern wildcardPattern) } } } - diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 48a80d8e0b8..be051b221dc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -2,6 +2,31 @@ # Licensed under the MIT License. Describe "Test-Path" -Tags "CI" { BeforeAll { + $testDirnames = @( + 'WildCardCommandA' + '[' + 'goose]' + 'du[[ck' + 'du][ck' + 'duck]]' + ) + + $testFailDirnames = @( + 'WildCardCommand' + ']' + 'goos]e' + 'du[ck' + 'du]ck' + 'du]]ck' + 'duc]k]' + ) + + foreach($currentDir in $testDirNames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + New-Item -Path $expandedFile -ItemType Directory + } + $testdirectory = $TestDrive $testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force @@ -141,4 +166,17 @@ Describe "Test-Path" -Tags "CI" { Test-Path Env:\PATH | Should -BeTrue } + It "Validate Test-Path scenario where DestinationPath has incomplete brackets" { + foreach($currentDir in $testDirNames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + Test-Path -Path $expandedFile | Should Be $True + } + + foreach($currentDir in $testFailDirnames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + Test-Path -Path $expandedFile | Should Be $False + } + } }