Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public static string Escape(string pattern)
/// </param>
/// <returns>True if the string has wild card chars, false otherwise..</returns>
/// <remarks>
/// Currently { '*', '?', '[' } are considered wild card chars and
/// Currently { '*', '?', '[', ']' } are considered wild card chars and
/// '`' is the escape character.
/// </remarks>
public static bool ContainsWildcardCharacters(string pattern)
Expand All @@ -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;
}

Expand Down Expand Up @@ -1347,4 +1366,3 @@ internal static string Parse(WildcardPattern wildcardPattern)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
}