diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 4ca21bd131..cead035f05 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -238,9 +238,9 @@ namespace System.Management.Automation char ch = pattern[i]; // - // if it is a wildcard char, escape it + // if it is a special char, escape it // - if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch)) + if (SpecialChars.Contains(ch) && !charsNotToEscape.Contains(ch)) { temp[tempIndex++] = escapeChar; } diff --git a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 index 7606914332..ddc6498eb6 100644 --- a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 @@ -82,6 +82,21 @@ Describe "ComparisonOperator" -Tag "CI" { param($lhs, $operator, $rhs) Invoke-Expression "$lhs $operator $rhs" | Should -BeFalse } + + It "Should be for backtick comparison " -TestCases @( + @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc`def'; result = $false } + @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc``def'; result = $true } + @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc````def'; result = $false } + @{ lhs = 'abc``def'; operator = '-like'; rhs = 'abc````def'; result = $true } + @{ lhs = 'abc`def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc`def'); result = $true } + @{ lhs = 'abc`def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc``def'); result = $false } + @{ lhs = 'abc``def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc``def'); result = $true } + @{ lhs = 'abc``def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc````def'); result = $false } + ) { + param($lhs, $operator, $rhs, $result) + $expression = "'$lhs' $operator '$rhs'" + Invoke-Expression $expression | Should -Be $result + } } Describe "Bytewise Operator" -Tag "CI" { diff --git a/test/xUnit/csharp/test_WildcardPattern.cs b/test/xUnit/csharp/test_WildcardPattern.cs index 07f4045ef1..7aa4fc8c25 100644 --- a/test/xUnit/csharp/test_WildcardPattern.cs +++ b/test/xUnit/csharp/test_WildcardPattern.cs @@ -25,6 +25,22 @@ namespace PSTests.Parallel [InlineData("a", "a")] [InlineData("a*", "a`*")] [InlineData("*?[]", "`*`?`[`]")] + [InlineData("`", "``")] + [InlineData("```", "``````")] + [InlineData("a`", "a``")] + [InlineData("abc`def", "abc``def")] + [InlineData("abc``def", "abc````def")] + [InlineData("text with `backticks", "text with ``backticks")] + [InlineData("ends with `", "ends with ``")] + [InlineData("`starts with", "``starts with")] + [InlineData("`in`between`", "``in``between``")] + [InlineData("no special characters", "no special characters")] + [InlineData("`*`", "```*``")] + [InlineData("*`?[]", "`*```?`[`]")] + [InlineData("`*`?`[", "```*```?```[")] + [InlineData("*?[]`", "`*`?`[`]``")] + [InlineData("nested `backticks `inside`", "nested ``backticks ``inside``")] + [InlineData("wildcard*with`backtick", "wildcard`*with``backtick")] public void TestEscape_String(string source, string expected) { Assert.Equal(WildcardPattern.Escape(source), expected);