Skip to content
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

Fix WildcardPattern.Escape to escape lone backticks correctly #25211

Merged
merged 8 commits into from
Mar 25, 2025

Conversation

ArmaanMcleod
Copy link
Contributor

@ArmaanMcleod ArmaanMcleod commented Mar 22, 2025

PR Summary

Fix WildcardPattern.Escape to escape lone backticks correctly.

I have added the -like specific tests in Pester and the Escape() method conversion tests in XUnit.

Given backtick is included in SpecialChars which also has wildcard chars, it seems more straightforward to just do SpecialChars.Contains(ch) instead of IsWildcardChar(ch) || ch == escapeChar in the Escape() method.

PR Context

Fixes #16306

cc @IISResetMe

PR Checklist

@iSazonov iSazonov added CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Mar 22, 2025
@ArmaanMcleod ArmaanMcleod changed the title Fix WildcardPattern.Escape for backticks Fix WildcardPattern.Escape to escape lone backticks correctly Mar 22, 2025
@{ 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 }
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is "Win7 backcompatibility requires treating '`' pattern as '' pattern" comment in regex.cs file. Please add a test for the scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov Do we need to worry about this? I am not sure why we are still trying to support Win 7 compatibility and why such code even exists 😄. Can you elaborate on what scenario this covers?

Copy link
Contributor Author

@ArmaanMcleod ArmaanMcleod Mar 22, 2025

Choose a reason for hiding this comment

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

Given also the code changes here are in Escape() I am not sure what I am testing. The Win7 compatibility is more with ensuring WildcardPattern in Parse() method treats lone backtick as empty string.

@iSazonov
Copy link
Collaborator

@IISResetMe Could you please review?

@iSazonov iSazonov self-assigned this Mar 22, 2025
@IISResetMe
Copy link
Collaborator

Changes look good to me!

@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@iSazonov iSazonov merged commit e308222 into PowerShell:master Mar 25, 2025
39 of 41 checks passed
Copy link
Contributor

microsoft-github-policy-service bot commented Mar 25, 2025

📣 Hey @ArmaanMcleod, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@ArmaanMcleod
Copy link
Contributor Author

Amazing. Thanks @iSazonov & @IISResetMe for the reviews and discussion on this one. It took a couple tries but I think we got there in the end 😄. Learnt a lot more how PowerShell does escaping internally too.

@iSazonov
Copy link
Collaborator

We are waiting feedback from community now. If there are no critical messages, then this will be included in the new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WildcardPattern.Escape() does not escape lone backticks correctly
3 participants