-
Notifications
You must be signed in to change notification settings - Fork 8.3k
CommandSearcher: treat invalid wildcard path as literal #7399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -314,6 +314,70 @@ public static bool ContainsWildcardCharacters(string pattern) | |
| return result; | ||
| } | ||
|
|
||
| /// FIXME | ||
| /// For invalid wildcard pattern, it currently only check if there is | ||
| /// unclosed bracket. Need to find a better solution and rewrite this. | ||
| /// | ||
| /// <summary> | ||
| /// Checks to see if the given string might contains wildcard pattern. | ||
| /// </summary> | ||
| /// <param name="pattern"> | ||
| /// String which needs to be checked | ||
| /// </param> | ||
| /// <returns> | ||
| /// true if the string does not contain invalid wildcard pattern, | ||
| /// false otherwise. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Currently string contains { '*', '?' } or both { '[', ']' } is | ||
| /// considered to be properly a wildcard pattern and | ||
| /// '`' is the escape character. | ||
| /// </remarks> | ||
| internal static bool ContainsValidWildcardPattern(string pattern) | ||
| { | ||
| if (string.IsNullOrEmpty(pattern)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| bool hasWildcard = false; | ||
| bool openBracket = false; | ||
|
|
||
| for (int index = 0; index < pattern.Length; ++index) | ||
| { | ||
| switch (pattern[index]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to reuse as much code as possible. Perhaps add an overload of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe there is little benefit as |
||
| { | ||
| case '*': | ||
| case '?': | ||
| hasWildcard = true; | ||
| break; | ||
|
|
||
| case '[': | ||
| hasWildcard = true; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We probably need something like: case '[':
if (openBracket)
{
return false;
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| openBracket = true; | ||
| break; | ||
|
|
||
| case ']': | ||
| // ']' is wildcard only if '[' exists | ||
| // so we do not set hasWildcard here | ||
| openBracket = false; | ||
| break; | ||
|
kwkam marked this conversation as resolved.
Outdated
|
||
|
|
||
| // If it is an escape character then advance past | ||
| // the next character | ||
| case escapeChar: | ||
| ++index; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (openBracket) { | ||
| return false; | ||
| } | ||
|
|
||
| return hasWildcard; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unescapes any escaped characters in the input string. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,13 @@ Describe "Tests Get-Command with relative paths and wildcards" -Tag "CI" { | |
| # Create temporary EXE command files | ||
| $file1 = Setup -f WildCardCommandA.exe -pass | ||
| $file2 = Setup -f WildCardCommand[B].exe -pass | ||
| $file3 = Setup -f [.exe -pass | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are probably best as Pester TestCases: BeforeAll {
$testFileNames = @(
'WildCardCommandA'
'WildCardCommand[B]'
'['
'[a-mt]ook'
'[ab-z]ook'
'[a?b]'
'[a*]x'
'goose]'
'du[[ck]'
'du[ck]]'
) | ForEach-Object { "$_.exe" }
foreach ($testFileName in $testFileNames)
{
Setup -f $testFileName -pass
if ($IsLinux -or $IsMacOS)
{
/bin/chmod 777 $testFileName
}
}
$fileTestCases = $testFileNames | ForEach-Object { @{ Filename = $_ } }
}
...
It "Should find the file <Filename>" -TestCases $fileTestCases {
param([string]$Filename)
$result = Get-Command -Name $Filename -Type Application
$result | Should -Not -BeNullOrEmpty
$result | Should -BeExactly $Filename
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add "du[[ck]" too. And perhaps "du[ck]]" too.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kwkam you're absolutely right that all the cases I suggested are valid wildcard patterns (except I think they're worth adding as positive test cases if possible, since they represent edges. |
||
| #$null = New-Item -ItemType File -Path (Join-Path $TestDrive WildCardCommandA.exe) -ErrorAction Ignore | ||
| #$null = New-Item -ItemType File -Path (Join-Path $TestDRive WildCardCommand[B].exe) -ErrorAction Ignore | ||
| if ( $IsLinux -or $IsMacOS ) { | ||
| /bin/chmod a+rw "$file1" | ||
| /bin/chmod a+rw "$file2" | ||
| /bin/chmod a+rw "$file3" | ||
| } | ||
| $commandInfo = Get-Command Get-Date -ShowCommandInfo | ||
| } | ||
|
|
@@ -54,6 +56,11 @@ Describe "Tests Get-Command with relative paths and wildcards" -Tag "CI" { | |
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -Be WildCardCommand[B].exe | ||
|
|
||
| # This should find the file [.exe | ||
| $result = Get-Command -Name .\[.exe -Type Application | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeExactly [.exe | ||
|
|
||
| Pop-Location | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of extra cases this method should reject:
"[a-mt]ook""[ab-z]ook""[a?b]""[a*]x""goose]"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth putting these into test cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused now. All patterns suggested in the list are valid, would you please explain why should
ContainsValidWildcardPatternreject them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"goose]"is a not a pattern, even though it's valid (not throw when callingWildCardPatter.IsMatch)