Skip to content

Commit

Permalink
Move the warnings/errors in a place that doesn't import anything.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Sep 23, 2024
1 parent a9ea7b7 commit 76fe2a7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
44 changes: 44 additions & 0 deletions src/pytest_cov/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
"""pytest-cov: avoid already-imported warning: PYTEST_DONT_REWRITE."""

__version__ = '5.0.0'

import pytest


class CoverageError(Exception):
"""Indicates that our coverage is too low"""


class PytestCovWarning(pytest.PytestWarning):
"""
The base for all pytest-cov warnings, never raised directly.
"""


class CovDisabledWarning(PytestCovWarning):
"""
Indicates that Coverage was manually disabled.
"""


class CovReportWarning(PytestCovWarning):
"""
Indicates that we failed to generate a report.
"""


class CovFailUnderWarning(PytestCovWarning):
"""
Indicates that we failed to generate a report.
"""


class CentralCovContextWarning(PytestCovWarning):
"""
Indicates that dynamic_context was set to test_function instead of using the builtin --cov-context.
"""


class DistCovError(Exception):
"""
Raised when dynamic_context is set to test_function and xdist is also used.
See: https://github.com/pytest-dev/pytest-cov/issues/604
"""
11 changes: 2 additions & 9 deletions src/pytest_cov/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from coverage.data import CoverageData
from coverage.sqldata import filename_suffix

from . import CentralCovContextWarning
from . import DistCovError
from .embed import cleanup
from .plugin import PytestCovWarning


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


class CentralCovContextWarning(PytestCovWarning):
pass


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

Expand Down Expand Up @@ -286,10 +283,6 @@ def finish(self):
self.node_descs.add(node_desc)


class DistCovError(Exception):
pass


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

Expand Down
25 changes: 3 additions & 22 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,13 @@
from coverage.results import display_covered
from coverage.results import should_fail_under

from . import CovDisabledWarning
from . import CovFailUnderWarning
from . import CovReportWarning
from . import compat
from . import embed


class CoverageError(Exception):
"""Indicates that our coverage is too low"""


class PytestCovWarning(pytest.PytestWarning):
"""
The base for all pytest-cov warnings, never raised directly
"""


class CovDisabledWarning(PytestCovWarning):
"""Indicates that Coverage was manually disabled"""


class CovReportWarning(PytestCovWarning):
"""Indicates that we failed to generate a report"""


class CovFailUnderWarning(PytestCovWarning):
"""Indicates that we failed to generate a report"""


def validate_report(arg):
file_choices = ['annotate', 'html', 'xml', 'json', 'lcov']
term_choices = ['term', 'term-missing']
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ def test_dynamic_context(pytester, testdir, opts, prop):
""")
result = testdir.runpytest('-v', f'--cov={script.dirpath()}', script, *opts.split() + prop.args)
if opts:
result.stderr.fnmatch_lines(['pytest_cov.engine.DistCovError: Detected dynamic_context=test_function*'])
result.stderr.fnmatch_lines(['pytest_cov.DistCovError: Detected dynamic_context=test_function*'])
else:
result.stdout.fnmatch_lines(
[
Expand Down

0 comments on commit 76fe2a7

Please sign in to comment.