-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Fix the issue Get-Help does not support string pattern under Unix #3852
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace System.Management.Automation | ||
{ | ||
|
@@ -120,11 +121,25 @@ private string[] GetFiles(string path, string pattern) | |
// extra logic to select the files that match the given pattern. | ||
ArrayList result = new ArrayList(); | ||
string[] files = Directory.GetFiles(path); | ||
|
||
string regexPattern = pattern.Replace(".", @"\."); | ||
regexPattern = regexPattern.Replace("*", ".*"); | ||
regexPattern = regexPattern.Replace("?", ".?"); | ||
|
||
foreach (string filePath in files) | ||
{ | ||
if (filePath.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0) | ||
{ | ||
result.Add(filePath); | ||
break; | ||
} | ||
// If the input is pattern instead of string, we need to use Regex expression. | ||
if (pattern.Contains("*") || pattern.Contains("?")) | ||
{ | ||
if (Regex.IsMatch(filePath, regexPattern)) | ||
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. Why are we using regular expression matching instead of wildcard matching? FWIW - wildcard matching already converts the wildcard to a regex - so this code is probably doing something similar but subtly different. 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 agree. We basically need to implementation 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. |
||
{ | ||
result.Add(filePath); | ||
} | ||
} | ||
} | ||
return (String[])result.ToArray(typeof(string)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,3 +226,45 @@ Describe "Get-Help should find help info within help files" -Tags @('CI', 'Requi | |
} | ||
} | ||
} | ||
|
||
Describe "Get-Help should find pattern help files" -Tags "CI" { | ||
|
||
# There is a bug specific to Travis CI that hangs the test if "get-help" is used to search pattern string. This doesn't repro locally. | ||
# This occurs even if Unix system just returns "Directory.GetFiles(path, pattern);" as the windows' code does. | ||
# Since there's currently no way to get the vm from Travis CI and the test PASSES locally on both Ubuntu and MacOS, excluding pattern test under Unix system. | ||
|
||
BeforeAll { | ||
$helpFile1 = "about_testCase1.help.txt" | ||
$helpFile2 = "about_testCase.2.help.txt" | ||
$culture = (Get-Culture).Name | ||
$helpFolderPath = Join-Path $PSHOME $culture | ||
$helpFilePath1 = Join-Path $helpFolderPath $helpFile1 | ||
$helpFilePath2 = Join-Path $helpFolderPath $helpFile2 | ||
$null = New-Item -ItemType Directory -Path $helpFolderPath -ErrorAction SilentlyContinue -Force | ||
# Create at least one help file matches "about*" pattern | ||
$null = New-Item -ItemType File -Path $helpFilePath1 -Value "about_test1" -ErrorAction SilentlyContinue | ||
$null = New-Item -ItemType File -Path $helpFilePath2 -Value "about_test2" -ErrorAction SilentlyContinue | ||
} | ||
|
||
# Remove the test files | ||
AfterAll { | ||
Remove-Item $helpFilePath1 -Force -ErrorAction SilentlyContinue | ||
Remove-Item $helpFilePath2 -Force -ErrorAction SilentlyContinue | ||
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. $helpFolderPath does not seem to be cleaned up? Maybe you can just delete $helpFolderPath recursively. 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. $helpFolderPath is an existed folder contains other help files. |
||
} | ||
|
||
$testcases = @( | ||
@{command = {Get-Help about_testCas?1}; testname = "test ? pattern"; result = "about_test1"} | ||
@{command = {Get-Help about_testCase.?}; testname = "test ? pattern with dot"; result = "about_test2"} | ||
@{command = {(Get-Help about_testCase*).Count}; testname = "test * pattern"; result = "2"} | ||
@{command = {Get-Help about_testCas?.2*}; testname = "test ?, * pattern with dot"; result = "about_test2"} | ||
) | ||
|
||
It "Get-Help should find pattern help files - <testname>" -TestCases $testcases -Pending: (-not $IsWindows){ | ||
param ( | ||
$command, | ||
$result | ||
) | ||
$command.Invoke() | Should Be $result | ||
} | ||
|
||
} |
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.
RegEx.Escape(pattern) should be used first, before any replaces.
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.
This actually should not be used. RegEx.Escape will also escape the '*' and '?' which we want them as normal character to replace later.