2010-11-06 18:38:53 +08:00
|
|
|
""" version info, help messages, tracing configuration. """
|
2009-08-19 21:45:01 +08:00
|
|
|
import py
|
2010-10-28 04:29:01 +08:00
|
|
|
import pytest
|
2009-12-29 17:59:01 +08:00
|
|
|
import inspect, sys
|
2009-08-19 21:45:01 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup('debugconfig')
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption('--version', action="store_true",
|
2010-10-31 01:23:50 +08:00
|
|
|
help="display pytest lib version and import information.")
|
2010-10-28 04:29:01 +08:00
|
|
|
group._addoption("-h", "--help", action="store_true", dest="help",
|
|
|
|
help="show help message and configuration info")
|
2010-01-03 19:41:29 +08:00
|
|
|
group._addoption('-p', action="append", dest="plugins", default = [],
|
2010-07-27 03:15:15 +08:00
|
|
|
metavar="name",
|
2010-01-03 19:41:29 +08:00
|
|
|
help="early-load given plugin (multi-allowed).")
|
2010-01-13 04:43:25 +08:00
|
|
|
group.addoption('--traceconfig',
|
|
|
|
action="store_true", dest="traceconfig", default=False,
|
|
|
|
help="trace considerations of conftest.py files."),
|
|
|
|
group._addoption('--nomagic',
|
|
|
|
action="store_true", dest="nomagic", default=False,
|
|
|
|
help="don't reinterpret asserts, no traceback cutting. ")
|
|
|
|
group.addoption('--debug',
|
|
|
|
action="store_true", dest="debug", default=False,
|
|
|
|
help="generate and show internal debugging information.")
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
|
2010-09-26 22:23:43 +08:00
|
|
|
def pytest_cmdline_main(config):
|
2009-08-19 21:45:01 +08:00
|
|
|
if config.option.version:
|
2010-11-13 18:10:45 +08:00
|
|
|
p = py.path.local(pytest.__file__)
|
2010-07-27 03:15:15 +08:00
|
|
|
sys.stderr.write("This is py.test version %s, imported from %s\n" %
|
2010-11-01 01:57:44 +08:00
|
|
|
(pytest.__version__, p))
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
2010-10-28 04:29:01 +08:00
|
|
|
elif config.option.help:
|
2010-09-26 22:23:43 +08:00
|
|
|
config.pluginmanager.do_configure(config)
|
2010-10-28 04:29:01 +08:00
|
|
|
showhelp(config)
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
|
|
|
|
2010-10-28 04:29:01 +08:00
|
|
|
def showhelp(config):
|
2009-08-19 21:45:01 +08:00
|
|
|
tw = py.io.TerminalWriter()
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.write(config._parser.optparser.format_help())
|
|
|
|
tw.line()
|
|
|
|
tw.line()
|
2010-11-01 00:41:58 +08:00
|
|
|
#tw.sep( "=", "config file settings")
|
2010-11-25 19:11:10 +08:00
|
|
|
tw.line("[pytest] ini-options in the next "
|
|
|
|
"pytest.ini|tox.ini|setup.cfg file:")
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.line()
|
|
|
|
|
2010-11-07 23:10:22 +08:00
|
|
|
for name in config._parser._ininames:
|
|
|
|
help, type, default = config._parser._inidict[name]
|
2010-11-05 06:21:26 +08:00
|
|
|
if type is None:
|
|
|
|
type = "string"
|
|
|
|
spec = "%s (%s)" % (name, type)
|
|
|
|
line = " %-24s %s" %(spec, help)
|
2009-08-19 21:45:01 +08:00
|
|
|
tw.line(line[:tw.fullwidth])
|
2010-10-28 04:29:01 +08:00
|
|
|
|
|
|
|
tw.line() ; tw.line()
|
2010-11-01 00:41:58 +08:00
|
|
|
#tw.sep("=")
|
|
|
|
return
|
|
|
|
|
|
|
|
tw.line("conftest.py options:")
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.line()
|
2010-11-01 00:41:58 +08:00
|
|
|
conftestitems = sorted(config._parser._conftestdict.items())
|
|
|
|
for name, help in conftest_options + conftestitems:
|
2010-10-28 04:29:01 +08:00
|
|
|
line = " %-15s %s" %(name, help)
|
2009-08-19 21:45:01 +08:00
|
|
|
tw.line(line[:tw.fullwidth])
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.line()
|
2010-11-01 00:41:58 +08:00
|
|
|
#tw.sep( "=")
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
conftest_options = [
|
2009-08-19 21:45:01 +08:00
|
|
|
('pytest_plugins', 'list of plugin names to load'),
|
2010-11-01 00:41:58 +08:00
|
|
|
]
|
2009-12-29 17:59:01 +08:00
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
def pytest_report_header(config):
|
|
|
|
lines = []
|
|
|
|
if config.option.debug or config.option.traceconfig:
|
2010-11-05 06:21:26 +08:00
|
|
|
lines.append("using: pytest-%s pylib-%s" %
|
2010-10-28 04:29:01 +08:00
|
|
|
(pytest.__version__,py.__version__))
|
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
if config.option.traceconfig:
|
|
|
|
lines.append("active plugins:")
|
|
|
|
plugins = []
|
|
|
|
items = config.pluginmanager._name2plugin.items()
|
|
|
|
for name, plugin in items:
|
2010-12-06 23:54:42 +08:00
|
|
|
if hasattr(plugin, '__file__'):
|
|
|
|
r = plugin.__file__
|
|
|
|
else:
|
|
|
|
r = repr(plugin)
|
|
|
|
lines.append(" %-20s: %s" %(name, r))
|
2010-01-13 04:43:25 +08:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2009-12-29 17:59:01 +08:00
|
|
|
# =====================================================
|
2010-07-27 03:15:15 +08:00
|
|
|
# validate plugin syntax and hooks
|
2009-12-29 17:59:01 +08:00
|
|
|
# =====================================================
|
|
|
|
|
|
|
|
def pytest_plugin_registered(manager, plugin):
|
|
|
|
methods = collectattr(plugin)
|
2010-04-22 17:57:57 +08:00
|
|
|
hooks = {}
|
|
|
|
for hookspec in manager.hook._hookspecs:
|
|
|
|
hooks.update(collectattr(hookspec))
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-29 17:59:01 +08:00
|
|
|
stringio = py.io.TextIO()
|
|
|
|
def Print(*args):
|
|
|
|
if args:
|
|
|
|
stringio.write(" ".join(map(str, args)))
|
|
|
|
stringio.write("\n")
|
|
|
|
|
|
|
|
fail = False
|
|
|
|
while methods:
|
|
|
|
name, method = methods.popitem()
|
|
|
|
#print "checking", name
|
|
|
|
if isgenerichook(name):
|
|
|
|
continue
|
2010-07-27 03:15:15 +08:00
|
|
|
if name not in hooks:
|
2010-04-22 17:57:57 +08:00
|
|
|
if not getattr(method, 'optionalhook', False):
|
|
|
|
Print("found unknown hook:", name)
|
|
|
|
fail = True
|
2009-12-29 17:59:01 +08:00
|
|
|
else:
|
2010-04-22 17:57:57 +08:00
|
|
|
#print "checking", method
|
2009-12-29 17:59:01 +08:00
|
|
|
method_args = getargs(method)
|
2010-04-22 17:57:57 +08:00
|
|
|
#print "method_args", method_args
|
2009-12-29 17:59:01 +08:00
|
|
|
if '__multicall__' in method_args:
|
|
|
|
method_args.remove('__multicall__')
|
|
|
|
hook = hooks[name]
|
|
|
|
hookargs = getargs(hook)
|
|
|
|
for arg in method_args:
|
|
|
|
if arg not in hookargs:
|
|
|
|
Print("argument %r not available" %(arg, ))
|
|
|
|
Print("actual definition: %s" %(formatdef(method)))
|
2010-07-27 03:15:15 +08:00
|
|
|
Print("available hook arguments: %s" %
|
2009-12-29 17:59:01 +08:00
|
|
|
", ".join(hookargs))
|
|
|
|
fail = True
|
2010-07-27 03:15:15 +08:00
|
|
|
break
|
2009-12-29 17:59:01 +08:00
|
|
|
#if not fail:
|
|
|
|
# print "matching hook:", formatdef(method)
|
|
|
|
if fail:
|
|
|
|
name = getattr(plugin, '__name__', plugin)
|
2010-11-14 06:33:50 +08:00
|
|
|
raise PluginValidationError("%s:\n%s" % (name, stringio.getvalue()))
|
2009-12-29 17:59:01 +08:00
|
|
|
|
|
|
|
class PluginValidationError(Exception):
|
|
|
|
""" plugin failed validation. """
|
|
|
|
|
|
|
|
def isgenerichook(name):
|
|
|
|
return name == "pytest_plugins" or \
|
|
|
|
name.startswith("pytest_funcarg__")
|
|
|
|
|
|
|
|
def getargs(func):
|
|
|
|
args = inspect.getargs(py.code.getrawcode(func))[0]
|
|
|
|
startindex = inspect.ismethod(func) and 1 or 0
|
|
|
|
return args[startindex:]
|
|
|
|
|
2010-10-28 04:29:01 +08:00
|
|
|
def collectattr(obj):
|
2009-12-29 17:59:01 +08:00
|
|
|
methods = {}
|
|
|
|
for apiname in dir(obj):
|
2010-10-28 04:29:01 +08:00
|
|
|
if apiname.startswith("pytest_"):
|
|
|
|
methods[apiname] = getattr(obj, apiname)
|
2010-07-27 03:15:15 +08:00
|
|
|
return methods
|
2009-12-29 17:59:01 +08:00
|
|
|
|
|
|
|
def formatdef(func):
|
2010-11-14 06:33:50 +08:00
|
|
|
return "%s%s" % (
|
2010-07-27 03:15:15 +08:00
|
|
|
func.__name__,
|
2009-12-29 17:59:01 +08:00
|
|
|
inspect.formatargspec(*inspect.getargspec(func))
|
|
|
|
)
|
|
|
|
|