Fix WildcardPattern.Escape to escape lone backticks correctly (#25211)

This commit is contained in:
Armaan Mcleod
2025-03-25 09:05:42 +05:00
committed by GitHub
parent 93e053c57e
commit e308222e19
3 changed files with 33 additions and 2 deletions
@@ -82,6 +82,21 @@ Describe "ComparisonOperator" -Tag "CI" {
param($lhs, $operator, $rhs)
Invoke-Expression "$lhs $operator $rhs" | Should -BeFalse
}
It "Should be <result> for backtick comparison <lhs> <operator> <rhs>" -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" {
+16
View File
@@ -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);