From 62faddf4430942f192d8b1e2c6371e0fd108a3ef Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Wed, 11 Sep 2024 09:42:39 -0400 Subject: [PATCH] Add test that tests bug seen in production, where the current_path after 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 --- src/test/java/com/hubspot/jinjava/EagerTest.java | 14 +++++++++++++- .../dir1/macro.jinja | 4 ++++ .../dir2/included.jinja | 2 ++ .../test.expected.jinja | 8 ++++++++ .../test.jinja | 5 +++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir1/macro.jinja create mode 100644 src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir2/included.jinja create mode 100644 src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.expected.jinja create mode 100644 src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja diff --git a/src/test/java/com/hubspot/jinjava/EagerTest.java b/src/test/java/com/hubspot/jinjava/EagerTest.java index 76d5f170f..4c8e19375 100644 --- a/src/test/java/com/hubspot/jinjava/EagerTest.java +++ b/src/test/java/com/hubspot/jinjava/EagerTest.java @@ -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 ); } @@ -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" + ); + } } diff --git a/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir1/macro.jinja b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir1/macro.jinja new file mode 100644 index 000000000..45fb53d08 --- /dev/null +++ b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir1/macro.jinja @@ -0,0 +1,4 @@ +{% macro foo_importer() -%} + {%- include "../dir2/included.jinja" -%} + {%- print foo -%} +{%- endmacro %} \ No newline at end of file diff --git a/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir2/included.jinja b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir2/included.jinja new file mode 100644 index 000000000..bde4ef606 --- /dev/null +++ b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/dir2/included.jinja @@ -0,0 +1,2 @@ +{% set foo = deferred %} +{{ foo }} \ No newline at end of file diff --git a/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.expected.jinja b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.expected.jinja new file mode 100644 index 000000000..025cd0eb5 --- /dev/null +++ b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.expected.jinja @@ -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 \ No newline at end of file diff --git a/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja new file mode 100644 index 000000000..e1f64358a --- /dev/null +++ b/src/test/resources/eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja @@ -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 }}