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
Original file line number Diff line number Diff line change
Expand Up @@ -4398,6 +4398,12 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
if (CompletionRequiresQuotes(completionText, !useLiteralPath))
{
var quoteInUse = quote == string.Empty ? "'" : quote;

Copy link
Member

@TravisEz13 TravisEz13 Jun 24, 2019

Choose a reason for hiding this comment

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

should all changes be behind the experimental feature?

Copy link
Member

Choose a reason for hiding this comment

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

This was a condition of the committee

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Except filename completion will now also escape * and ? in non-literal path, the behaviour should be identical unless the experimental feature (the breaking part) is enabled.

if (!useLiteralPath)
{
completionText = WildcardPattern.Escape(completionText);
}

if (quoteInUse == "'")
{
completionText = completionText.Replace("'", "''");
Expand All @@ -4410,20 +4416,6 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
completionText = completionText.Replace("$", "`$");
}

if (!useLiteralPath)
{
if (quoteInUse == "'")
{
completionText = completionText.Replace("[", "`[");
completionText = completionText.Replace("]", "`]");
}
else
{
completionText = completionText.Replace("[", "``[");
completionText = completionText.Replace("]", "``]");
}
}

completionText = quoteInUse + completionText + quoteInUse;
}
else if (quote != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: "PSCommandNotFoundSuggestion",
description: "Recommend potential commands based on fuzzy search on a CommandNotFoundException"),
new ExperimentalFeature(
name: "PSWildcardEscapeEscape",
description: "Fix WildcardPattern API: escape the escape character"),
#if UNIX
new ExperimentalFeature(
name: "PSUnixFileStat",
Expand Down
21 changes: 17 additions & 4 deletions src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,28 @@ internal static string Escape(string pattern, char[] charsNotToEscape)

Span<char> temp = pattern.Length < StackAllocThreshold ? stackalloc char[pattern.Length * 2 + 1] : new char[pattern.Length * 2 + 1];
int tempIndex = 0;
bool charNeedsEscaping = false;

for (int i = 0; i < pattern.Length; i++)
{
char ch = pattern[i];

//
// if it is a wildcard char, escape it
//
if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch))
if (ExperimentalFeature.IsEnabled("PSWildcardEscapeEscape"))
{
//
// if it is a wildcard char or escape char, escape it
//
charNeedsEscaping = IsWildcardChar(ch) || ch == escapeChar;
}
else
{
//
// if it is a wildcard char, escape it
//
charNeedsEscaping = IsWildcardChar(ch);
}

if (charNeedsEscaping && !charsNotToEscape.Contains(ch))
{
temp[tempIndex++] = escapeChar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3360,13 +3360,11 @@ private List<string> GenerateNewPSPathsWithGlobLeaf(
StringContainsGlobCharacters(leafElement) ||
isLastLeaf)
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As explained in the PR Summary, regexEscapedLeafElement is passed to WildcardPattern::Get, which does not make sense since in PowerShell, regex escaped string is \[abc\], while wildcard escaped string is `[abc`].
Moreover, why should we escape/alter the pattern string in methods GenerateNew*PathsWithGlobLeaf which is supposed to glob? This behaviour is causing issue on tab completion when filename contains bracket.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I mark this resolved?


// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down Expand Up @@ -3966,13 +3964,11 @@ internal List<string> GenerateNewPathsWithGlobLeaf(
(StringContainsGlobCharacters(leafElement) ||
isLastLeaf))
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);

// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down
3 changes: 3 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,9 @@ dir -Recurse `
@{ inputStr = "Get-Process >'.\My ``[Path``]\'"; expected = "'.${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "Get-Process >${tempDir}\My"; expected = "'${tempDir}${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``[Path``]\'"; expected = "'${tempDir}${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "Set-Location -Path 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``["; expected = "'${tempDir}${separator}My ``[Path``]'" }
)

Push-Location -Path $tempDir
Expand Down
68 changes: 68 additions & 0 deletions test/powershell/engine/Api/WildcardPattern.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Copyright (c) Microsoft Corporation. All rights reserved.
# Copyright (c) Microsoft Corporation.

# Licensed under the MIT License.

Describe "WildcardPattern Escape - Experimental-Feature-Disabled" -Tags "CI" {

BeforeAll {
$testName = 'PSWildcardEscapeEscape'
$skipTest = $EnabledExperimentalFeatures.Contains($testName)

if ($skipTest) {
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature '$testName' to be disabled." -Verbose
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
}
}

AfterAll {
if ($skipTest) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
}

It "Unescaping '<escapedStr>' which escaped from '<inputStr>' should get the original" -TestCases @(
@{inputStr = '*This'; escapedStr = '`*This'}
@{inputStr = 'Is?'; escapedStr = 'Is`?'}
@{inputStr = 'Real[ly]'; escapedStr = 'Real`[ly`]'}
@{inputStr = 'Ba`sic'; escapedStr = 'Ba`sic'}
@{inputStr = 'Test `[more`]?'; escapedStr = 'Test ``[more``]`?'}
) {
param($inputStr, $escapedStr)

[WildcardPattern]::Escape($inputStr) | Should -BeExactly $escapedStr
[WildcardPattern]::Unescape($escapedStr) | Should -BeExactly $inputStr
}
}

Describe "WildcardPattern Escape - Experimental-Feature-Enabled" -Tags "CI" {

BeforeAll {
$testName = 'PSWildcardEscapeEscape'
$skipTest = -not $EnabledExperimentalFeatures.Contains($testName)

if ($skipTest) {
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature '$testName' to be enabled." -Verbose
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
}
}

AfterAll {
if ($skipTest) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
}

It "Unescaping '<escapedStr>' which escaped from '<inputStr>' should get the original" -TestCases @(
@{inputStr = '*This'; escapedStr = '`*This'}
@{inputStr = 'Is?'; escapedStr = 'Is`?'}
@{inputStr = 'Real[ly]'; escapedStr = 'Real`[ly`]'}
@{inputStr = 'Ba`sic'; escapedStr = 'Ba``sic'}
@{inputStr = 'Test `[more`]?'; escapedStr = 'Test ```[more```]`?'}
) {
param($inputStr, $escapedStr)

[WildcardPattern]::Escape($inputStr) | Should -BeExactly $escapedStr
[WildcardPattern]::Unescape($escapedStr) | Should -BeExactly $inputStr
}
}
1 change: 1 addition & 0 deletions test/tools/TestMetadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ExperimentalFeatures": {
"PSWildcardEscapeEscape": [ "test/powershell/engine/Api/WildcardPattern.Tests.ps1" ],
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ],
"PSNullConditionalOperators": [
"test/powershell/Language/Operators/NullConditional.Tests.ps1",
Expand Down