| 1 |
""" version info, help messages, tracing configuration. """ |
| 2 |
import py |
| 3 |
import pytest |
| 4 |
import os, sys |
| 5 |
|
| 6 |
def pytest_addoption(parser): |
| 7 |
group = parser.getgroup('debugconfig') |
| 8 |
group.addoption('--version', action="store_true", |
| 9 |
help="display pytest lib version and import information.") |
| 10 |
group._addoption("-h", "--help", action="store_true", dest="help", |
| 11 |
help="show help message and configuration info") |
| 12 |
group._addoption('-p', action="append", dest="plugins", default = [], |
| 13 |
metavar="name", |
| 14 |
help="early-load given plugin (multi-allowed). " |
| 15 |
"To avoid loading of plugins, use the `no:` prefix, e.g. " |
| 16 |
"`no:doctest`.") |
| 17 |
group.addoption('--traceconfig', '--trace-config', |
| 18 |
action="store_true", default=False, |
| 19 |
help="trace considerations of conftest.py files."), |
| 20 |
group.addoption('--debug', |
| 21 |
action="store_true", dest="debug", default=False, |
| 22 |
help="store internal tracing debug information in 'pytestdebug.log'.") |
| 23 |
|
| 24 |
|
| 25 |
@pytest.hookimpl(hookwrapper=True) |
| 26 |
def pytest_cmdline_parse(): |
| 27 |
outcome = yield |
| 28 |
config = outcome.get_result() |
| 29 |
if config.option.debug: |
| 30 |
path = os.path.abspath("pytestdebug.log") |
| 31 |
debugfile = open(path, 'w') |
| 32 |
debugfile.write("versions pytest-%s, py-%s, " |
| 33 |
"python-%s\ncwd=%s\nargs=%s\n\n" %( |
| 34 |
pytest.__version__, py.__version__, |
| 35 |
".".join(map(str, sys.version_info)), |
| 36 |
os.getcwd(), config._origargs)) |
| 37 |
config.trace.root.setwriter(debugfile.write) |
| 38 |
undo_tracing = config.pluginmanager.enable_tracing() |
| 39 |
sys.stderr.write("writing pytestdebug information to %s\n" % path) |
| 40 |
def unset_tracing(): |
| 41 |
debugfile.close() |
| 42 |
sys.stderr.write("wrote pytestdebug information to %s\n" % |
| 43 |
debugfile.name) |
| 44 |
config.trace.root.setwriter(None) |
| 45 |
undo_tracing() |
| 46 |
config.add_cleanup(unset_tracing) |
| 47 |
|
| 48 |
def pytest_cmdline_main(config): |
| 49 |
if config.option.version: |
| 50 |
p = py.path.local(pytest.__file__) |
| 51 |
sys.stderr.write("This is pytest version %s, imported from %s\n" % |
| 52 |
(pytest.__version__, p)) |
| 53 |
plugininfo = getpluginversioninfo(config) |
| 54 |
if plugininfo: |
| 55 |
for line in plugininfo: |
| 56 |
sys.stderr.write(line + "\n") |
| 57 |
return 0 |
| 58 |
elif config.option.help: |
| 59 |
config._do_configure() |
| 60 |
showhelp(config) |
| 61 |
config._ensure_unconfigure() |
| 62 |
return 0 |
| 63 |
|
| 64 |
def showhelp(config): |
| 65 |
reporter = config.pluginmanager.get_plugin('terminalreporter') |
| 66 |
tw = reporter._tw |
| 67 |
tw.write(config._parser.optparser.format_help()) |
| 68 |
tw.line() |
| 69 |
tw.line() |
| 70 |
|
| 71 |
tw.line("[pytest] ini-options in the next " |
| 72 |
"pytest.ini|tox.ini|setup.cfg file:") |
| 73 |
tw.line() |
| 74 |
|
| 75 |
for name in config._parser._ininames: |
| 76 |
help, type, default = config._parser._inidict[name] |
| 77 |
if type is None: |
| 78 |
type = "string" |
| 79 |
spec = "%s (%s)" % (name, type) |
| 80 |
line = " %-24s %s" %(spec, help) |
| 81 |
tw.line(line[:tw.fullwidth]) |
| 82 |
|
| 83 |
tw.line() |
| 84 |
tw.line("environment variables:") |
| 85 |
vars = [ |
| 86 |
("PYTEST_ADDOPTS", "extra command line options"), |
| 87 |
("PYTEST_PLUGINS", "comma-separated plugins to load during startup"), |
| 88 |
("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals") |
| 89 |
] |
| 90 |
for name, help in vars: |
| 91 |
tw.line(" %-24s %s" % (name, help)) |
| 92 |
tw.line() |
| 93 |
tw.line() |
| 94 |
|
| 95 |
tw.line("to see available markers type: py.test --markers") |
| 96 |
tw.line("to see available fixtures type: py.test --fixtures") |
| 97 |
tw.line("(shown according to specified file_or_dir or current dir " |
| 98 |
"if not specified)") |
| 99 |
|
| 100 |
for warningreport in reporter.stats.get('warnings', []): |
| 101 |
tw.line("warning : " + warningreport.message, red=True) |
| 102 |
return |
| 103 |
|
| 104 |
|
| 105 |
conftest_options = [ |
| 106 |
('pytest_plugins', 'list of plugin names to load'), |
| 107 |
] |
| 108 |
|
| 109 |
def getpluginversioninfo(config): |
| 110 |
lines = [] |
| 111 |
plugininfo = config.pluginmanager.list_plugin_distinfo() |
| 112 |
if plugininfo: |
| 113 |
lines.append("setuptools registered plugins:") |
| 114 |
for plugin, dist in plugininfo: |
| 115 |
loc = getattr(plugin, '__file__', repr(plugin)) |
| 116 |
content = "%s-%s at %s" % (dist.project_name, dist.version, loc) |
| 117 |
lines.append(" " + content) |
| 118 |
return lines |
| 119 |
|
| 120 |
def pytest_report_header(config): |
| 121 |
lines = [] |
| 122 |
if config.option.debug or config.option.traceconfig: |
| 123 |
lines.append("using: pytest-%s pylib-%s" % |
| 124 |
(pytest.__version__,py.__version__)) |
| 125 |
|
| 126 |
verinfo = getpluginversioninfo(config) |
| 127 |
if verinfo: |
| 128 |
lines.extend(verinfo) |
| 129 |
|
| 130 |
if config.option.traceconfig: |
| 131 |
lines.append("active plugins:") |
| 132 |
items = config.pluginmanager.list_name_plugin() |
| 133 |
for name, plugin in items: |
| 134 |
if hasattr(plugin, '__file__'): |
| 135 |
r = plugin.__file__ |
| 136 |
else: |
| 137 |
r = repr(plugin) |
| 138 |
lines.append(" %-20s: %s" %(name, r)) |
| 139 |
return lines |