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
Provenance
- Commit
12a316b- Tree
35b0dee- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
95131f2
2 files changed+96 added−24 removed
modified:jinja2/testsuite/loader.py+47 added-6 removed
10 unmodified lines11 import os12 import sys13 import time14 import tempfile15+import shutil16 import unittest17 18 from jinja2.testsuite import JinjaTestCase, dict_loader, \19 package_loader, filesystem_loader, function_loader, \82 unmodified lines102 103 class ModuleLoaderTestCase(JinjaTestCase):104 archive = None105 105- 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)119 120 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 = None128 117- 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 log136+137+ 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()141 142 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()145 146+ def test_deflated_zip_compile(self):147+ self.compile_down(zip='deflated')148+ self._test_common()149+150+ def test_stored_zip_compile(self):151+ self.compile_down(zip='stored')152+ self._test_common()153+154+ def test_filesystem_compile(self):155+ self.compile_down(zip=None)156+ self._test_common()157+158 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 lines174 pass175 176 assert name not in sys.modules177 178+ 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')185 186+187 def suite():188 suite = unittest.TestSuite()189 suite.addTest(unittest.makeSuite(LoaderTestCase))190 suite.addTest(unittest.makeSuite(ModuleLoaderTestCase))1 unmodified linemodified:jinja2/environment.py+49 added-18 removed
6 unmodified lines7 8 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10 """11+import os12 import sys13 from jinja2 import nodes14 from jinja2.defaults import *15 from jinja2.lexer import get_lexer, TokenStream485 unmodified lines501 template = self.from_string(nodes.Template(body, lineno=1))502 return TemplateExpression(template, undefined_to_none)503 504 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'``.512 513 `extensions` and `filter_func` are passed to :meth:`list_templates`.514 Each template returned will be compiled to the target folder or515 zipfile.516 517+ 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.521+522+ If `py_compile` is set to `True` .pyc files will be written to the523+ target instead of standard .py files.524+525 .. versionadded:: 2.4526 """527 from jinja2.loaders import ModuleLoader528+529 if log_function is None:530 log_function = lambda x: None531 519- 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'535+536+ 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()547+548+ 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 lines560 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)568+569+ filename = ModuleLoader.get_module_filename(name)570+571+ 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()582 583 log_function('Finished compiling templates')584 585 def list_templates(self, extensions=None, filter_func=None):496 unmodified lines