diff options
author | Michał Górny <mgorny@gentoo.org> | 2022-03-17 16:54:31 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-13 08:48:40 -0400 |
commit | 5780c33e51f1117a641a050c3e0d70f27914f554 (patch) | |
tree | d5ff598aeb048cd6f2531367495e1f2fa921156a | |
parent | eae8e3aaa420f4305450e4f1a55881ea9a46bba0 (diff) | |
download | mako-5780c33e51f1117a641a050c3e0d70f27914f554.tar.gz |
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: https://github.com/sqlalchemy/mako/pull/357
Pull-request-sha: 44f788b4f3a3c3e1f8328670a2334555163b90e2
Change-Id: I39d59383d92e041e10adc967f51d74839105a1eb
-rw-r--r-- | mako/testing/fixtures.py | 10 | ||||
-rw-r--r-- | test/ext/test_babelplugin.py | 27 | ||||
-rw-r--r-- | test/ext/test_linguaplugin.py | 3 | ||||
-rw-r--r-- | test/test_exceptions.py | 5 |
4 files changed, 33 insertions, 12 deletions
diff --git a/mako/testing/fixtures.py b/mako/testing/fixtures.py index c9379c0..01e9961 100644 --- a/mako/testing/fixtures.py +++ b/mako/testing/fixtures.py @@ -80,6 +80,16 @@ class TemplateTest: output = filters(output) eq_(output, expected) + def indicates_unbound_local_error(self, rendered_output, unbound_var): + var = f"'{unbound_var}'" + error_msgs = ( + # < 3.11 + f"local variable {var} referenced before assignment", + # >= 3.11 + f"cannot access local variable {var} where it is not associated", + ) + return any((msg in rendered_output) for msg in error_msgs) + class PlainCacheImpl(CacheImpl): """Simple memory cache impl so that tests which diff --git a/test/ext/test_babelplugin.py b/test/ext/test_babelplugin.py index de3e461..cfe79b6 100644 --- a/test/ext/test_babelplugin.py +++ b/test/ext/test_babelplugin.py @@ -1,26 +1,35 @@ import io import os -from mako.ext.babelplugin import extract +import pytest + from mako.testing.assertions import eq_ from mako.testing.config import config from mako.testing.exclusions import requires_babel from mako.testing.fixtures import TemplateTest +class UsesExtract: + @pytest.fixture(scope="class") + def extract(self): + from mako.ext.babelplugin import extract + + return extract + + @requires_babel -class PluginExtractTest: - def test_parse_python_expression(self): +class PluginExtractTest(UsesExtract): + def test_parse_python_expression(self, extract): input_ = io.BytesIO(b'<p>${_("Message")}</p>') messages = list(extract(input_, ["_"], [], {})) eq_(messages, [(1, "_", ("Message"), [])]) - def test_python_gettext_call(self): + def test_python_gettext_call(self, extract): input_ = io.BytesIO(b'<p>${_("Message")}</p>') messages = list(extract(input_, ["_"], [], {})) eq_(messages, [(1, "_", ("Message"), [])]) - def test_translator_comment(self): + def test_translator_comment(self, extract): input_ = io.BytesIO( b""" <p> @@ -43,8 +52,8 @@ class PluginExtractTest: @requires_babel -class MakoExtractTest(TemplateTest): - def test_extract(self): +class MakoExtractTest(UsesExtract, TemplateTest): + def test_extract(self, extract): with open( os.path.join(config.template_base, "gettext.mako") ) as mako_tmpl: @@ -83,7 +92,7 @@ class MakoExtractTest(TemplateTest): ] eq_(expected, messages) - def test_extract_utf8(self): + def test_extract_utf8(self, extract): with open( os.path.join(config.template_base, "gettext_utf8.mako"), "rb" ) as mako_tmpl: @@ -92,7 +101,7 @@ class MakoExtractTest(TemplateTest): ) assert message == (1, "_", "K\xf6ln", []) - def test_extract_cp1251(self): + def test_extract_cp1251(self, extract): with open( os.path.join(config.template_base, "gettext_cp1251.mako"), "rb" ) as mako_tmpl: diff --git a/test/ext/test_linguaplugin.py b/test/ext/test_linguaplugin.py index ae24f67..6e2faa8 100644 --- a/test/ext/test_linguaplugin.py +++ b/test/ext/test_linguaplugin.py @@ -2,7 +2,6 @@ import os import pytest -from mako.ext.linguaplugin import LinguaMakoExtractor from mako.testing.assertions import eq_ from mako.testing.config import config from mako.testing.exclusions import requires_lingua @@ -24,6 +23,8 @@ class MakoExtractTest(TemplateTest): register_extractors() def test_extract(self): + from mako.ext.linguaplugin import LinguaMakoExtractor + plugin = LinguaMakoExtractor({"comment-tags": "TRANSLATOR"}) messages = list( plugin( diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 4a90b90..be573e6 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -274,7 +274,8 @@ ${foobar} html_error = exceptions.html_error_template().render_unicode( error=v, traceback=None ) - assert "local variable 'y'" in html_error + + assert self.indicates_unbound_local_error(html_error, "y") def test_tback_trace_from_py_file(self): t = self._file_template("runtimeerr.html") @@ -284,7 +285,7 @@ ${foobar} except: html_error = exceptions.html_error_template().render_unicode() - assert "local variable 'y'" in html_error + assert self.indicates_unbound_local_error(html_error, "y") def test_code_block_line_number(self): l = TemplateLookup() |