8000 Fix the issue Get-Help does not support string pattern under Unix by chunqingchen · Pull Request #3852 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 1 commit into from
Jun 7, 2017
Merged
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
15 changes: 15 additions & 0 deletions src/System.Management.Automation/help/MUIFileSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text.RegularExpressions;

namespace System.Management.Automation
{
Expand Down Expand Up @@ -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(".", @"\.");
Copy link
Member

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.

Copy link
Contributor Author

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.

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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We basically need to implementation Directory.GetFiles(path, pattern); in a case-insensitive way, and the code at https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs#L5961 is doing the same thing.

Copy link
Member
@daxian-dbw daxian-dbw Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#4053 is opened for this.
Never mind, it's a duplicate of #3967

{
result.Add(filePath);
}
}
}
return (String[])result.ToArray(typeof(string));
Expand Down
42 changes: 42 additions & 0 deletions test/powershell/engine/Help/HelpSystem.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}

}
0