Skip to content

Commit

Permalink
bugfix: ensure snippets end on a fresh line
Browse files Browse the repository at this point in the history
this change ensures every snippet ends on a fresh line, ensuring
that the corresponding close snippet tag is written to its own line.
Without this, snippets which would end in the middle of a line would
ultimately cause the next parse to fail as the close tag would be
written on the snippet's last line, causing the snippet parser to
not identify the snippet end on next parse.
  • Loading branch information
jwdevantier committed Jul 16, 2022
1 parent b2df02f commit 351dd2f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions gcgen/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def on_snippet(
scope["$snippets"] = self._snippets_scope.derive()
try:
snippet_fn(emitter, scope, snippet_arg)
emitter.freshline()
except Exception as e:
logger.error(
f"error executing snippet {snippet_name!r} in {fpath!s}", exc_info=True
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from gcgen.api import snippet, Emitter, Scope, Json


@snippet("foo")
def s_foo(e: Emitter, scope: Scope, val: Json):
e.emit("i end in the middle of a line...")


@snippet("bar")
def s_bar(e: Emitter, scope: Scope, val: Json):
e.emitln("I end on a fresh (new) line..")


def gcgen_parse_files():
return ["testfile.txt"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

something
# <<? foo
# ?>>
something else..
# <<? bar
# ?>>
13 changes: 13 additions & 0 deletions tests/test_gentests.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ def test_bb_snippets_err_unclosed_snippet():
generate.compile(gtc.input_path)


def test_bb_snippets_always_end_last_line():
"""encountered bug where a snippet which did not end on a fresh (empty)
line would cause the snippet end tag to be written on that line, causing
the next parse to fail because the corresponding snippet close tag was
not picked up.
This test is to ensure that we now always end on a fresh line, even if the
snippet last called `emit` rather than `emitln`."""
with load_gentest("bb-snippets-always-end-last-line") as gtc:
generate.compile(gtc.input_path)
generate.compile(gtc.input_path)


def test_cc_generators_write_test():
"write two files using a generator"
gentest_test_eql("cc-generators-write-test", ["foo.txt", "bar.txt"])

0 comments on commit 351dd2f

Please sign in to comment.