From 64979909c92e766fce60af131d3b970100942a32 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 6 Sep 2024 19:01:26 -0400 Subject: [PATCH] Actually it's fine to process extends roots in From and ImportTag because they use child interpreters --- .../com/hubspot/jinjava/lib/tag/FromTag.java | 2 +- .../hubspot/jinjava/lib/tag/ImportTag.java | 2 +- .../jinjava/lib/tag/eager/EagerFromTag.java | 2 +- .../jinjava/lib/tag/eager/EagerImportTag.java | 2 +- .../jinjava/ExpectedTemplateInterpreter.java | 28 +++++++++++++++++++ .../com/hubspot/jinjava/FullSnippetsTest.java | 15 ++++++++-- .../from-tag-doesnt-steal-extends/base.jinja | 3 ++ .../something-to-import.jinja | 1 + .../test.expected.jinja | 1 + .../from-tag-doesnt-steal-extends/test.jinja | 7 +++++ 10 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/snippets/from-tag-doesnt-steal-extends/base.jinja create mode 100644 src/test/resources/snippets/from-tag-doesnt-steal-extends/something-to-import.jinja create mode 100644 src/test/resources/snippets/from-tag-doesnt-steal-extends/test.expected.jinja create mode 100644 src/test/resources/snippets/from-tag-doesnt-steal-extends/test.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java index 0a3068e9e..ce00eceef 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java @@ -95,7 +95,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { child.getContext().put(Context.IMPORT_RESOURCE_PATH_KEY, templateFile); JinjavaInterpreter.pushCurrent(child); try { - child.render(node, false); + child.render(node); } finally { JinjavaInterpreter.popCurrent(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java index 5767a54fa..aa9ffbd30 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java @@ -102,7 +102,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { JinjavaInterpreter.pushCurrent(child); try { - child.render(node, false); + child.render(node); } finally { JinjavaInterpreter.popCurrent(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java index 72000d265..937a4ca17 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java @@ -79,7 +79,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter JinjavaInterpreter.pushCurrent(child); String output; try { - output = child.render(node, false); + output = child.render(node); } finally { JinjavaInterpreter.popCurrent(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java index d6828a667..ea46cae3b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java @@ -64,7 +64,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter String output; try { eagerImportingStrategy.setup(child); - output = child.render(node, false); + output = child.render(node); } finally { JinjavaInterpreter.popCurrent(); } diff --git a/src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java b/src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java index eba39bb0e..0c9be20bf 100644 --- a/src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java +++ b/src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java @@ -15,6 +15,7 @@ public class ExpectedTemplateInterpreter { private Jinjava jinjava; private JinjavaInterpreter interpreter; private String path; + private boolean sensibleCurrentPath = false; public ExpectedTemplateInterpreter( Jinjava jinjava, @@ -26,6 +27,26 @@ public ExpectedTemplateInterpreter( this.path = path; } + public static ExpectedTemplateInterpreter withSensibleCurrentPath( + Jinjava jinjava, + JinjavaInterpreter interpreter, + String path + ) { + return new ExpectedTemplateInterpreter(jinjava, interpreter, path, true); + } + + private ExpectedTemplateInterpreter( + Jinjava jinjava, + JinjavaInterpreter interpreter, + String path, + boolean sensibleCurrentPath + ) { + this.jinjava = jinjava; + this.interpreter = interpreter; + this.path = path; + this.sensibleCurrentPath = sensibleCurrentPath; + } + public String assertExpectedOutput(String name) { String template = getFixtureTemplate(name); String output = JinjavaInterpreter.getCurrent().render(template); @@ -116,6 +137,13 @@ public String assertExpectedNonEagerOutput(String name) { public String getFixtureTemplate(String name) { try { + if (sensibleCurrentPath) { + JinjavaInterpreter + .getCurrent() + .getContext() + .getCurrentPathStack() + .push(String.format("%s/%s.jinja", path, name), 0, 0); + } return Resources.toString( Resources.getResource(String.format("%s/%s.jinja", path, name)), StandardCharsets.UTF_8 diff --git a/src/test/java/com/hubspot/jinjava/FullSnippetsTest.java b/src/test/java/com/hubspot/jinjava/FullSnippetsTest.java index 06fca73db..e033118ad 100644 --- a/src/test/java/com/hubspot/jinjava/FullSnippetsTest.java +++ b/src/test/java/com/hubspot/jinjava/FullSnippetsTest.java @@ -39,7 +39,7 @@ public String getString( JinjavaInterpreter interpreter ) throws IOException { return Resources.toString( - Resources.getResource(String.format("tags/macrotag/%s", fullName)), + Resources.getResource(relativePathResolver.resolve(fullName, interpreter)), StandardCharsets.UTF_8 ); } @@ -66,7 +66,11 @@ public Optional getLocationResolver() { ); interpreter = new JinjavaInterpreter(parentInterpreter); expectedTemplateInterpreter = - new ExpectedTemplateInterpreter(jinjava, interpreter, "snippets"); + ExpectedTemplateInterpreter.withSensibleCurrentPath( + jinjava, + interpreter, + "snippets" + ); localContext = interpreter.getContext(); JinjavaInterpreter.pushCurrent(interpreter); @@ -101,4 +105,11 @@ public void itUsesLowerScopeValueInMacroEvaluation() { "uses-lower-scope-value-in-macro-evaluation" ); } + + @Test + public void itFromTagDoesntStealExtends() { + expectedTemplateInterpreter.assertExpectedOutput( + "from-tag-doesnt-steal-extends/test" + ); + } } diff --git a/src/test/resources/snippets/from-tag-doesnt-steal-extends/base.jinja b/src/test/resources/snippets/from-tag-doesnt-steal-extends/base.jinja new file mode 100644 index 000000000..4dec9a793 --- /dev/null +++ b/src/test/resources/snippets/from-tag-doesnt-steal-extends/base.jinja @@ -0,0 +1,3 @@ +{% block content %} +yo +{% endblock %} \ No newline at end of file diff --git a/src/test/resources/snippets/from-tag-doesnt-steal-extends/something-to-import.jinja b/src/test/resources/snippets/from-tag-doesnt-steal-extends/something-to-import.jinja new file mode 100644 index 000000000..fbce8d547 --- /dev/null +++ b/src/test/resources/snippets/from-tag-doesnt-steal-extends/something-to-import.jinja @@ -0,0 +1 @@ +{% set foo = 'hi' %} \ No newline at end of file diff --git a/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.expected.jinja b/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.expected.jinja new file mode 100644 index 000000000..b2feaee55 --- /dev/null +++ b/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.expected.jinja @@ -0,0 +1 @@ +

Test

diff --git a/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.jinja b/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.jinja new file mode 100644 index 000000000..5b8981ae3 --- /dev/null +++ b/src/test/resources/snippets/from-tag-doesnt-steal-extends/test.jinja @@ -0,0 +1,7 @@ +{% extends './base.jinja' %} +{% from './something-to-import.jinja' import foo %} +{{ foo }} +Don't show me! +{% block content %} +

Test

+{% endblock %} \ No newline at end of file