- remove context manager to allow 2.5 and 2.4 to work, [ticket:188] - add conditionals to pygments-dependent tests that ensure pygments 1.4, separate check for no pygments.
diff --git a/mako/util.py b/mako/util.py index 3151679..dc070a0 100644 --- a/mako/util.py +++ b/mako/util.py
@@ -9,6 +9,7 @@ py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0) +py26 = sys.version_info >= (2, 6) py24 = sys.version_info >= (2, 4) and sys.version_info < (2, 5) jython = sys.platform.startswith('java') win32 = sys.platform.startswith('win')
diff --git a/test/__init__.py b/test/__init__.py index d2a1f25..7f258dd 100644 --- a/test/__init__.py +++ b/test/__init__.py
@@ -1,6 +1,7 @@ from mako.template import Template import unittest, os -from mako.util import function_named, py3k +from mako.util import py3k +from mako.util import function_named import re from nose import SkipTest @@ -91,3 +92,18 @@ return fn(*args, **kw) return function_named(maybe, fn_name) return decorate + +def requires_pygments_14(fn): + try: + import pygments + version = pygments.__version__ + except: + version = "0" + return skip_if(lambda: version < "1.4")(fn) + +def requires_no_pygments(fn): + try: + import pygments + except: + pygments = None + return skip_if(lambda:pygments is not None)(fn) \ No newline at end of file
diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 8b7254b..9ddbc71 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py
@@ -7,6 +7,7 @@ from mako.lookup import TemplateLookup from util import result_lines from test import template_base, module_base, TemplateTest +from test import requires_pygments_14, requires_no_pygments class ExceptionsTest(TemplateTest): def test_html_error_template(self): @@ -53,8 +54,8 @@ assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in text_error - - def test_utf8_html_error_template(self): + @requires_pygments_14 + def test_utf8_html_error_template_pygments(self): """test the html_error_template with a Template containing utf8 chars""" @@ -87,26 +88,59 @@ html_error if util.py3k: - try: - import pygments - assert u"".encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error - except ImportError: - assert u"${'привет'}".encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert u"".encode(sys.getdefaultencoding(), + 'htmlentityreplace') in html_error else: - try: - import pygments - assert u'<pre>3</pre></div></td><td class="code">'\ - '<div class="syntax-highlighted"><pre><span '\ - 'class="cp">${</span><span class="s">u''\ - 'привет'\ - ''</span><span class="cp">}</span>'.encode( - sys.getdefaultencoding(), - 'htmlentityreplace') in html_error - except ImportError: - assert u"${u'привет'}".encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert u'<pre>3</pre></div></td><td class="code">'\ + '<div class="syntax-highlighted"><pre><span '\ + 'class="cp">${</span><span class="s">u''\ + 'привет'\ + ''</span><span class="cp">}</span>'.encode( + sys.getdefaultencoding(), + 'htmlentityreplace') in html_error + else: + assert False, ("This function should trigger a CompileException, " + "but didn't") + + @requires_no_pygments + def test_utf8_html_error_template_no_pygments(self): + """test the html_error_template with a Template containing utf8 + chars""" + + if util.py3k: + code = """# -*- coding: utf-8 -*- +% if 2 == 2: /an error +${'привет'} +% endif +""" + else: + code = """# -*- coding: utf-8 -*- +% if 2 == 2: /an error +${u'привет'} +% endif +""" + try: + template = Template(code) + template.render_unicode() + except exceptions.CompileException, ce: + html_error = exceptions.html_error_template().render() + if util.py3k: + assert ("CompileException: Fragment 'if 2 == 2: /an " + "error' is not a partial control statement " + "at line: 2 char: 1").encode(sys.getdefaultencoding(), 'htmlentityreplace') in \ + html_error + else: + assert ("CompileException: Fragment 'if 2 == 2: /an " + "error' is not a partial control statement " + "at line: 2 char: 1") in \ + html_error + + if util.py3k: + assert u"${'привет'}".encode(sys.getdefaultencoding(), + 'htmlentityreplace') in html_error + else: + assert u"${u'привет'}".encode(sys.getdefaultencoding(), + 'htmlentityreplace') in html_error else: assert False, ("This function should trigger a CompileException, " "but didn't") @@ -141,7 +175,8 @@ html_error = exceptions.html_error_template().render() assert u"RuntimeError: 日本".encode('ascii', 'ignore') in html_error - def test_format_exceptions(self): + @requires_pygments_14 + def test_format_exceptions_pygments(self): l = TemplateLookup(format_exceptions=True) l.put_string("foo.html", """ @@ -153,19 +188,31 @@ ${self.body()} """) - try: - import pygments - assert '<div class="sourceline"><table class="syntax-highlightedtable">'\ - '<tr><td class="linenos"><div class="linenodiv"><pre>3</pre>'\ - '</div></td><td class="code"><div class="syntax-highlighted">'\ - '<pre><span class="err">$</span><span class="p">{</span>'\ - '<span class="n">foobar</span><span class="p">}</span>' in \ - result_lines(l.get_template("foo.html").render_unicode()) - except ImportError: - assert '<div class="sourceline">${foobar}</div>' in \ - result_lines(l.get_template("foo.html").render_unicode()) + assert '<div class="sourceline"><table class="syntax-highlightedtable">'\ + '<tr><td class="linenos"><div class="linenodiv"><pre>3</pre>'\ + '</div></td><td class="code"><div class="syntax-highlighted">'\ + '<pre><span class="err">$</span><span class="p">{</span>'\ + '<span class="n">foobar</span><span class="p">}</span>' in \ + result_lines(l.get_template("foo.html").render_unicode()) + + @requires_no_pygments + def test_format_exceptions_no_pygments(self): + l = TemplateLookup(format_exceptions=True) + + l.put_string("foo.html", """ +<%inherit file="base.html"/> +${foobar} + """) + + l.put_string("base.html", """ + ${self.body()} + """) + + assert '<div class="sourceline">${foobar}</div>' in \ + result_lines(l.get_template("foo.html").render_unicode()) - def test_utf8_format_exceptions(self): + @requires_pygments_14 + def test_utf8_format_exceptions_pygments(self): """test that htmlentityreplace formatting is applied to exceptions reported with format_exceptions=True""" @@ -176,37 +223,43 @@ l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") if util.py3k: - try: - import pygments - assert '<table class="error syntax-highlightedtable"><tr><td '\ - 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ - '</div></td><td class="code"><div class="error '\ - 'syntax-highlighted"><pre><span class="cp">${</span>'\ - '<span class="s">'привет'</span> <span class="o">+</span> '\ - '<span class="n">foobar</span><span class="cp">}</span>'\ - '<span class="x"></span>' in \ - result_lines(l.get_template("foo.html").render().decode('utf-8')) - except ImportError: - assert u'<div class="sourceline">${'привет' + foobar}</div>'\ - in result_lines(l.get_template("foo.html").render().decode('utf-8')) + assert '<table class="error syntax-highlightedtable"><tr><td '\ + 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ + '</div></td><td class="code"><div class="error '\ + 'syntax-highlighted"><pre><span class="cp">${</span>'\ + '<span class="s">'привет'</span> <span class="o">+</span> '\ + '<span class="n">foobar</span><span class="cp">}</span>'\ + '<span class="x"></span>' in \ + result_lines(l.get_template("foo.html").render().decode('utf-8')) else: - try: - import pygments + assert '<table class="error syntax-highlightedtable"><tr><td '\ + 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ + '</div></td><td class="code"><div class="error '\ + 'syntax-highlighted"><pre><span class="cp">${</span>'\ + '<span class="s">u'прив'\ + 'ет'</span> <span class="o">+</span> '\ + '<span class="n">foobar</span><span class="cp">}</span>'\ + '<span class="x"></span>' in \ + result_lines(l.get_template("foo.html").render().decode('utf-8')) - assert '<table class="error syntax-highlightedtable"><tr><td '\ - 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ - '</div></td><td class="code"><div class="error '\ - 'syntax-highlighted"><pre><span class="cp">${</span>'\ - '<span class="s">u'прив'\ - 'ет'</span> <span class="o">+</span> '\ - '<span class="n">foobar</span><span class="cp">}</span>'\ - '<span class="x"></span>' in \ - result_lines(l.get_template("foo.html").render().decode('utf-8')) + @requires_no_pygments + def test_utf8_format_exceptions_no_pygments(self): + """test that htmlentityreplace formatting is applied to + exceptions reported with format_exceptions=True""" + + l = TemplateLookup(format_exceptions=True) + if util.py3k: + l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""") + else: + l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") - except ImportError: - assert '${u'приве'\ - 'т' + foobar}' in \ - result_lines(l.get_template("foo.html").render().decode('utf-8')) + if util.py3k: + assert u'<div class="sourceline">${'привет' + foobar}</div>'\ + in result_lines(l.get_template("foo.html").render().decode('utf-8')) + else: + assert '${u'приве'\ + 'т' + foobar}' in \ + result_lines(l.get_template("foo.html").render().decode('utf-8')) def test_custom_tback(self):
diff --git a/test/test_template.py b/test/test_template.py index 6a617ef..a1ab11e 100644 --- a/test/test_template.py +++ b/test/test_template.py
@@ -873,6 +873,7 @@ filters=lambda s:s.strip() ) + @skip_if(lambda: not util.py26) def test_blank_control_8(self): self._do_memory_test( """ @@ -970,6 +971,7 @@ filters=lambda s:s.strip() ) + @skip_if(lambda: not util.py26) def test_commented_blank_control_8(self): self._do_memory_test( """ @@ -1093,9 +1095,10 @@ def test_custom_writer(self): canary = [] def write_module(source, outputpath): - with open(outputpath, 'wb') as f: - canary.append(outputpath) - f.write(source) + f = open(outputpath, 'wb') + canary.append(outputpath) + f.write(source) + f.close() lookup = TemplateLookup(template_base, module_writer=write_module, module_directory=module_base) t = lookup.get_template('/modtest.html')