Skip to content

Commit

Permalink
Add test that tests bug seen in production, where the current_path after
Browse files Browse the repository at this point in the history
macro function executed with nested interpretation was not preserved.
With this change, by wrapping the output in a child scope, the
current_path is preserved
  • Loading branch information
jasmith-hs committed Sep 11, 2024
1 parent 6b3c54b commit 62faddf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getString(
JinjavaInterpreter interpreter
) throws IOException {
return Resources.toString(
Resources.getResource(String.format("tags/macrotag/%s", fullName)),
Resources.getResource(fullName),
StandardCharsets.UTF_8
);
}
Expand Down Expand Up @@ -1597,4 +1597,16 @@ public void prepareContext(Context context) {
"keeps-meta-context-variables-through-import/test"
);
}

@Test
public void itWrapsMacroThatWouldChangeCurrentPathInChildScope() {
interpreter
.getContext()
.put(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY, "starting path");
expectedTemplateInterpreter =
ExpectedTemplateInterpreter.withSensibleCurrentPath(jinjava, interpreter, "eager");
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"wraps-macro-that-would-change-current-path-in-child-scope/test"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% macro foo_importer() -%}
{%- include "../dir2/included.jinja" -%}
{%- print foo -%}
{%- endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% set foo = deferred %}
{{ foo }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Starting current_path: starting path
Intermediate current_path: starting path
{% for __ignored__ in [0] %}\
{% set foo = deferred %}
{{ foo }}\
{% print foo %}\
{% endfor %}
Ending current_path: starting path
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Starting current_path: {{ current_path }}
{% from "./dir1/macro.jinja" import foo_importer -%}
Intermediate current_path: {{ current_path }}
{{ foo_importer() }}
Ending current_path: {{ current_path }}

0 comments on commit 62faddf

Please sign in to comment.