Repository navigation
Fix macro resolution when an escaped argument follows a normal one - #1860
tleonhardt merged 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1860 +/- ##
=======================================
Coverage 99.76% 99.76%
=======================================
Files 23 23
Lines 6777 6777
=======================================
Hits 6761 6761
Misses 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
tleonhardt
left a comment
There was a problem hiding this comment.
Thanks for the fix and the regression test, @mokshupadhyay. I reproduced the bug on the base commit (3a5a2ff). After macro create fake {1} "{{1}}", running fake help tried to execute a command named {1}. On this PR (2a9ca6f), it runs help with the literal argument {1}, as documented.
Using the recorded positions fixes the underlying problem. The resolver already works from right to left, so each remaining position still points to its original placeholder even when a replacement changes length. I also checked macro create probe {1} {2} followed by probe help "{1}". That reproduced the same bug on the base commit and worked on this branch.
The new test exercises the reported case through the command interface. My local make test run finished with 3,230 passed and 3 skipped. Looks good to merge as written.
When a macro is resolved, each argument placeholder is replaced by the argument given to the macro.
Cmd._resolve_macrodid this by searching the partly resolved string for the placeholder text (resolved.rsplit(to_replace, maxsplit=1)), starting from the last one. The macro stores where each placeholder starts (macro_arg.start_index), but that index was not used. When the text that was already substituted, or an escaped placeholder ({{1}}, which becomes a literal{1}), looks like another placeholder, the search finds the wrong occurrence and replaces that one.Real result of the new test:
macro create fake {1} "{{1}}"(a normal argument followed by an escaped one), thenfake help. The macro should runhelp {1}with a literal{1}; onmainand with this change:The fix replaces the text at the recorded
start_index(resolved[:start_index] + replacement + resolved[end_index:]), instead of searching for it.Test:
test_macro_escaped_arg_after_normal_argintests/test_cmd2.py; it fails onmainand passes now. The full suite passes (3228 passed, 5 skipped), andruff checkandruff format --checkare clean on the two changed files.