From 76fe2a7e48e5c9c53644994c5ba5a421c84286f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Wed, 18 Sep 2024 04:42:19 +0300 Subject: [PATCH] Move the warnings/errors in a place that doesn't import anything. --- src/pytest_cov/__init__.py | 44 ++++++++++++++++++++++++++++++++++++++ src/pytest_cov/engine.py | 11 ++-------- src/pytest_cov/plugin.py | 25 +++------------------- tests/test_pytest_cov.py | 2 +- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/pytest_cov/__init__.py b/src/pytest_cov/__init__.py index 82ce08bb..87a54122 100644 --- a/src/pytest_cov/__init__.py +++ b/src/pytest_cov/__init__.py @@ -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 + """ diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index 73e2f8dc..650dbcc9 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -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): @@ -231,10 +232,6 @@ def summary(self, stream): return total -class CentralCovContextWarning(PytestCovWarning): - pass - - class Central(CovController): """Implementation for centralised operation.""" @@ -286,10 +283,6 @@ def finish(self): self.node_descs.add(node_desc) -class DistCovError(Exception): - pass - - class DistMaster(CovController): """Implementation for distributed master.""" diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index f0835af6..b2b103f0 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -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'] diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 2c9d426e..72f7ab1a 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -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( [