test_imports.py 7.4 KB

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