Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when an import alias references an invalid macro (#558) #559

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() }}