Skip to content

Commit

Permalink
Work with xdist 2.0
Browse files Browse the repository at this point in the history
Fixes #411, at cost of support for xdist 1.22 and earlier from 2017.
  • Loading branch information
Zac-HD committed Jun 14, 2020
1 parent 694f7fd commit 193932f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 33 deletions.
19 changes: 0 additions & 19 deletions src/pytest_cov/compat.py
Expand Up @@ -29,22 +29,3 @@ def testsfailed(self):
@testsfailed.setter
def testsfailed(self, value):
setattr(self._session, self._attr, value)


def _attrgetter(attr):
"""
Return a callable object that fetches attr from its operand.
Unlike operator.attrgetter, the returned callable supports an extra two
arg form for a default.
"""
def fn(obj, *args):
return getattr(obj, attr, *args)

return fn


worker = 'slave' # for compatability with pytest-xdist<=1.22.0
workerid = worker + 'id'
workerinput = _attrgetter(worker + 'input')
workeroutput = _attrgetter(worker + 'output')
16 changes: 7 additions & 9 deletions src/pytest_cov/engine.py
Expand Up @@ -11,8 +11,6 @@
from coverage.data import CoverageData

from .compat import StringIO
from .compat import workerinput
from .compat import workeroutput
from .embed import cleanup


Expand Down Expand Up @@ -271,7 +269,7 @@ def start(self):
def configure_node(self, node):
"""Workers need to know if they are collocated and what files have moved."""

workerinput(node).update({
node.workerinput.update({
'cov_master_host': socket.gethostname(),
'cov_master_topdir': self.topdir,
'cov_master_rsync_roots': [str(root) for root in node.nodemanager.roots],
Expand All @@ -282,7 +280,7 @@ def testnodedown(self, node, error):

# If worker doesn't return any data then it is likely that this
# plugin didn't get activated on the worker side.
output = workeroutput(node, {})
output = getattr(node, 'workeroutput', {})
if 'cov_worker_node_id' not in output:
self.failed_workers.append(node)
return
Expand Down Expand Up @@ -341,12 +339,12 @@ def start(self):
cleanup()

# Determine whether we are collocated with master.
self.is_collocated = (socket.gethostname() == workerinput(self.config)['cov_master_host'] and
self.topdir == workerinput(self.config)['cov_master_topdir'])
self.is_collocated = (socket.gethostname() == self.config.workerinput['cov_master_host'] and
self.topdir == self.config.workerinput['cov_master_topdir'])

# If we are not collocated then rewrite master paths to worker paths.
if not self.is_collocated:
master_topdir = workerinput(self.config)['cov_master_topdir']
master_topdir = self.config.workerinput['cov_master_topdir']
worker_topdir = self.topdir
if self.cov_source is not None:
self.cov_source = [source.replace(master_topdir, worker_topdir)
Expand Down Expand Up @@ -375,7 +373,7 @@ def finish(self):

# If we are collocated then just inform the master of our
# data file to indicate that we have finished.
workeroutput(self.config)['cov_worker_node_id'] = self.nodeid
self.config.workeroutput['cov_worker_node_id'] = self.nodeid
else:
self.cov.combine()
self.cov.save()
Expand All @@ -391,7 +389,7 @@ def finish(self):
else:
data = self.cov.get_data().dumps()

workeroutput(self.config).update({
self.config.workeroutput.update({
'cov_worker_path': self.topdir,
'cov_worker_node_id': self.nodeid,
'cov_worker_data': data,
Expand Down
5 changes: 2 additions & 3 deletions src/pytest_cov/plugin.py
Expand Up @@ -203,7 +203,7 @@ class Config(object):
self.options.cov_fail_under = cov_config.fail_under

def _is_worker(self, session):
return compat.workerinput(session.config, None) is not None
return getattr(session.config, 'workerinput', None) is not None

def pytest_sessionstart(self, session):
"""At session start determine our implementation and delegate to it."""
Expand All @@ -220,8 +220,7 @@ def pytest_sessionstart(self, session):
self.pid = os.getpid()
if self._is_worker(session):
nodeid = (
compat.workerinput(session.config)
.get(compat.workerid, getattr(session, 'nodeid'))
session.config.workerinput.get('workerid', getattr(session, 'nodeid'))
)
self.start(engine.DistWorker, session.config, nodeid)
elif not self._started:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_pytest_cov.py
Expand Up @@ -19,7 +19,6 @@
from six import exec_

import pytest_cov.plugin
from pytest_cov import compat

try:
from StringIO import StringIO
Expand All @@ -28,7 +27,7 @@

coverage, platform # required for skipif mark on test_cov_min_from_coveragerc

max_worker_restart_0 = "--max-" + compat.worker + "-restart=0"
max_worker_restart_0 = "--max-worker-restart=0"

SCRIPT = '''
import sys, helper
Expand Down

0 comments on commit 193932f

Please sign in to comment.