Skip to content

Commit

Permalink
Actually it's fine to process extends roots in From and ImportTag
Browse files Browse the repository at this point in the history
because they use child interpreters
  • Loading branch information
jasmith-hs committed Sep 6, 2024
1 parent de36db9 commit 6497990
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ExpectedTemplateInterpreter {
private Jinjava jinjava;
private JinjavaInterpreter interpreter;
private String path;
private boolean sensibleCurrentPath = false;

public ExpectedTemplateInterpreter(
Jinjava jinjava,
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/com/hubspot/jinjava/FullSnippetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -66,7 +66,11 @@ public Optional<LocationResolver> getLocationResolver() {
);
interpreter = new JinjavaInterpreter(parentInterpreter);
expectedTemplateInterpreter =
new ExpectedTemplateInterpreter(jinjava, interpreter, "snippets");
ExpectedTemplateInterpreter.withSensibleCurrentPath(
jinjava,
interpreter,
"snippets"
);
localContext = interpreter.getContext();

JinjavaInterpreter.pushCurrent(interpreter);
Expand Down Expand Up @@ -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"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% block content %}
yo
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% set foo = 'hi' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Test</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends './base.jinja' %}
{% from './something-to-import.jinja' import foo %}
{{ foo }}
Don't show me!
{% block content %}
<p>Test</p>
{% endblock %}

0 comments on commit 6497990

Please sign in to comment.