Skip to content

Commit

Permalink
Throw an error when an import alias references an invalid macro (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen-caps committed Dec 9, 2020
1 parent 74fdbec commit ec43797
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public void importNamedMacrosFromTemplate(String name, List<Pair<String, String>
.getTemplate(this.resolveRelativePath(name));
for (Pair<String, String> pair : namedMacros) {
Macro m = templateImpl.macros.get(pair.getRight());

if (m == null) {
throw new PebbleException(null, "Function or Macro [" + pair.getRight() + "] referenced by alias ["
+ pair.getLeft() + "] does not exist.");
}

this.registerMacro(pair.getLeft(), m);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,22 @@ void testInvalidSameAliasMacroWithImportAsToken() throws IOException {
.startsWith("More than one named template can not share the same name"));
}
}

@Test
void testInvalidAliasReferencingUnknownMacro() throws IOException {
PebbleEngine pebble = new PebbleEngine.Builder().build();

try {
PebbleTemplate template = pebble
.getTemplate("templates/macros/invalid.from.unknownMacro.peb");
Writer writer = new StringWriter();
template.evaluate(writer);
fail("expected PebbleException");
} catch (PebbleException e) {
assertEquals(
"Function or Macro [iDontExist] referenced by alias [macro_test] does not exist.",
e.getPebbleMessage()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hello

{% from "templates/macros/macro.peb" import iDontExist as macro_test %}
Call 1: {{ macro_test() }}

0 comments on commit ec43797

Please sign in to comment.