From b25757199c89051c7f0aedc1fc2bef70677fd88c Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 8 Jul 2020 17:51:45 -0300 Subject: [PATCH] Support new ``pytest_warning_recorded`` hook from pytest 6.0 --- src/xdist/dsession.py | 9 ++++++++- src/xdist/remote.py | 12 ++++++++++++ src/xdist/workermanage.py | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/xdist/dsession.py b/src/xdist/dsession.py index 7ac5f57..2f06cb1 100644 --- a/src/xdist/dsession.py +++ b/src/xdist/dsession.py @@ -277,10 +277,17 @@ class DSession(object): self.config.hook.pytest_logwarning.call_historic(kwargs=kwargs) def worker_warning_captured(self, warning_message, when, item): - """Emitted when a node calls the pytest_logwarning hook.""" + """Emitted when a node calls the pytest_warning_captured hook (deprecated in 6.0).""" kwargs = dict(warning_message=warning_message, when=when, item=item) self.config.hook.pytest_warning_captured.call_historic(kwargs=kwargs) + def worker_warning_recorded(self, warning_message, when, nodeid, location): + """Emitted when a node calls the pytest_warning_recorded hook.""" + kwargs = dict( + warning_message=warning_message, when=when, nodeid=nodeid, location=location + ) + self.config.hook.pytest_warning_recorded.call_historic(kwargs=kwargs) + def _clone_node(self, node): """Return new node based on an existing one. diff --git a/src/xdist/remote.py b/src/xdist/remote.py index 28991c0..984dffb 100644 --- a/src/xdist/remote.py +++ b/src/xdist/remote.py @@ -151,6 +151,18 @@ class WorkerInteractor(object): item=None, ) + # the pytest_warning_recorded hook was introduced in pytest 6.0 + if hasattr(_pytest.hookspec, "pytest_warning_recorded"): + + def pytest_warning_recorded(self, warning_message, when, nodeid, location): + self.sendevent( + "warning_recorded", + warning_message_data=serialize_warning_message(warning_message), + when=when, + nodeid=nodeid, + location=location, + ) + def serialize_warning_message(warning_message): if isinstance(warning_message.message, Warning): diff --git a/src/xdist/workermanage.py b/src/xdist/workermanage.py index 6a47e1c..ff599f5 100644 --- a/src/xdist/workermanage.py +++ b/src/xdist/workermanage.py @@ -359,6 +359,17 @@ class WorkerController(object): when=kwargs["when"], item=kwargs["item"], ) + elif eventname == "warning_recorded": + warning_message = unserialize_warning_message( + kwargs["warning_message_data"] + ) + self.notify_inproc( + eventname, + warning_message=warning_message, + when=kwargs["when"], + nodeid=kwargs["nodeid"], + location=kwargs["location"], + ) else: raise ValueError("unknown event: {}".format(eventname)) except KeyboardInterrupt: