conftest.py
This file
Path
tests/conftest.py
149 lines · 4.2 KiB · Python
Last changed by
Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
History of this fileTake it
The walk stopped after 200 commits: lines older than b217886 point at it.
added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 conftest4 ~~~~~~~~56 Configure py.test for support stuff.7Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
line 8why, one step back8 :copyright: (c) 2009 by the Jinja Team.added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 9–20why, one step back9 :license: BSD, see LICENSE for more details.10"""1112import os13import sys14sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))1516import py17from jinja2 import Environment18from jinja2.loaders import BaseLoader19from jinja2.exceptions import TemplateNotFound20Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)Armin Ronacherbefore adoptionabd3657
line 21why, one step back21added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 22–70why, one step back22try:23 # This code adds support for coverage.py (see24 # http://nedbatchelder.com/code/modules/coverage.html).25 # It prints a coverage report for the modules specified in all26 # module globals (of the test modules) named "coverage_modules".2728 import coverage, atexit2930 IGNORED_MODULES = ['jinja2._speedups', 'jinja2.defaults',31 'jinja2.translators']3233 def report_coverage():34 coverage.stop()35 module_list = [36 mod for name, mod in sys.modules.copy().iteritems() if37 getattr(mod, '__file__', None) and38 name.startswith('jinja2.') and39 name not in IGNORED_MODULES40 ]41 module_list.sort()42 coverage.report(module_list)4344 def callback(option, opt_str, value, parser):45 atexit.register(report_coverage)46 coverage.erase()47 coverage.start()4849 py.test.config.addoptions('Test options', py.test.config.Option('-C',50 '--coverage', action='callback', callback=callback,51 help='Output information about code coverage (slow!)'))5253except ImportError:54 coverage = None555657class GlobalLoader(BaseLoader):58 scope = globals()5960 def get_source(self, environment, name):61 try:62 return self.scope[name.upper() + 'TEMPLATE'], None, None63 except KeyError:64 raise TemplateNotFound(name)656667loader = GlobalLoader()68simple_env = Environment(trim_blocks=True, loader=loader, cache_size=0)6970Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)Armin Ronacherbefore adoptionabd3657
lines 71–84why, one step back71class Directory(py.test.collect.Directory):7273 def run(self):74 rv = super(Directory, self).run()75 if self.fspath.basename == 'tests':76 rv.append('doctests')77 return rv7879 def join(self, name):80 if name == 'doctests':81 return JinjaDocTestModule(name, parent=self)82 return super(Directory, self).join(name)8384added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 85–111why, one step back85class Module(py.test.collect.Module):8687 def __init__(self, *args, **kwargs):88 self.env = simple_env89 super(Module, self).__init__(*args, **kwargs)9091 def makeitem(self, name, obj, usefilters=True):92 if name.startswith('test_'):93 if hasattr(obj, 'func_code'):94 return JinjaTestFunction(name, parent=self)95 elif isinstance(obj, basestring):96 return JinjaDocTest(name, parent=self)979899class JinjaTestFunction(py.test.collect.Function):100101 def execute(self, target, *args):102 loader.scope = target.func_globals103 co = target.func_code104 if 'env' in co.co_varnames[:co.co_argcount]:105 target(self.parent.env, *args)106 else:107 target(*args)108109110class JinjaDocTest(py.test.collect.Item):111Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)Armin Ronacherbefore adoptionabd3657
lines 112–116why, one step back112 def __init__(self, *args, **kwargs):113 realmod = kwargs.pop('realmod', False)114 super(JinjaDocTest, self).__init__(*args, **kwargs)115 self.realmod = realmod116added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
line 117why, one step back117 def run(self):Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)Armin Ronacherbefore adoptionabd3657
lines 118–124why, one step back118 if self.realmod:119 mod = __import__(self.name, None, None, [''])120 else:121 mod = py.std.types.ModuleType(self.name)122 mod.__doc__ = self.obj123 mod.env = self.parent.env124 mod.MODULE = self.parent.objadded author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 125–131why, one step back125 self.execute(mod)126127 def execute(self, mod):128 failed, tot = py.compat.doctest.testmod(mod, verbose=True)129 if failed:130 py.test.fail('doctest %s: %s failed out of %s' % (131 self.fspath, failed, tot))Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)Armin Ronacherbefore adoptionabd3657
lines 132–149why, one step back132133134class JinjaDocTestModule(py.test.collect.Module):135136 def __init__(self, *args, **kwargs):137 super(JinjaDocTestModule, self).__init__(*args, **kwargs)138 self.doctest_modules = [139 'jinja2.environment', 'jinja2.compiler', 'jinja2.parser',140 'jinja2.lexer', 'jinja2.ext', 'jinja2.sandbox',141 'jinja2.filters', 'jinja2.tests', 'jinja2.utils',142 'jinja2.runtime'143 ]144145 def run(self):146 return self.doctest_modules147148 def join(self, name):149 return JinjaDocTest(name, parent=self, realmod=True)