helpconfig.py 5.0 KB

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