2010-11-06 06:37:31 +08:00
|
|
|
"""
|
|
|
|
pytest PluginManager, basic initialization and tracing.
|
|
|
|
(c) Holger Krekel 2004-2010
|
|
|
|
"""
|
2010-10-13 17:12:27 +08:00
|
|
|
import sys, os
|
|
|
|
import inspect
|
2010-10-12 18:54:32 +08:00
|
|
|
import py
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest import hookspec # the extension point definitions
|
2010-10-13 03:59:15 +08:00
|
|
|
|
2010-11-24 02:11:21 +08:00
|
|
|
assert py.__version__.split(".")[:2] >= ['1', '4'], ("installation problem: "
|
2010-10-13 03:59:15 +08:00
|
|
|
"%s is too old, remove or upgrade 'py'" % (py.__version__))
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
default_plugins = (
|
2010-12-06 23:54:42 +08:00
|
|
|
"config mark main terminal runner python pdb unittest capture skipping "
|
2010-10-26 05:08:56 +08:00
|
|
|
"tmpdir monkeypatch recwarn pastebin helpconfig nose assertion genscript "
|
2010-12-06 23:54:42 +08:00
|
|
|
"junitxml resultlog doctest").split()
|
2010-10-13 17:12:27 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
class TagTracer:
|
2010-11-06 16:05:17 +08:00
|
|
|
def __init__(self, prefix="[pytest] "):
|
2010-11-06 06:37:31 +08:00
|
|
|
self._tag2proc = {}
|
|
|
|
self.writer = None
|
2010-11-06 16:05:17 +08:00
|
|
|
self.indent = 0
|
|
|
|
self.prefix = prefix
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
return TagTracerSub(self, (name,))
|
|
|
|
|
|
|
|
def processmessage(self, tags, args):
|
|
|
|
if self.writer is not None:
|
2010-11-06 16:05:17 +08:00
|
|
|
if args:
|
|
|
|
indent = " " * self.indent
|
|
|
|
content = " ".join(map(str, args))
|
|
|
|
self.writer("%s%s%s\n" %(self.prefix, indent, content))
|
2010-11-06 06:37:31 +08:00
|
|
|
try:
|
|
|
|
self._tag2proc[tags](tags, args)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setwriter(self, writer):
|
|
|
|
self.writer = writer
|
|
|
|
|
|
|
|
def setprocessor(self, tags, processor):
|
|
|
|
if isinstance(tags, str):
|
|
|
|
tags = tuple(tags.split(":"))
|
|
|
|
else:
|
|
|
|
assert isinstance(tags, tuple)
|
|
|
|
self._tag2proc[tags] = processor
|
|
|
|
|
|
|
|
class TagTracerSub:
|
|
|
|
def __init__(self, root, tags):
|
|
|
|
self.root = root
|
|
|
|
self.tags = tags
|
|
|
|
def __call__(self, *args):
|
|
|
|
self.root.processmessage(self.tags, args)
|
|
|
|
def setmyprocessor(self, processor):
|
|
|
|
self.root.setprocessor(self.tags, processor)
|
|
|
|
def get(self, name):
|
|
|
|
return self.__class__(self.root, self.tags + (name,))
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
class PluginManager(object):
|
2010-10-13 17:12:27 +08:00
|
|
|
def __init__(self, load=False):
|
2010-10-12 18:54:32 +08:00
|
|
|
self._name2plugin = {}
|
2011-03-08 01:28:45 +08:00
|
|
|
self._listattrcache = {}
|
2010-10-13 17:12:27 +08:00
|
|
|
self._plugins = []
|
2010-10-12 18:54:32 +08:00
|
|
|
self._hints = []
|
2010-11-06 16:05:17 +08:00
|
|
|
self.trace = TagTracer().get("pluginmanage")
|
2011-01-13 02:39:36 +08:00
|
|
|
self._plugin_distinfo = []
|
2010-11-06 06:37:31 +08:00
|
|
|
if os.environ.get('PYTEST_DEBUG'):
|
2010-11-06 16:58:04 +08:00
|
|
|
err = sys.stderr
|
2010-11-07 08:10:15 +08:00
|
|
|
encoding = getattr(err, 'encoding', 'utf8')
|
|
|
|
try:
|
|
|
|
err = py.io.dupfile(err, encoding=encoding)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace.root.setwriter(err.write)
|
2010-10-13 17:12:27 +08:00
|
|
|
self.hook = HookRelay([hookspec], pm=self)
|
2010-10-12 18:54:32 +08:00
|
|
|
self.register(self)
|
2010-10-13 17:12:27 +08:00
|
|
|
if load:
|
|
|
|
for spec in default_plugins:
|
|
|
|
self.import_plugin(spec)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def register(self, plugin, name=None, prepend=False):
|
|
|
|
assert not self.isregistered(plugin), plugin
|
2010-12-06 23:54:42 +08:00
|
|
|
name = name or getattr(plugin, '__name__', str(id(plugin)))
|
2010-10-12 18:54:32 +08:00
|
|
|
if name in self._name2plugin:
|
|
|
|
return False
|
2010-12-06 23:54:42 +08:00
|
|
|
#self.trace("registering", name, plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
self._name2plugin[name] = plugin
|
|
|
|
self.call_plugin(plugin, "pytest_addhooks", {'pluginmanager': self})
|
|
|
|
self.hook.pytest_plugin_registered(manager=self, plugin=plugin)
|
2010-10-13 17:12:27 +08:00
|
|
|
if not prepend:
|
|
|
|
self._plugins.append(plugin)
|
|
|
|
else:
|
|
|
|
self._plugins.insert(0, plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
return True
|
|
|
|
|
2010-11-01 16:20:58 +08:00
|
|
|
def unregister(self, plugin=None, name=None):
|
|
|
|
if plugin is None:
|
|
|
|
plugin = self.getplugin(name=name)
|
2010-10-13 17:12:27 +08:00
|
|
|
self._plugins.remove(plugin)
|
2010-11-01 16:20:58 +08:00
|
|
|
self.hook.pytest_plugin_unregistered(plugin=plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
for name, value in list(self._name2plugin.items()):
|
|
|
|
if value == plugin:
|
|
|
|
del self._name2plugin[name]
|
|
|
|
|
|
|
|
def isregistered(self, plugin, name=None):
|
2010-12-06 23:54:42 +08:00
|
|
|
if self.getplugin(name) is not None:
|
2010-10-12 18:54:32 +08:00
|
|
|
return True
|
|
|
|
for val in self._name2plugin.values():
|
|
|
|
if plugin == val:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def addhooks(self, spec):
|
|
|
|
self.hook._addhooks(spec, prefix="pytest_")
|
|
|
|
|
|
|
|
def getplugins(self):
|
2010-10-13 17:12:27 +08:00
|
|
|
return list(self._plugins)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def skipifmissing(self, name):
|
|
|
|
if not self.hasplugin(name):
|
|
|
|
py.test.skip("plugin %r is missing" % name)
|
|
|
|
|
|
|
|
def hasplugin(self, name):
|
2010-12-07 01:32:04 +08:00
|
|
|
return bool(self.getplugin(name))
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def getplugin(self, name):
|
2010-12-06 23:54:42 +08:00
|
|
|
if name is None:
|
|
|
|
return None
|
2010-10-12 18:54:32 +08:00
|
|
|
try:
|
|
|
|
return self._name2plugin[name]
|
|
|
|
except KeyError:
|
2010-12-06 23:54:42 +08:00
|
|
|
return self._name2plugin.get("_pytest." + name, None)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
# API for bootstrapping
|
|
|
|
#
|
|
|
|
def _envlist(self, varname):
|
|
|
|
val = py.std.os.environ.get(varname, None)
|
|
|
|
if val is not None:
|
|
|
|
return val.split(',')
|
|
|
|
return ()
|
|
|
|
|
|
|
|
def consider_env(self):
|
|
|
|
for spec in self._envlist("PYTEST_PLUGINS"):
|
|
|
|
self.import_plugin(spec)
|
|
|
|
|
|
|
|
def consider_setuptools_entrypoints(self):
|
|
|
|
try:
|
2010-11-14 02:46:28 +08:00
|
|
|
from pkg_resources import iter_entry_points, DistributionNotFound
|
2010-10-12 18:54:32 +08:00
|
|
|
except ImportError:
|
|
|
|
return # XXX issue a warning
|
|
|
|
for ep in iter_entry_points('pytest11'):
|
2010-12-06 23:56:12 +08:00
|
|
|
name = ep.name
|
|
|
|
if name.startswith("pytest_"):
|
|
|
|
name = name[7:]
|
|
|
|
if ep.name in self._name2plugin or name in self._name2plugin:
|
2010-10-12 18:54:32 +08:00
|
|
|
continue
|
2010-11-14 02:46:28 +08:00
|
|
|
try:
|
|
|
|
plugin = ep.load()
|
|
|
|
except DistributionNotFound:
|
|
|
|
continue
|
2011-01-13 02:39:36 +08:00
|
|
|
self._plugin_distinfo.append((ep.dist, plugin))
|
2010-10-12 18:54:32 +08:00
|
|
|
self.register(plugin, name=name)
|
|
|
|
|
|
|
|
def consider_preparse(self, args):
|
|
|
|
for opt1,opt2 in zip(args, args[1:]):
|
|
|
|
if opt1 == "-p":
|
2011-03-17 01:00:52 +08:00
|
|
|
self.consider_pluginarg(opt2)
|
|
|
|
|
|
|
|
def consider_pluginarg(self, arg):
|
|
|
|
if arg.startswith("no:"):
|
|
|
|
name = arg[3:]
|
|
|
|
if self.getplugin(name) is not None:
|
|
|
|
self.unregister(None, name=name)
|
|
|
|
self._name2plugin[name] = -1
|
|
|
|
else:
|
|
|
|
if self.getplugin(arg) is None:
|
|
|
|
self.import_plugin(arg)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def consider_conftest(self, conftestmodule):
|
|
|
|
if self.register(conftestmodule, name=conftestmodule.__file__):
|
|
|
|
self.consider_module(conftestmodule)
|
|
|
|
|
|
|
|
def consider_module(self, mod):
|
|
|
|
attr = getattr(mod, "pytest_plugins", ())
|
|
|
|
if attr:
|
|
|
|
if not isinstance(attr, (list, tuple)):
|
|
|
|
attr = (attr,)
|
|
|
|
for spec in attr:
|
|
|
|
self.import_plugin(spec)
|
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def import_plugin(self, modname):
|
|
|
|
assert isinstance(modname, str)
|
|
|
|
if self.getplugin(modname) is not None:
|
2010-10-12 18:54:32 +08:00
|
|
|
return
|
|
|
|
try:
|
2010-12-06 23:54:42 +08:00
|
|
|
#self.trace("importing", modname)
|
2010-10-12 18:54:32 +08:00
|
|
|
mod = importplugin(modname)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2010-12-06 23:54:42 +08:00
|
|
|
except ImportError:
|
|
|
|
if modname.startswith("pytest_"):
|
|
|
|
return self.import_plugin(modname[7:])
|
|
|
|
raise
|
2010-10-13 17:12:27 +08:00
|
|
|
except:
|
2010-10-12 18:54:32 +08:00
|
|
|
e = py.std.sys.exc_info()[1]
|
2010-10-12 21:34:32 +08:00
|
|
|
if not hasattr(py.test, 'skip'):
|
|
|
|
raise
|
|
|
|
elif not isinstance(e, py.test.skip.Exception):
|
|
|
|
raise
|
2010-10-12 18:54:32 +08:00
|
|
|
self._hints.append("skipped plugin %r: %s" %((modname, e.msg)))
|
|
|
|
else:
|
|
|
|
self.register(mod, modname)
|
|
|
|
self.consider_module(mod)
|
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def pytest_plugin_registered(self, plugin):
|
2010-11-13 18:10:45 +08:00
|
|
|
import pytest
|
2010-10-13 17:12:27 +08:00
|
|
|
dic = self.call_plugin(plugin, "pytest_namespace", {}) or {}
|
|
|
|
if dic:
|
2010-11-13 16:05:11 +08:00
|
|
|
self._setns(pytest, dic)
|
2010-10-13 17:12:27 +08:00
|
|
|
if hasattr(self, '_config'):
|
|
|
|
self.call_plugin(plugin, "pytest_addoption",
|
|
|
|
{'parser': self._config._parser})
|
|
|
|
self.call_plugin(plugin, "pytest_configure",
|
|
|
|
{'config': self._config})
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def _setns(self, obj, dic):
|
2010-11-13 18:10:45 +08:00
|
|
|
import pytest
|
2010-10-12 18:54:32 +08:00
|
|
|
for name, value in dic.items():
|
|
|
|
if isinstance(value, dict):
|
|
|
|
mod = getattr(obj, name, None)
|
|
|
|
if mod is None:
|
2010-11-13 16:05:11 +08:00
|
|
|
modname = "pytest.%s" % name
|
|
|
|
mod = py.std.types.ModuleType(modname)
|
|
|
|
sys.modules[modname] = mod
|
2010-10-12 18:54:32 +08:00
|
|
|
mod.__all__ = []
|
|
|
|
setattr(obj, name, mod)
|
2010-11-13 18:10:45 +08:00
|
|
|
obj.__all__.append(name)
|
2010-10-12 18:54:32 +08:00
|
|
|
self._setns(mod, value)
|
|
|
|
else:
|
|
|
|
setattr(obj, name, value)
|
|
|
|
obj.__all__.append(name)
|
2010-11-13 18:10:45 +08:00
|
|
|
#if obj != pytest:
|
|
|
|
# pytest.__all__.append(name)
|
2010-11-13 16:05:11 +08:00
|
|
|
setattr(pytest, name, value)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def pytest_terminal_summary(self, terminalreporter):
|
|
|
|
tw = terminalreporter._tw
|
|
|
|
if terminalreporter.config.option.traceconfig:
|
|
|
|
for hint in self._hints:
|
|
|
|
tw.line("hint: %s" % hint)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2010-11-07 23:10:22 +08:00
|
|
|
def do_addoption(self, parser):
|
|
|
|
mname = "pytest_addoption"
|
|
|
|
methods = reversed(self.listattr(mname))
|
|
|
|
MultiCall(methods, {'parser': parser}).execute()
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def do_configure(self, config):
|
|
|
|
assert not hasattr(self, '_config')
|
|
|
|
self._config = config
|
|
|
|
config.hook.pytest_configure(config=self._config)
|
|
|
|
|
|
|
|
def do_unconfigure(self, config):
|
|
|
|
config = self._config
|
|
|
|
del self._config
|
|
|
|
config.hook.pytest_unconfigure(config=config)
|
|
|
|
config.pluginmanager.unregister(self)
|
|
|
|
|
2011-04-17 18:20:11 +08:00
|
|
|
def notify_exception(self, excinfo, option=None):
|
|
|
|
if option and option.fulltrace:
|
|
|
|
style = "long"
|
|
|
|
else:
|
|
|
|
style = "native"
|
|
|
|
excrepr = excinfo.getrepr(funcargs=True,
|
|
|
|
showlocals=getattr(option, 'showlocals', False),
|
|
|
|
style=style,
|
|
|
|
)
|
2010-10-13 17:12:27 +08:00
|
|
|
res = self.hook.pytest_internalerror(excrepr=excrepr)
|
|
|
|
if not py.builtin.any(res):
|
|
|
|
for line in str(excrepr).split("\n"):
|
|
|
|
sys.stderr.write("INTERNALERROR> %s\n" %line)
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
def listattr(self, attrname, plugins=None):
|
|
|
|
if plugins is None:
|
|
|
|
plugins = self._plugins
|
2011-03-08 01:28:45 +08:00
|
|
|
key = (attrname,) + tuple(plugins)
|
|
|
|
try:
|
|
|
|
return list(self._listattrcache[key])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2010-10-13 17:12:27 +08:00
|
|
|
l = []
|
2010-11-22 06:17:59 +08:00
|
|
|
last = []
|
2010-10-13 17:12:27 +08:00
|
|
|
for plugin in plugins:
|
|
|
|
try:
|
2010-11-22 06:17:59 +08:00
|
|
|
meth = getattr(plugin, attrname)
|
|
|
|
if hasattr(meth, 'tryfirst'):
|
|
|
|
last.append(meth)
|
|
|
|
elif hasattr(meth, 'trylast'):
|
|
|
|
l.insert(0, meth)
|
|
|
|
else:
|
|
|
|
l.append(meth)
|
2010-10-13 17:12:27 +08:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
2010-11-22 06:17:59 +08:00
|
|
|
l.extend(last)
|
2011-03-08 01:28:45 +08:00
|
|
|
self._listattrcache[key] = list(l)
|
2010-10-13 17:12:27 +08:00
|
|
|
return l
|
|
|
|
|
|
|
|
def call_plugin(self, plugin, methname, kwargs):
|
|
|
|
return MultiCall(methods=self.listattr(methname, plugins=[plugin]),
|
|
|
|
kwargs=kwargs, firstresult=True).execute()
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def importplugin(importspec):
|
2010-12-06 23:54:42 +08:00
|
|
|
name = importspec
|
2010-10-12 18:54:32 +08:00
|
|
|
try:
|
2010-12-06 23:54:42 +08:00
|
|
|
mod = "_pytest." + name
|
|
|
|
return __import__(mod, None, None, '__doc__')
|
2010-10-12 18:54:32 +08:00
|
|
|
except ImportError:
|
2010-12-06 23:54:42 +08:00
|
|
|
#e = py.std.sys.exc_info()[1]
|
|
|
|
#if str(e).find(name) == -1:
|
|
|
|
# raise
|
|
|
|
pass #
|
|
|
|
return __import__(importspec, None, None, '__doc__')
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
class MultiCall:
|
2010-10-13 17:12:27 +08:00
|
|
|
""" execute a call into multiple python functions/methods. """
|
2010-10-12 18:54:32 +08:00
|
|
|
def __init__(self, methods, kwargs, firstresult=False):
|
2010-10-13 17:12:27 +08:00
|
|
|
self.methods = list(methods)
|
|
|
|
self.kwargs = kwargs
|
2010-10-12 18:54:32 +08:00
|
|
|
self.results = []
|
|
|
|
self.firstresult = firstresult
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
status = "%d results, %d meths" % (len(self.results), len(self.methods))
|
|
|
|
return "<MultiCall %s, kwargs=%r>" %(status, self.kwargs)
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
while self.methods:
|
|
|
|
method = self.methods.pop()
|
|
|
|
kwargs = self.getkwargs(method)
|
|
|
|
res = method(**kwargs)
|
|
|
|
if res is not None:
|
|
|
|
self.results.append(res)
|
|
|
|
if self.firstresult:
|
|
|
|
return res
|
|
|
|
if not self.firstresult:
|
|
|
|
return self.results
|
|
|
|
|
|
|
|
def getkwargs(self, method):
|
|
|
|
kwargs = {}
|
|
|
|
for argname in varnames(method):
|
|
|
|
try:
|
|
|
|
kwargs[argname] = self.kwargs[argname]
|
|
|
|
except KeyError:
|
2010-10-13 17:12:27 +08:00
|
|
|
if argname == "__multicall__":
|
|
|
|
kwargs[argname] = self
|
2010-10-12 18:54:32 +08:00
|
|
|
return kwargs
|
|
|
|
|
2010-10-26 05:08:56 +08:00
|
|
|
def varnames(func):
|
2011-03-08 01:28:45 +08:00
|
|
|
try:
|
|
|
|
return func._varnames
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2010-10-12 18:54:32 +08:00
|
|
|
if not inspect.isfunction(func) and not inspect.ismethod(func):
|
|
|
|
func = getattr(func, '__call__', func)
|
|
|
|
ismethod = inspect.ismethod(func)
|
|
|
|
rawcode = py.code.getrawcode(func)
|
|
|
|
try:
|
2011-03-08 01:28:45 +08:00
|
|
|
x = rawcode.co_varnames[ismethod:rawcode.co_argcount]
|
2010-10-12 18:54:32 +08:00
|
|
|
except AttributeError:
|
2011-03-08 01:28:45 +08:00
|
|
|
x = ()
|
|
|
|
py.builtin._getfuncdict(func)['_varnames'] = x
|
|
|
|
return x
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
class HookRelay:
|
2010-10-13 17:12:27 +08:00
|
|
|
def __init__(self, hookspecs, pm, prefix="pytest_"):
|
2010-10-12 18:54:32 +08:00
|
|
|
if not isinstance(hookspecs, list):
|
|
|
|
hookspecs = [hookspecs]
|
|
|
|
self._hookspecs = []
|
2010-10-13 17:12:27 +08:00
|
|
|
self._pm = pm
|
2010-11-06 16:05:17 +08:00
|
|
|
self.trace = pm.trace.root.get("hook")
|
2010-10-12 18:54:32 +08:00
|
|
|
for hookspec in hookspecs:
|
|
|
|
self._addhooks(hookspec, prefix)
|
|
|
|
|
|
|
|
def _addhooks(self, hookspecs, prefix):
|
|
|
|
self._hookspecs.append(hookspecs)
|
|
|
|
added = False
|
|
|
|
for name, method in vars(hookspecs).items():
|
|
|
|
if name.startswith(prefix):
|
|
|
|
firstresult = getattr(method, 'firstresult', False)
|
|
|
|
hc = HookCaller(self, name, firstresult=firstresult)
|
|
|
|
setattr(self, name, hc)
|
|
|
|
added = True
|
|
|
|
#print ("setting new hook", name)
|
|
|
|
if not added:
|
|
|
|
raise ValueError("did not find new %r hooks in %r" %(
|
|
|
|
prefix, hookspecs,))
|
|
|
|
|
|
|
|
|
|
|
|
class HookCaller:
|
|
|
|
def __init__(self, hookrelay, name, firstresult):
|
|
|
|
self.hookrelay = hookrelay
|
|
|
|
self.name = name
|
|
|
|
self.firstresult = firstresult
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace = self.hookrelay.trace
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<HookCaller %r>" %(self.name,)
|
2009-03-06 05:10:18 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def __call__(self, **kwargs):
|
2010-10-13 17:12:27 +08:00
|
|
|
methods = self.hookrelay._pm.listattr(self.name)
|
2010-11-07 02:46:24 +08:00
|
|
|
return self._docall(methods, kwargs)
|
2010-10-07 17:51:58 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def pcall(self, plugins, **kwargs):
|
2010-11-07 02:46:24 +08:00
|
|
|
methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
|
|
|
|
return self._docall(methods, kwargs)
|
|
|
|
|
|
|
|
def _docall(self, methods, kwargs):
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace(self.name, kwargs)
|
|
|
|
self.trace.root.indent += 1
|
2010-10-12 18:54:32 +08:00
|
|
|
mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
|
2010-11-07 03:06:32 +08:00
|
|
|
try:
|
|
|
|
res = mc.execute()
|
|
|
|
if res:
|
2010-11-07 03:12:45 +08:00
|
|
|
self.trace("finish", self.name, "-->", res)
|
2010-11-07 03:06:32 +08:00
|
|
|
finally:
|
|
|
|
self.trace.root.indent -= 1
|
2010-11-06 16:58:04 +08:00
|
|
|
return res
|
2010-10-07 17:51:58 +08:00
|
|
|
|
2010-11-13 18:10:45 +08:00
|
|
|
_preinit = []
|
|
|
|
|
|
|
|
def _preloadplugins():
|
|
|
|
_preinit.append(PluginManager(load=True))
|
2010-10-12 21:34:32 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def main(args=None, plugins=None):
|
2010-11-13 16:05:11 +08:00
|
|
|
""" returned exit code integer, after an in-process testing run
|
|
|
|
with the given command line arguments, preloading an optional list
|
|
|
|
of passed in plugin objects. """
|
2010-10-12 21:34:32 +08:00
|
|
|
if args is None:
|
|
|
|
args = sys.argv[1:]
|
2010-11-08 00:37:40 +08:00
|
|
|
elif isinstance(args, py.path.local):
|
|
|
|
args = [str(args)]
|
2010-11-06 06:37:31 +08:00
|
|
|
elif not isinstance(args, (tuple, list)):
|
2010-11-07 06:45:48 +08:00
|
|
|
if not isinstance(args, str):
|
|
|
|
raise ValueError("not a string or argument list: %r" % (args,))
|
|
|
|
args = py.std.shlex.split(args)
|
2010-11-06 06:37:31 +08:00
|
|
|
if _preinit:
|
|
|
|
_pluginmanager = _preinit.pop(0)
|
|
|
|
else: # subsequent calls to main will create a fresh instance
|
|
|
|
_pluginmanager = PluginManager(load=True)
|
|
|
|
hook = _pluginmanager.hook
|
2010-10-12 21:34:32 +08:00
|
|
|
try:
|
2010-11-06 06:37:31 +08:00
|
|
|
if plugins:
|
|
|
|
for plugin in plugins:
|
|
|
|
_pluginmanager.register(plugin)
|
2010-10-28 01:35:27 +08:00
|
|
|
config = hook.pytest_cmdline_parse(
|
2010-11-06 06:37:31 +08:00
|
|
|
pluginmanager=_pluginmanager, args=args)
|
2010-10-12 21:34:32 +08:00
|
|
|
exitstatus = hook.pytest_cmdline_main(config=config)
|
2010-10-28 01:35:27 +08:00
|
|
|
except UsageError:
|
2010-10-12 21:34:32 +08:00
|
|
|
e = sys.exc_info()[1]
|
|
|
|
sys.stderr.write("ERROR: %s\n" %(e.args[0],))
|
|
|
|
exitstatus = 3
|
|
|
|
return exitstatus
|
|
|
|
|
2010-10-28 01:35:27 +08:00
|
|
|
class UsageError(Exception):
|
|
|
|
""" error in py.test usage or invocation"""
|
2010-11-06 06:37:31 +08:00
|
|
|
|