test_debug.py
This file
Path
tests/test_debug.py
84 lines · 2.7 KiB · Python
Last changed by
Merge branch '2.9-maintenance'Armin Ronacherbefore adoptionb088619
History of this fileTake it
Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.debug4 ~~~~~~~~~~~~~~~~~~~~~~56 Tests the debug system.7Bump up the copyright to 2017Armin Ronacherbefore adoptionbbe0a41
line 8why, one step back8 :copyright: (c) 2017 by the Jinja Team.Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 9–27why, one step back9 :license: BSD, see LICENSE for more details.10"""11import pytest1213import re14import sys15from traceback import format_exception1617from jinja2 import Environment, TemplateSyntaxError181920@pytest.fixture21def fs_env(filesystem_loader):22 '''returns a new environment.23 '''24 return Environment(loader=filesystem_loader)252627@pytest.mark.debugMerge branch 'feature/no-locals'Armin Ronacherbefore adoption2ce56dd
line 28why, one step back28class TestDebug(object):Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 29–71why, one step back2930 def assert_traceback_matches(self, callback, expected_tb):31 try:32 callback()33 except Exception as e:34 tb = format_exception(*sys.exc_info())35 if re.search(expected_tb.strip(), ''.join(tb)) is None:36 assert False, ('Traceback did not match:\n\n%s\nexpected:\n%s' %37 (''.join(tb), expected_tb))38 else:39 assert False, 'Expected exception'4041 def test_runtime_error(self, fs_env):42 def test():43 tmpl.render(fail=lambda: 1 / 0)44 tmpl = fs_env.get_template('broken.html')45 self.assert_traceback_matches(test, r'''46 File ".*?broken.html", line 2, in (top-level template code|<module>)47 \{\{ fail\(\) \}\}48 File ".*debug?.pyc?", line \d+, in <lambda>49 tmpl\.render\(fail=lambda: 1 / 0\)50ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero51''')5253 def test_syntax_error(self, fs_env):54 # XXX: the .*? is necessary for python3 which does not hide55 # some of the stack frames we don't want to show. Not sure56 # what's up with that, but that is not that critical. Should57 # be fixed though.58 self.assert_traceback_matches(lambda: fs_env.get_template('syntaxerror.html'), r'''(?sm)59 File ".*?syntaxerror.html", line 4, in (template|<module>)60 \{% endif %\}.*?61(jinja2\.exceptions\.)?TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.62 ''')6364 def test_regular_syntax_error(self, fs_env):65 def test():66 raise TemplateSyntaxError('wtf', 42)67 self.assert_traceback_matches(test, r'''68 File ".*debug.pyc?", line \d+, in test69 raise TemplateSyntaxError\('wtf', 42\)70(jinja2\.exceptions\.)?TemplateSyntaxError: wtf71 line 42''')Merge branch 'feature/no-locals'Armin Ronacherbefore adoption2ce56dd
lines 72–84why, one step back7273 def test_local_extraction(self):74 from jinja2.debug import get_jinja_locals75 from jinja2.runtime import missing76 locals = get_jinja_locals({77 'l_0_foo': 42,78 'l_1_foo': 23,79 'l_2_foo': 13,80 'l_0_bar': 99,81 'l_1_bar': missing,82 'l_0_baz': missing,83 })84 assert locals == {'foo': 13, 'bar': 99}