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
|
2011-07-15 01:11:50 +08:00
|
|
|
import os, inspect, sys
|
2011-03-08 01:28:45 +08:00
|
|
|
from _pytest.core import varnames
|
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",
|
2014-01-20 05:05:14 +08:00
|
|
|
help="early-load given plugin (multi-allowed). "
|
|
|
|
"To avoid loading of plugins, use the `no:` prefix, e.g. "
|
|
|
|
"`no:doctest`.")
|
2013-08-01 22:49:26 +08:00
|
|
|
group.addoption('--traceconfig', '--trace-config',
|
|
|
|
action="store_true", default=False,
|
2010-01-13 04:43:25 +08:00
|
|
|
help="trace considerations of conftest.py files."),
|
|
|
|
group.addoption('--debug',
|
|
|
|
action="store_true", dest="debug", default=False,
|
2011-07-15 01:11:50 +08:00
|
|
|
help="store internal tracing debug information in 'pytestdebug.log'.")
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_cmdline_parse(__multicall__):
|
|
|
|
config = __multicall__.execute()
|
|
|
|
if config.option.debug:
|
|
|
|
path = os.path.abspath("pytestdebug.log")
|
|
|
|
f = open(path, 'w')
|
|
|
|
config._debugfile = f
|
|
|
|
f.write("versions pytest-%s, py-%s, python-%s\ncwd=%s\nargs=%s\n\n" %(
|
|
|
|
pytest.__version__, py.__version__, ".".join(map(str, sys.version_info)),
|
|
|
|
os.getcwd(), config._origargs))
|
|
|
|
config.trace.root.setwriter(f.write)
|
|
|
|
sys.stderr.write("writing pytestdebug information to %s\n" % path)
|
|
|
|
return config
|
|
|
|
|
|
|
|
@pytest.mark.trylast
|
|
|
|
def pytest_unconfigure(config):
|
|
|
|
if hasattr(config, '_debugfile'):
|
|
|
|
config._debugfile.close()
|
|
|
|
sys.stderr.write("wrote pytestdebug information to %s\n" %
|
|
|
|
config._debugfile.name)
|
|
|
|
config.trace.root.setwriter(None)
|
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__)
|
2014-01-18 19:31:33 +08:00
|
|
|
sys.stderr.write("This is pytest version %s, imported from %s\n" %
|
2010-11-01 01:57:44 +08:00
|
|
|
(pytest.__version__, p))
|
2011-01-13 02:39:36 +08:00
|
|
|
plugininfo = getpluginversioninfo(config)
|
|
|
|
if plugininfo:
|
|
|
|
for line in plugininfo:
|
|
|
|
sys.stderr.write(line + "\n")
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
2010-10-28 04:29:01 +08:00
|
|
|
elif config.option.help:
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_configure()
|
2010-10-28 04:29:01 +08:00
|
|
|
showhelp(config)
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_unconfigure()
|
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())
|
2013-07-25 21:33:43 +08:00
|
|
|
tw.write(config._parser.optparser.format_epilog(None))
|
2010-10-28 04:29:01 +08:00
|
|
|
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("=")
|
2012-09-22 17:44:56 +08:00
|
|
|
tw.line("to see available markers type: py.test --markers")
|
2012-10-05 20:24:44 +08:00
|
|
|
tw.line("to see available fixtures type: py.test --fixtures")
|
2014-01-22 17:24:22 +08:00
|
|
|
tw.line("(shown according to specified file_or_dir or current dir "
|
|
|
|
"if not specified)")
|
2010-11-01 00:41:58 +08:00
|
|
|
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
|
|
|
|
2011-01-13 02:39:36 +08:00
|
|
|
def getpluginversioninfo(config):
|
|
|
|
lines = []
|
|
|
|
plugininfo = config.pluginmanager._plugin_distinfo
|
|
|
|
if plugininfo:
|
|
|
|
lines.append("setuptools registered plugins:")
|
|
|
|
for dist, plugin in plugininfo:
|
|
|
|
loc = getattr(plugin, '__file__', repr(plugin))
|
|
|
|
content = "%s-%s at %s" % (dist.project_name, dist.version, loc)
|
|
|
|
lines.append(" " + content)
|
|
|
|
return lines
|
|
|
|
|
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__))
|
2011-01-13 02:39:36 +08:00
|
|
|
|
|
|
|
verinfo = getpluginversioninfo(config)
|
|
|
|
if verinfo:
|
|
|
|
lines.extend(verinfo)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
if config.option.traceconfig:
|
|
|
|
lines.append("active 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
|
2011-03-08 01:28:45 +08:00
|
|
|
method_args = list(varnames(method))
|
2009-12-29 17:59:01 +08:00
|
|
|
if '__multicall__' in method_args:
|
|
|
|
method_args.remove('__multicall__')
|
|
|
|
hook = hooks[name]
|
2011-03-08 01:28:45 +08:00
|
|
|
hookargs = varnames(hook)
|
2009-12-29 17:59:01 +08:00
|
|
|
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__")
|
|
|
|
|
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))
|
|
|
|
)
|
|
|
|
|