Skip to content

Commit fa0d3b5

Browse files
committed
fix: stop comment stripping from breaking base64
1 parent 816b548 commit fa0d3b5

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/algokit_utils/deploy.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,29 +325,45 @@ def add_deploy_template_variables(
325325

326326

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

331331
if end < 0:
332332
end = len(line)
333333
idx = start
334-
in_quotes = False
334+
in_quotes = in_base64 = False
335335
while idx < end:
336336
current_char = line[idx]
337337
match current_char:
338-
case "\\":
339-
if in_quotes: # skip next character
340-
idx += 1
338+
# enter base64
339+
case " " | "(" if not in_quotes and _last_token_base64(line, idx):
340+
in_base64 = True
341+
# exit base64
342+
case " " | ")" if not in_quotes and in_base64:
343+
in_base64 = False
344+
# escaped char
345+
case "\\" if in_quotes:
346+
# skip next character
347+
idx += 1
348+
# quote boundary
341349
case '"':
342350
in_quotes = not in_quotes
343-
case _:
351+
# can test for match
352+
case _ if not in_quotes and not in_base64 and line.startswith(token, idx):
344353
# only match if not in quotes and string matches
345-
if not in_quotes and line.startswith(token, idx):
346-
return idx
354+
return idx
347355
idx += 1
348356
return None
349357

350358

359+
def _last_token_base64(line: str, idx: int) -> bool:
360+
try:
361+
*_, last = line[:idx].split()
362+
except ValueError:
363+
return False
364+
return last in ("base64", "b64")
365+
366+
351367
def _find_template_token(line: str, token: str, start: int = 0, end: int = -1) -> int | None:
352368
"""Find the first template token within a line of TEAL. Only matches outside of quotes are returned.
353369
Only full token matches are returned, i.e. TMPL_STR will not match against TMPL_STRING

tests/test_deploy.approvals/test_comment_stripping.approved.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ op 123
1212
op 123
1313
op ""
1414
op "//"
15-
op "//"
15+
op "//"
16+
pushbytes base64(//8=)
17+
pushbytes b64(//8=)
18+
19+
pushbytes base64(//8=)
20+
pushbytes b64(//8=)
21+
pushbytes "base64(//8=)"
22+
pushbytes "b64(//8=)"
23+
24+
pushbytes base64 //8=
25+
pushbytes b64 //8=
26+
27+
pushbytes base64 //8=
28+
pushbytes b64 //8=
29+
pushbytes "base64 //8="
30+
pushbytes "b64 //8="

tests/test_deploy.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ def test_comment_stripping() -> None:
4949
op "" // more comments
5050
op "//" //op "//"
5151
op "//"
52+
pushbytes base64(//8=)
53+
pushbytes b64(//8=)
54+
55+
pushbytes base64(//8=) // pushbytes base64(//8=)
56+
pushbytes b64(//8=) // pushbytes b64(//8=)
57+
pushbytes "base64(//8=)" // pushbytes "base64(//8=)"
58+
pushbytes "b64(//8=)" // pushbytes "b64(//8=)"
59+
60+
pushbytes base64 //8=
61+
pushbytes b64 //8=
62+
63+
pushbytes base64 //8= // pushbytes base64 //8=
64+
pushbytes b64 //8= // pushbytes b64 //8=
65+
pushbytes "base64 //8=" // pushbytes "base64 //8="
66+
pushbytes "b64 //8=" // pushbytes "b64 //8="
67+
5268
"""
5369
result = strip_comments(program)
5470
check_output_stability(result)

0 commit comments

Comments
 (0)