Debian Codegraph @

tests/test_imports.py

raw
1 import pytest
2
3 from jinja2.environment import Environment
4 from jinja2.exceptions import TemplateNotFound
5 from jinja2.exceptions import TemplatesNotFound
6 from jinja2.exceptions import TemplateSyntaxError
7 from jinja2.exceptions import UndefinedError
8 from jinja2.loaders import DictLoader
9
10
11 @pytest.fixture
12 def test_env():
13 env = Environment(
14 loader=DictLoader(
15 dict(
16 module="{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}",
17 header="[{{ foo }}|{{ 23 }}]",
18 o_printer="({{ o }})",
19 )
20 )
21 )
22 env.globals["bar"] = 23
23 return env
24
25
26 class TestImports:
27 def test_context_imports(self, test_env):
28 t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
29 assert t.render(foo=42) == "[|23]"
30 t = test_env.from_string(
31 '{% import "module" as m without context %}{{ m.test() }}'
32 )
33 assert t.render(foo=42) == "[|23]"
34 t = test_env.from_string(
35 '{% import "module" as m with context %}{{ m.test() }}'
36 )
37 assert t.render(foo=42) == "[42|23]"
38 t = test_env.from_string('{% from "module" import test %}{{ test() }}')
39 assert t.render(foo=42) == "[|23]"
40 t = test_env.from_string(
41 '{% from "module" import test without context %}{{ test() }}'
42 )
43 assert t.render(foo=42) == "[|23]"
44 t = test_env.from_string(
45 '{% from "module" import test with context %}{{ test() }}'
46 )
47 assert t.render(foo=42) == "[42|23]"
48
49 def test_import_needs_name(self, test_env):
50 test_env.from_string('{% from "foo" import bar %}')
51 test_env.from_string('{% from "foo" import bar, baz %}')
52
53 with pytest.raises(TemplateSyntaxError):
54 test_env.from_string('{% from "foo" import %}')
55
56 def test_no_trailing_comma(self, test_env):
57 with pytest.raises(TemplateSyntaxError):
58 test_env.from_string('{% from "foo" import bar, %}')
59
60 with pytest.raises(TemplateSyntaxError):
61 test_env.from_string('{% from "foo" import bar,, %}')
62
63 with pytest.raises(TemplateSyntaxError):
64 test_env.from_string('{% from "foo" import, %}')
65
66 def test_trailing_comma_with_context(self, test_env):
67 test_env.from_string('{% from "foo" import bar, baz with context %}')
68 test_env.from_string('{% from "foo" import bar, baz, with context %}')
69 test_env.from_string('{% from "foo" import bar, with context %}')
70 test_env.from_string('{% from "foo" import bar, with, context %}')
71 test_env.from_string('{% from "foo" import bar, with with context %}')
72
73 with pytest.raises(TemplateSyntaxError):
74 test_env.from_string('{% from "foo" import bar,, with context %}')
75
76 with pytest.raises(TemplateSyntaxError):
77 test_env.from_string('{% from "foo" import bar with context, %}')
78
79 def test_exports(self, test_env):
80 m = test_env.from_string(
81 """
82 {% macro toplevel() %}...{% endmacro %}
83 {% macro __private() %}...{% endmacro %}
84 {% set variable = 42 %}
85 {% for item in [1] %}
86 {% macro notthere() %}{% endmacro %}
87 {% endfor %}
88 """
89 ).module
90 assert m.toplevel() == "..."
91 assert not hasattr(m, "__missing")
92 assert m.variable == 42
93 assert not hasattr(m, "notthere")
94
95 def test_not_exported(self, test_env):
96 t = test_env.from_string("{% from 'module' import nothing %}{{ nothing() }}")
97
98 with pytest.raises(UndefinedError, match="does not export the requested name"):
99 t.render()
100
101 def test_import_with_globals(self, test_env):
102 t = test_env.from_string(
103 '{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
104 )
105 assert t.render() == "[42|23]"
106
107 t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
108 assert t.render() == "[|23]"
109
110 def test_import_with_globals_override(self, test_env):
111 t = test_env.from_string(
112 '{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
113 globals={"foo": 42},
114 )
115 assert t.render() == "[42|23]"
116
117 def test_from_import_with_globals(self, test_env):
118 t = test_env.from_string(
119 '{% from "module" import test %}{{ test() }}',
120 globals={"foo": 42},
121 )
122 assert t.render() == "[42|23]"
123
124
125 class TestIncludes:
126 def test_context_include(self, test_env):
127 t = test_env.from_string('{% include "header" %}')
128 assert t.render(foo=42) == "[42|23]"
129 t = test_env.from_string('{% include "header" with context %}')
130 assert t.render(foo=42) == "[42|23]"
131 t = test_env.from_string('{% include "header" without context %}')
132 assert t.render(foo=42) == "[|23]"
133
134 def test_choice_includes(self, test_env):
135 t = test_env.from_string('{% include ["missing", "header"] %}')
136 assert t.render(foo=42) == "[42|23]"
137
138 t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')
139 assert t.render(foo=42) == ""
140
141 t = test_env.from_string('{% include ["missing", "missing2"] %}')
142 pytest.raises(TemplateNotFound, t.render)
143 with pytest.raises(TemplatesNotFound) as e:
144 t.render()
145
146 assert e.value.templates == ["missing", "missing2"]
147 assert e.value.name == "missing2"
148
149 def test_includes(t, **ctx):
150 ctx["foo"] = 42
151 assert t.render(ctx) == "[42|23]"
152
153 t = test_env.from_string('{% include ["missing", "header"] %}')
154 test_includes(t)
155 t = test_env.from_string("{% include x %}")
156 test_includes(t, x=["missing", "header"])
157 t = test_env.from_string('{% include [x, "header"] %}')
158 test_includes(t, x="missing")
159 t = test_env.from_string("{% include x %}")
160 test_includes(t, x="header")
161 t = test_env.from_string("{% include [x] %}")
162 test_includes(t, x="header")
163
164 def test_include_ignoring_missing(self, test_env):
165 t = test_env.from_string('{% include "missing" %}')
166 pytest.raises(TemplateNotFound, t.render)
167 for extra in "", "with context", "without context":
168 t = test_env.from_string(
169 '{% include "missing" ignore missing ' + extra + " %}"
170 )
171 assert t.render() == ""
172
173 def test_context_include_with_overrides(self, test_env):
174 env = Environment(
175 loader=DictLoader(
176 dict(
177 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
178 item="{{ item }}",
179 )
180 )
181 )
182 assert env.get_template("main").render() == "123"
183
184 def test_unoptimized_scopes(self, test_env):
185 t = test_env.from_string(
186 """
187 {% macro outer(o) %}
188 {% macro inner() %}
189 {% include "o_printer" %}
190 {% endmacro %}
191 {{ inner() }}
192 {% endmacro %}
193 {{ outer("FOO") }}
194 """
195 )
196 assert t.render().strip() == "(FOO)"
197
198 def test_import_from_with_context(self):
199 env = Environment(
200 loader=DictLoader({"a": "{% macro x() %}{{ foobar }}{% endmacro %}"})
201 )
202 t = env.from_string(
203 "{% set foobar = 42 %}{% from 'a' import x with context %}{{ x() }}"
204 )
205 assert t.render() == "42"