Skip to content
Myr

test_debug.py

This file

Path

tests/test_debug.py

73 lines · 2.4 KiB · Python

Last changed by

Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    jinja2.testsuite.debug4    ~~~~~~~~~~~~~~~~~~~~~~56    Tests the debug system.78    :copyright: (c) 2010 by the Jinja Team.9    :license: BSD, see LICENSE for more details.10"""11import pytest1213import re1415import sys16from traceback import format_exception1718from jinja2 import Environment, TemplateSyntaxError19from traceback import format_exception202122@pytest.fixture23def fs_env(filesystem_loader):24    '''returns a new environment.25    '''26    return Environment(loader=filesystem_loader)272829@pytest.mark.debug30class TestDebug():3132    def assert_traceback_matches(self, callback, expected_tb):33        try:34            callback()35        except Exception as e:36            tb = format_exception(*sys.exc_info())37            if re.search(expected_tb.strip(), ''.join(tb)) is None:38                assert False, ('Traceback did not match:\n\n%s\nexpected:\n%s' %39                               (''.join(tb), expected_tb))40        else:41            assert False, 'Expected exception'4243    def test_runtime_error(self, fs_env):44        def test():45            tmpl.render(fail=lambda: 1 / 0)46        tmpl = fs_env.get_template('broken.html')47        self.assert_traceback_matches(test, r'''48  File ".*?broken.html", line 2, in (top-level template code|<module>)49    \{\{ fail\(\) \}\}50  File ".*debug?.pyc?", line \d+, in <lambda>51    tmpl\.render\(fail=lambda: 1 / 0\)52ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero53''')5455    def test_syntax_error(self, fs_env):56        # XXX: the .*? is necessary for python3 which does not hide57        # some of the stack frames we don't want to show.  Not sure58        # what's up with that, but that is not that critical.  Should59        # be fixed though.60        self.assert_traceback_matches(lambda: fs_env.get_template('syntaxerror.html'), r'''(?sm)61  File ".*?syntaxerror.html", line 4, in (template|<module>)62    \{% endif %\}.*?63(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'.64    ''')6566    def test_regular_syntax_error(self, fs_env):67        def test():68            raise TemplateSyntaxError('wtf', 42)69        self.assert_traceback_matches(test, r'''70  File ".*debug.pyc?", line \d+, in test71    raise TemplateSyntaxError\('wtf', 42\)72(jinja2\.exceptions\.)?TemplateSyntaxError: wtf73  line 42''')