Skip to content

Commit 5780c33

Browse files
mgornyzzzeek
authored andcommitted
Fix test runs without lingua or babel
Defer test imports to fall within exclusion decorators Support for identifying new 3.11-style error message Closes: #357 Pull-request: #357 Pull-request-sha: 44f788b Change-Id: I39d59383d92e041e10adc967f51d74839105a1eb
1 parent eae8e3a commit 5780c33

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

mako/testing/fixtures.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ def _do_test(
8080
output = filters(output)
8181
eq_(output, expected)
8282

83+
def indicates_unbound_local_error(self, rendered_output, unbound_var):
84+
var = f"'{unbound_var}'"
85+
error_msgs = (
86+
# < 3.11
87+
f"local variable {var} referenced before assignment",
88+
# >= 3.11
89+
f"cannot access local variable {var} where it is not associated",
90+
)
91+
return any((msg in rendered_output) for msg in error_msgs)
92+
8393

8494
class PlainCacheImpl(CacheImpl):
8595
"""Simple memory cache impl so that tests which

test/ext/test_babelplugin.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import io
22
import os
33

4-
from mako.ext.babelplugin import extract
4+
import pytest
5+
56
from mako.testing.assertions import eq_
67
from mako.testing.config import config
78
from mako.testing.exclusions import requires_babel
89
from mako.testing.fixtures import TemplateTest
910

1011

12+
class UsesExtract:
13+
@pytest.fixture(scope="class")
14+
def extract(self):
15+
from mako.ext.babelplugin import extract
16+
17+
return extract
18+
19+
1120
@requires_babel
12-
class PluginExtractTest:
13-
def test_parse_python_expression(self):
21+
class PluginExtractTest(UsesExtract):
22+
def test_parse_python_expression(self, extract):
1423
input_ = io.BytesIO(b'<p>${_("Message")}</p>')
1524
messages = list(extract(input_, ["_"], [], {}))
1625
eq_(messages, [(1, "_", ("Message"), [])])
1726

18-
def test_python_gettext_call(self):
27+
def test_python_gettext_call(self, extract):
1928
input_ = io.BytesIO(b'<p>${_("Message")}</p>')
2029
messages = list(extract(input_, ["_"], [], {}))
2130
eq_(messages, [(1, "_", ("Message"), [])])
2231

23-
def test_translator_comment(self):
32+
def test_translator_comment(self, extract):
2433
input_ = io.BytesIO(
2534
b"""
2635
<p>
@@ -43,8 +52,8 @@ def test_translator_comment(self):
4352

4453

4554
@requires_babel
46-
class MakoExtractTest(TemplateTest):
47-
def test_extract(self):
55+
class MakoExtractTest(UsesExtract, TemplateTest):
56+
def test_extract(self, extract):
4857
with open(
4958
os.path.join(config.template_base, "gettext.mako")
5059
) as mako_tmpl:
@@ -83,7 +92,7 @@ def test_extract(self):
8392
]
8493
eq_(expected, messages)
8594

86-
def test_extract_utf8(self):
95+
def test_extract_utf8(self, extract):
8796
with open(
8897
os.path.join(config.template_base, "gettext_utf8.mako"), "rb"
8998
) as mako_tmpl:
@@ -92,7 +101,7 @@ def test_extract_utf8(self):
92101
)
93102
assert message == (1, "_", "K\xf6ln", [])
94103

95-
def test_extract_cp1251(self):
104+
def test_extract_cp1251(self, extract):
96105
with open(
97106
os.path.join(config.template_base, "gettext_cp1251.mako"), "rb"
98107
) as mako_tmpl:

test/ext/test_linguaplugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
from mako.ext.linguaplugin import LinguaMakoExtractor
65
from mako.testing.assertions import eq_
76
from mako.testing.config import config
87
from mako.testing.exclusions import requires_lingua
@@ -24,6 +23,8 @@ def register_lingua_extractors(self):
2423
register_extractors()
2524

2625
def test_extract(self):
26+
from mako.ext.linguaplugin import LinguaMakoExtractor
27+
2728
plugin = LinguaMakoExtractor({"comment-tags": "TRANSLATOR"})
2829
messages = list(
2930
plugin(

test/test_exceptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def test_tback_no_trace_from_py_file(self):
274274
html_error = exceptions.html_error_template().render_unicode(
275275
error=v, traceback=None
276276
)
277-
assert "local variable &#39;y&#39;" in html_error
277+
278+
assert self.indicates_unbound_local_error(html_error, "y")
278279

279280
def test_tback_trace_from_py_file(self):
280281
t = self._file_template("runtimeerr.html")
@@ -284,7 +285,7 @@ def test_tback_trace_from_py_file(self):
284285
except:
285286
html_error = exceptions.html_error_template().render_unicode()
286287

287-
assert "local variable &#39;y&#39;" in html_error
288+
assert self.indicates_unbound_local_error(html_error, "y")
288289

289290
def test_code_block_line_number(self):
290291
l = TemplateLookup()

0 commit comments

Comments
 (0)