Skip to content
Myr

Improved tests and template compilation.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 95131f2 · Browse files · 2 files+96 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+96 added−24 removed

  • other:+96 added−24 removed

2 files+96 added−24 removed

modified:jinja2/testsuite/loader.py+47 added-6 removed
10 unmodified lines
11import os12import sys13import time14import tempfile15import shutil16import unittest1718from jinja2.testsuite import JinjaTestCase, dict_loader, \19 package_loader, filesystem_loader, function_loader, \
82 unmodified lines
102103class ModuleLoaderTestCase(JinjaTestCase):104 archive = None105105 def setup(self):106 def compile_down(self, zip='deflated', py_compile=False):107 super(ModuleLoaderTestCase, self).setup()108 log = []109 self.reg_env = Environment(loader=prefix_loader)110 if zip is not None:111 self.archive = tempfile.mkstemp(suffix='.zip')[1]112 else:108 self.archive = tempfile.mkstemp(suffix='.zip')[1]109 self.reg_env.compile_templates(self.archive)113 self.archive = tempfile.mkdtemp()114 self.reg_env.compile_templates(self.archive, zip=zip,115 log_function=log.append,116 py_compile=py_compile)117 self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))118 return ''.join(log)119120 def teardown(self):121 super(ModuleLoaderTestCase, self).teardown()122 if hasattr(self, 'mod_env'):123 if os.path.isfile(self.archive):124 os.remove(self.archive)125 else:114 os.remove(self.archive)115 self.archive = None126 shutil.rmtree(self.archive)127 self.archive = None128117 def test_module_loader(self):129 def test_log(self):130 log = self.compile_down()131 assert 'Compiled "a/foo/test.html" as ' \132 'tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a' in log133 assert 'Finished compiling templates' in log134 assert 'Could not compile "a/syntaxerror.html": ' \135 'Encountered unknown tag \'endif\'' in log136137 def _test_common(self):138 tmpl1 = self.reg_env.get_template('a/test.html')139 tmpl2 = self.mod_env.get_template('a/test.html')140 assert tmpl1.render() == tmpl2.render()141142 tmpl1 = self.reg_env.get_template('b/justdict.html')143 tmpl2 = self.mod_env.get_template('b/justdict.html')144 assert tmpl1.render() == tmpl2.render()145146 def test_deflated_zip_compile(self):147 self.compile_down(zip='deflated')148 self._test_common()149150 def test_stored_zip_compile(self):151 self.compile_down(zip='stored')152 self._test_common()153154 def test_filesystem_compile(self):155 self.compile_down(zip=None)156 self._test_common()157158 def test_weak_references(self):159 self.compile_down()160 tmpl = self.mod_env.get_template('a/test.html')161 key = loaders.ModuleLoader.get_template_key('a/test.html')162 name = self.mod_env.loader.module.__name__163
10 unmodified lines
174 pass175176 assert name not in sys.modules177178 def test_byte_compilation(self):179 log = self.compile_down(py_compile=True)180 assert 'Byte-compiled "a/test.html"' in log181 tmpl1 = self.mod_env.get_template('a/test.html')182 mod = self.mod_env.loader.module. \183 tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490184 assert mod.__file__.endswith('.pyc')185186187def suite():188 suite = unittest.TestSuite()189 suite.addTest(unittest.makeSuite(LoaderTestCase))190 suite.addTest(unittest.makeSuite(ModuleLoaderTestCase))
1 unmodified line
modified:jinja2/environment.py+49 added-18 removed
6 unmodified lines
78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import os12import sys13from jinja2 import nodes14from jinja2.defaults import *15from jinja2.lexer import get_lexer, TokenStream
485 unmodified lines
501 template = self.from_string(nodes.Template(body, lineno=1))502 return TemplateExpression(template, undefined_to_none)503504 def compile_templates(self, target, extensions=None, filter_func=None,504 zip=True, log_function=None):505 zip='deflated', log_function=None,506 ignore_errors=True, py_compile=False):507 """Compiles all the templates the loader can find, compiles them506 and stores them in `target`. If `zip` is true, a zipfile will be507 written, otherwise the templates are stored in a directory.508 and stores them in `target`. If `zip` is `None`, instead of in a509 zipfile, the templates will be will be stored in a directory.510 By default a deflate zip algorithm is used, to switch to511 the stored algorithm, `zip` can be set to ``'stored'``.512513 `extensions` and `filter_func` are passed to :meth:`list_templates`.514 Each template returned will be compiled to the target folder or515 zipfile.516517 By default template compilation errors are ignored. In case a518 log function is provided, errors are logged. If you want template519 syntax errors to abort the compilation you can set `ignore_errors`520 to `False` and you will get an exception on syntax errors.521522 If `py_compile` is set to `True` .pyc files will be written to the523 target instead of standard .py files.524525 .. versionadded:: 2.4526 """527 from jinja2.loaders import ModuleLoader528529 if log_function is None:530 log_function = lambda x: None531519 if zip:520 from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED521 f = ZipFile(target, 'w', ZIP_DEFLATED)532 if py_compile:533 import imp, struct, marshal534 py_header = imp.get_magic() + '\xff\xff\xff\xff'535536 def write_file(filename, data):537 if zip:538 info = ZipInfo(filename)539 info.external_attr = 0755 << 16L540 zip_file.writestr(info, data)541 else:542 f = open(os.path.join(target, filename), 'wb')543 try:544 f.write(data)545 finally:546 f.close()547548 if zip is not None:549 from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED, ZIP_STORED550 zip_file = ZipFile(target, 'w', dict(deflated=ZIP_DEFLATED,551 stored=ZIP_STORED)[zip])552 log_function('Compiling into Zip archive "%s"' % target)553 else:554 if not os.path.isdir(target):555 os.makedirs(target)
4 unmodified lines
560 source, filename, _ = self.loader.get_source(self, name)561 try:562 code = self.compile(source, name, filename, True, True)563 except TemplateSyntaxError, e:564 if not ignore_errors:565 raise566 log_function('Could not compile "%s": %s' % (name, e))567 continue536 module = ModuleLoader.get_module_filename(name)537 if zip:538 info = ZipInfo(module)539 info.external_attr = 0755 << 16L540 f.writestr(info, code)568569 filename = ModuleLoader.get_module_filename(name)570571 if py_compile:572 c = compile(code, _encode_filename(filename), 'exec')573 write_file(filename + 'c', py_header + marshal.dumps(c))574 log_function('Byte-compiled "%s" as %s' %575 (name, filename + 'c'))576 else:542 f = open(filename, 'w')543 try:544 f.write(code)545 finally:546 f.close()547 log_function('Compiled "%s" as %s' % (name, module))577 write_file(filename, code)578 log_function('Compiled "%s" as %s' % (name, filename))579 finally:580 if zip:550 f.close()581 zip_file.close()582583 log_function('Finished compiling templates')584585 def list_templates(self, extensions=None, filter_func=None):
496 unmodified lines