Skip to content

Commit

Permalink
Implement error/warning for the bad dynamic_context being set in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Sep 23, 2024
1 parent c299e01 commit a9ea7b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/pytest_cov/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import random
import socket
import sys
import warnings
from io import StringIO
from pathlib import Path

Expand All @@ -15,6 +16,11 @@
from coverage.sqldata import filename_suffix

from .embed import cleanup
from .plugin import PytestCovWarning


class BrokenCovConfigError(Exception):
pass


class _NullFile:
Expand Down Expand Up @@ -225,6 +231,10 @@ def summary(self, stream):
return total


class CentralCovContextWarning(PytestCovWarning):
pass


class Central(CovController):
"""Implementation for centralised operation."""

Expand All @@ -238,6 +248,13 @@ def start(self):
data_suffix=_data_suffix('c'),
config_file=self.cov_config,
)
if self.cov.config.dynamic_context == 'test_function':
message = (
'Detected dynamic_context=test_function in coverage configuration. '
'This is unnecessary as this plugin provides the more complete --cov-context option.'
)
warnings.warn(CentralCovContextWarning(message), stacklevel=1)

self.combining_cov = coverage.Coverage(
source=self.cov_source,
branch=self.cov_branch,
Expand Down Expand Up @@ -269,6 +286,10 @@ def finish(self):
self.node_descs.add(node_desc)


class DistCovError(Exception):
pass


class DistMaster(CovController):
"""Implementation for distributed master."""

Expand All @@ -282,6 +303,12 @@ def start(self):
data_suffix=_data_suffix('m'),
config_file=self.cov_config,
)
if self.cov.config.dynamic_context == 'test_function':
raise DistCovError(
'Detected dynamic_context=test_function in coverage configuration. '
'This is known to cause issues when using xdist, see: https://github.com/pytest-dev/pytest-cov/issues/604\n'
'It is recommended to use --cov-context instead.'
)
self.cov._warn_no_data = False
self.cov._warn_unimported_source = False
self.cov._warn_preimported_source = False
Expand Down
14 changes: 9 additions & 5 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,11 +1673,15 @@ def test_dynamic_context(pytester, testdir, opts, prop):
{prop.conf}
""")
result = testdir.runpytest('-v', f'--cov={script.dirpath()}', script, *opts.split() + prop.args)
result.stdout.fnmatch_lines(
[
f'test_1* {prop.result}*',
]
)
if opts:
result.stderr.fnmatch_lines(['pytest_cov.engine.DistCovError: Detected dynamic_context=test_function*'])
else:
result.stdout.fnmatch_lines(
[
'* CentralCovContextWarning: Detected dynamic_context=test_function*',
f'test_1* {prop.result}*',
]
)


@xdist_params
Expand Down

0 comments on commit a9ea7b7

Please sign in to comment.