aboutsummaryrefslogtreecommitdiff
path: root/tests/test_imports.py
diff options
context:
space:
mode:
authorAmy <leiamy12@gmail.com>2020-06-22 09:54:08 -0400
committerDavid Lord <davidism@gmail.com>2020-06-22 09:56:10 -0700
commitcc792d8c918b44c3c6815cced07b0a334a2fed42 (patch)
tree350a8732bfa8c222e39c82640197b8f788df7a91 /tests/test_imports.py
parentcd88dec8ea7c5aa13fdf120481cc989be2c7916f (diff)
downloadjinja-cc792d8c918b44c3c6815cced07b0a334a2fed42.tar.gz
imported templates can see the current globals
_get_default_module takes an optional context to indicate that the template is imported. If there are differences between the environment and rendered template globals, a new module is used for the imported template.
Diffstat (limited to 'tests/test_imports.py')
-rw-r--r--tests/test_imports.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/tests/test_imports.py b/tests/test_imports.py
index bb7400f8..054c9010 100644
--- a/tests/test_imports.py
+++ b/tests/test_imports.py
@@ -102,14 +102,31 @@ class TestImports:
env = Environment(
loader=DictLoader(
{
- "macros": "{% macro testing() %}foo: {{ foo }}{% endmacro %}",
- "test": "{% import 'macros' as m %}{{ m.testing() }}",
+ "macros": "{% macro test() %}foo: {{ foo }}{% endmacro %}",
+ "test": "{% import 'macros' as m %}{{ m.test() }}",
+ "test1": "{% import 'macros' as m %}{{ m.test() }}",
}
)
)
tmpl = env.get_template("test", globals={"foo": "bar"})
assert tmpl.render() == "foo: bar"
+ tmpl = env.get_template("test1")
+ assert tmpl.render() == "foo: "
+
+ def test_import_with_globals_override(self, test_env):
+ env = Environment(
+ loader=DictLoader(
+ {
+ "macros": "{% set foo = '42' %}{% macro test() %}"
+ "foo: {{ foo }}{% endmacro %}",
+ "test": "{% from 'macros' import test %}{{ test() }}",
+ }
+ )
+ )
+ tmpl = env.get_template("test", globals={"foo": "bar"})
+ assert tmpl.render() == "foo: 42"
+
def test_from_import_with_globals(self, test_env):
env = Environment(
loader=DictLoader(