Skip to content
Myr

test_debug.py

This file

Path

tests/test_debug.py

117 lines · 3.6 KiB · Python

Last changed by

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

History of this file

Take it

The walk stopped after 200 commits: lines older than c580dd0 point at it.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 1–25why, one step back
1import pickle
2import re3from traceback import format_exception45import pytest67from jinja2 import ChoiceLoader8from jinja2 import DictLoader9from jinja2 import Environment10from jinja2 import TemplateSyntaxError111213@pytest.fixture14def fs_env(filesystem_loader):15 """returns a new environment."""16 return Environment(loader=filesystem_loader)171819class TestDebug:20 def assert_traceback_matches(self, callback, expected_tb):21 with pytest.raises(Exception) as exc_info:22 callback()2324 tb = format_exception(exc_info.type, exc_info.value, exc_info.tb)25 m = re.search(expected_tb.strip(), "".join(tb))

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

lines 26–28why, one step back
26 assert m is not None, (
27 f"Traceback did not match:\n\n{''.join(tb)}\nexpected:\n{expected_tb}"28 )

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 29–38why, one step back
29
30 def test_runtime_error(self, fs_env):31 def test():32 tmpl.render(fail=lambda: 1 / 0)3334 tmpl = fs_env.get_template("broken.html")35 self.assert_traceback_matches(36 test,37 r"""38 File ".*?broken.html", line 2, in (top-level template code|<module>)

Merge branch '3.0.x'David Lordbefore adoption5e2385c

lines 39–40why, one step back
39 \{\{ fail\(\) \}\}(
40 \^{12})?

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 41why, one step back
41 File ".*debug?.pyc?", line \d+, in <lambda>

Merge branch '3.0.x'David Lordbefore adoption5e2385c

lines 42–43why, one step back
42 tmpl\.render\(fail=lambda: 1 / 0\)(
43 ~~\^~~)?

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 44–70why, one step back
44ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero
45""",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 test

Merge branch '3.0.x'David Lordbefore adoption5e2385c

lines 71–72why, one step back
71 raise TemplateSyntaxError\("wtf", 42\)(
72 \^{36})?

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 73–117why, one step back
73(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
74 line 42""",75 )7677 def test_pickleable_syntax_error(self, fs_env):78 original = TemplateSyntaxError("bad template", 42, "test", "test.txt")79 unpickled = pickle.loads(pickle.dumps(original))80 assert str(original) == str(unpickled)81 assert original.name == unpickled.name8283 def test_include_syntax_error_source(self, filesystem_loader):84 e = Environment(85 loader=ChoiceLoader(86 [87 filesystem_loader,88 DictLoader({"inc": "a\n{% include 'syntaxerror.html' %}\nb"}),89 ]90 )91 )92 t = e.get_template("inc")9394 with pytest.raises(TemplateSyntaxError) as exc_info:95 t.render()9697 assert exc_info.value.source is not None9899 def test_local_extraction(self):100 from jinja2.debug import get_template_locals101 from jinja2.runtime import missing102103 locals = get_template_locals(104 {105 "l_0_foo": 42,106 "l_1_foo": 23,107 "l_2_foo": 13,108 "l_0_bar": 99,109 "l_1_bar": missing,110 "l_0_baz": missing,111 }112 )113 assert locals == {"foo": 13, "bar": 99}114115 def test_get_corresponding_lineno_traceback(self, fs_env):116 tmpl = fs_env.get_template("test.html")117 assert tmpl.get_corresponding_lineno(1) == 1