Skip to content

Commit

Permalink
Support new pytest_warning_recorded hook from pytest 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 8, 2020
1 parent 77a1db2 commit b257571
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/xdist/dsession.py
Expand Up @@ -277,10 +277,17 @@ def worker_logwarning(self, message, code, nodeid, fslocation):
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.
Expand Down
12 changes: 12 additions & 0 deletions src/xdist/remote.py
Expand Up @@ -151,6 +151,18 @@ def pytest_warning_captured(self, warning_message, when, item):
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):
Expand Down
11 changes: 11 additions & 0 deletions src/xdist/workermanage.py
Expand Up @@ -359,6 +359,17 @@ def process_from_remote(self, eventcall): # noqa too complex
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:
Expand Down

0 comments on commit b257571

Please sign in to comment.