Skip to content
Myr

test_imports.py

This file

Path

tests/test_imports.py

205 lines · 7.4 KiB · Python

Last changed by

Merge branch '3.0.x'David Lordbefore adoptionf20a9c9

History of this file

Take it

1import pytest23from jinja2.environment import Environment4from jinja2.exceptions import TemplateNotFound5from jinja2.exceptions import TemplatesNotFound6from jinja2.exceptions import TemplateSyntaxError7from jinja2.exceptions import UndefinedError8from jinja2.loaders import DictLoader91011@pytest.fixture12def 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"] = 2323    return env242526class 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]"4849    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 %}')5253        with pytest.raises(TemplateSyntaxError):54            test_env.from_string('{% from "foo" import %}')5556    def test_no_trailing_comma(self, test_env):57        with pytest.raises(TemplateSyntaxError):58            test_env.from_string('{% from "foo" import bar, %}')5960        with pytest.raises(TemplateSyntaxError):61            test_env.from_string('{% from "foo" import bar,, %}')6263        with pytest.raises(TemplateSyntaxError):64            test_env.from_string('{% from "foo" import, %}')6566    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 %}')7273        with pytest.raises(TemplateSyntaxError):74            test_env.from_string('{% from "foo" import bar,, with context %}')7576        with pytest.raises(TemplateSyntaxError):77            test_env.from_string('{% from "foo" import bar with context, %}')7879    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        ).module90        assert m.toplevel() == "..."91        assert not hasattr(m, "__missing")92        assert m.variable == 4293        assert not hasattr(m, "notthere")9495    def test_not_exported(self, test_env):96        t = test_env.from_string("{% from 'module' import nothing %}{{ nothing() }}")9798        with pytest.raises(UndefinedError, match="does not export the requested name"):99            t.render()100101    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]"106107        t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')108        assert t.render() == "[|23]"109110    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]"116117    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]"123124125class 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]"133134    def test_choice_includes(self, test_env):135        t = test_env.from_string('{% include ["missing", "header"] %}')136        assert t.render(foo=42) == "[42|23]"137138        t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')139        assert t.render(foo=42) == ""140141        t = test_env.from_string('{% include ["missing", "missing2"] %}')142        pytest.raises(TemplateNotFound, t.render)143        with pytest.raises(TemplatesNotFound) as e:144            t.render()145146        assert e.value.templates == ["missing", "missing2"]147        assert e.value.name == "missing2"148149        def test_includes(t, **ctx):150            ctx["foo"] = 42151            assert t.render(ctx) == "[42|23]"152153        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")163164    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() == ""172173    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"183184    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)"197198    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"