Skip to content

Commit

Permalink
fix: stop comment stripping from breaking base64
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx committed Oct 17, 2024
1 parent 816b548 commit fa0d3b5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/algokit_utils/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,45 @@ def add_deploy_template_variables(


def _find_unquoted_string(line: str, token: str, start: int = 0, end: int = -1) -> int | None:
"""Find the first string within a line of TEAL. Only matches outside of quotes are returned.
"""Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.
Returns None if not found"""

if end < 0:
end = len(line)
idx = start
in_quotes = False
in_quotes = in_base64 = False
while idx < end:
current_char = line[idx]
match current_char:
case "\\":
if in_quotes: # skip next character
idx += 1
# enter base64
case " " | "(" if not in_quotes and _last_token_base64(line, idx):
in_base64 = True
# exit base64
case " " | ")" if not in_quotes and in_base64:
in_base64 = False
# escaped char
case "\\" if in_quotes:
# skip next character
idx += 1
# quote boundary
case '"':
in_quotes = not in_quotes
case _:
# can test for match
case _ if not in_quotes and not in_base64 and line.startswith(token, idx):
# only match if not in quotes and string matches
if not in_quotes and line.startswith(token, idx):
return idx
return idx
idx += 1
return None


def _last_token_base64(line: str, idx: int) -> bool:
try:
*_, last = line[:idx].split()
except ValueError:
return False
return last in ("base64", "b64")


def _find_template_token(line: str, token: str, start: int = 0, end: int = -1) -> int | None:
"""Find the first template token within a line of TEAL. Only matches outside of quotes are returned.
Only full token matches are returned, i.e. TMPL_STR will not match against TMPL_STRING
Expand Down
17 changes: 16 additions & 1 deletion tests/test_deploy.approvals/test_comment_stripping.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@ op 123
op 123
op ""
op "//"
op "//"
op "//"
pushbytes base64(//8=)
pushbytes b64(//8=)

pushbytes base64(//8=)
pushbytes b64(//8=)
pushbytes "base64(//8=)"
pushbytes "b64(//8=)"

pushbytes base64 //8=
pushbytes b64 //8=

pushbytes base64 //8=
pushbytes b64 //8=
pushbytes "base64 //8="
pushbytes "b64 //8="
16 changes: 16 additions & 0 deletions tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ def test_comment_stripping() -> None:
op "" // more comments
op "//" //op "//"
op "//"
pushbytes base64(//8=)
pushbytes b64(//8=)
pushbytes base64(//8=) // pushbytes base64(//8=)
pushbytes b64(//8=) // pushbytes b64(//8=)
pushbytes "base64(//8=)" // pushbytes "base64(//8=)"
pushbytes "b64(//8=)" // pushbytes "b64(//8=)"
pushbytes base64 //8=
pushbytes b64 //8=
pushbytes base64 //8= // pushbytes base64 //8=
pushbytes b64 //8= // pushbytes b64 //8=
pushbytes "base64 //8=" // pushbytes "base64 //8="
pushbytes "b64 //8=" // pushbytes "b64 //8="
"""
result = strip_comments(program)
check_output_stability(result)

0 comments on commit fa0d3b5

Please sign in to comment.