test_debug.py
This file
Path
tests/test_debug.py
116 lines · 3.6 KiB · Python
Last changed by
remove custom pytest marksDavid Lordbefore adoptiondd5a050
History of this fileTake it
1# -*- coding: utf-8 -*-2import pickle3import re4from traceback import format_exception56import pytest78from jinja2 import ChoiceLoader9from jinja2 import DictLoader10from jinja2 import Environment11from jinja2 import TemplateSyntaxError121314@pytest.fixture15def fs_env(filesystem_loader):16 """returns a new environment."""17 return Environment(loader=filesystem_loader)181920class TestDebug(object):21 def assert_traceback_matches(self, callback, expected_tb):22 with pytest.raises(Exception) as exc_info:23 callback()2425 tb = format_exception(exc_info.type, exc_info.value, exc_info.tb)26 m = re.search(expected_tb.strip(), "".join(tb))27 assert m is not None, "Traceback did not match:\n\n%s\nexpected:\n%s" % (28 "".join(tb),29 expected_tb,30 )3132 def test_runtime_error(self, fs_env):33 def test():34 tmpl.render(fail=lambda: 1 / 0)3536 tmpl = fs_env.get_template("broken.html")37 self.assert_traceback_matches(38 test,39 r"""40 File ".*?broken.html", line 2, in (top-level template code|<module>)41 \{\{ fail\(\) \}\}42 File ".*debug?.pyc?", line \d+, in <lambda>43 tmpl\.render\(fail=lambda: 1 / 0\)44ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero45""",46 )4748 def test_syntax_error(self, fs_env):49 # The trailing .*? is for PyPy 2 and 3, which don't seem to50 # clear the exception's original traceback, leaving the syntax51 # error in the middle of other compiler frames.52 self.assert_traceback_matches(53 lambda: fs_env.get_template("syntaxerror.html"),54 """(?sm)55 File ".*?syntaxerror.html", line 4, in (template|<module>)56 \\{% endif %\\}.*?57(jinja2\\.exceptions\\.)?TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja \58was looking for the following tags: 'endfor' or 'else'. The innermost block that needs \59to be closed is 'for'.60 """,61 )6263 def test_regular_syntax_error(self, fs_env):64 def test():65 raise TemplateSyntaxError("wtf", 42)6667 self.assert_traceback_matches(68 test,69 r"""70 File ".*debug.pyc?", line \d+, in test71 raise TemplateSyntaxError\("wtf", 42\)72(jinja2\.exceptions\.)?TemplateSyntaxError: wtf73 line 42""",74 )7576 def test_pickleable_syntax_error(self, fs_env):77 original = TemplateSyntaxError("bad template", 42, "test", "test.txt")78 unpickled = pickle.loads(pickle.dumps(original))79 assert str(original) == str(unpickled)80 assert original.name == unpickled.name8182 def test_include_syntax_error_source(self, filesystem_loader):83 e = Environment(84 loader=ChoiceLoader(85 [86 filesystem_loader,87 DictLoader({"inc": "a\n{% include 'syntaxerror.html' %}\nb"}),88 ]89 )90 )91 t = e.get_template("inc")9293 with pytest.raises(TemplateSyntaxError) as exc_info:94 t.render()9596 assert exc_info.value.source is not None9798 def test_local_extraction(self):99 from jinja2.debug import get_template_locals100 from jinja2.runtime import missing101102 locals = get_template_locals(103 {104 "l_0_foo": 42,105 "l_1_foo": 23,106 "l_2_foo": 13,107 "l_0_bar": 99,108 "l_1_bar": missing,109 "l_0_baz": missing,110 }111 )112 assert locals == {"foo": 13, "bar": 99}113114 def test_get_corresponding_lineno_traceback(self, fs_env):115 tmpl = fs_env.get_template("test.html")116 assert tmpl.get_corresponding_lineno(1) == 1