turn the pytest_logwarning hook historic

This commit is contained in:
Ronny Pfannschmidt 2015-09-02 18:49:49 +02:00
parent 3fa261564b
commit 76f0988551
4 changed files with 28 additions and 35 deletions

View File

@ -16,6 +16,8 @@ hookspec = HookspecMarker("pytest")
# pytest startup # pytest startup
# #
class ConftestImportFailure(Exception): class ConftestImportFailure(Exception):
def __init__(self, path, excinfo): def __init__(self, path, excinfo):
Exception.__init__(self, path, excinfo) Exception.__init__(self, path, excinfo)
@ -135,8 +137,6 @@ class PytestPluginManager(PluginManager):
""" """
def __init__(self): def __init__(self):
super(PytestPluginManager, self).__init__("pytest", implprefix="pytest_") super(PytestPluginManager, self).__init__("pytest", implprefix="pytest_")
self._warnings = []
self._pushwarning = self._warnings.append
self._conftest_plugins = set() self._conftest_plugins = set()
# state related to local conftest plugins # state related to local conftest plugins
@ -227,30 +227,14 @@ class PytestPluginManager(PluginManager):
"trylast: mark a hook implementation function such that the " "trylast: mark a hook implementation function such that the "
"plugin machinery will try to call it last/as late as possible.") "plugin machinery will try to call it last/as late as possible.")
def pytest_plugin_registered(self, plugin):
if self.get_name(plugin) == 'terminalreporter':
warnings = self._warnings
self._warnings = None
config = self.get_plugin('pytestconfig')
def pushwarning(args):
assert args.pop('nodeid', None) is None
config.warn(**args)
self._pushwarning = pushwarning
for warning in warnings:
config.hook.pytest_logwarning(**warning)
def _warn(self, message): def _warn(self, message):
if isinstance(message, dict): kwargs = message if isinstance(message, dict) else {
self._pushwarning(message)
else:
self._pushwarning({
'code': 'I1', 'code': 'I1',
'message': message, 'message': message,
'fslocation': None, 'fslocation': None,
'nodeid': None, 'nodeid': None,
}) }
self.hook.pytest_logwarning.call_historic(kwargs=kwargs)
# #
# internal API for local conftest plugin handling # internal API for local conftest plugin handling
@ -726,6 +710,7 @@ class MyOptionParser(argparse.ArgumentParser):
getattr(args, FILE_OR_DIR).extend(argv) getattr(args, FILE_OR_DIR).extend(argv)
return args return args
class DropShorterLongHelpFormatter(argparse.HelpFormatter): class DropShorterLongHelpFormatter(argparse.HelpFormatter):
"""shorten help for long options that differ only in extra hyphens """shorten help for long options that differ only in extra hyphens
@ -845,8 +830,9 @@ class Config(object):
def warn(self, code, message, fslocation=None): def warn(self, code, message, fslocation=None):
""" generate a warning for this test session. """ """ generate a warning for this test session. """
self.hook.pytest_logwarning(code=code, message=message, self.hook.pytest_logwarning.call_historic(kwargs=dict(
fslocation=fslocation, nodeid=None) code=code, message=message,
fslocation=fslocation, nodeid=None))
def get_terminal_writer(self): def get_terminal_writer(self):
return self.pluginmanager.get_plugin("terminalreporter")._tw return self.pluginmanager.get_plugin("terminalreporter")._tw

View File

@ -249,6 +249,8 @@ def pytest_report_teststatus(report):
def pytest_terminal_summary(terminalreporter): def pytest_terminal_summary(terminalreporter):
""" add additional section in terminal summary reporting. """ """ add additional section in terminal summary reporting. """
@hookspec(historic=True)
def pytest_logwarning(message, code, nodeid, fslocation): def pytest_logwarning(message, code, nodeid, fslocation):
""" process a warning specified by a message, a code string, """ process a warning specified by a message, a code string,
a nodeid and fslocation (both of which may be None a nodeid and fslocation (both of which may be None

View File

@ -279,9 +279,9 @@ class Node(object):
else: else:
fslocation = "%s:%s" % fslocation[:2] fslocation = "%s:%s" % fslocation[:2]
self.ihook.pytest_logwarning(code=code, message=message, self.ihook.pytest_logwarning.call_historic(kwargs=dict(
nodeid=self.nodeid, code=code, message=message,
fslocation=fslocation) nodeid=self.nodeid, fslocation=fslocation))
# methods for ordering nodes # methods for ordering nodes
@property @property
@ -742,5 +742,3 @@ class Session(FSCollector):
for x in self.genitems(subnode): for x in self.genitems(subnode):
yield x yield x
node.ihook.pytest_collectreport(report=rep) node.ihook.pytest_collectreport(report=rep)

View File

@ -129,14 +129,21 @@ class TestPytestPluginInteractions:
undo() undo()
def test_warn_on_deprecated_multicall(self, pytestpm): def test_warn_on_deprecated_multicall(self, pytestpm):
warnings = []
class get_warnings:
def pytest_logwarning(self, message):
warnings.append(message)
class Plugin: class Plugin:
def pytest_configure(self, __multicall__): def pytest_configure(self, __multicall__):
pass pass
before = list(pytestpm._warnings) pytestpm.register(get_warnings())
before = list(warnings)
pytestpm.register(Plugin()) pytestpm.register(Plugin())
assert len(pytestpm._warnings) == len(before) + 1 assert len(warnings) == len(before) + 1
assert "deprecated" in pytestpm._warnings[-1]["message"] assert "deprecated" in warnings[-1]
def test_namespace_has_default_and_env_plugins(testdir): def test_namespace_has_default_and_env_plugins(testdir):