diff options
-rw-r--r-- | CHANGES.rst | 4 | ||||
-rw-r--r-- | coverage/control.py | 4 | ||||
-rw-r--r-- | doc/cmd.rst | 6 | ||||
-rw-r--r-- | tests/test_api.py | 13 |
4 files changed, 27 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 4a177a19..262a44ec 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -30,7 +30,11 @@ Unreleased information about the config files read now shows absolute paths to the files. +- A new warning ("dynamic-conflict") is issued if two mechanisms are trying to + change the dynamic context. Closes `issue 901`_. + .. _issue 890: https://github.com/nedbat/coveragepy/issues/890 +.. _issue 901: https://github.com/nedbat/coveragepy/issues/901 .. _changes_501: diff --git a/coverage/control.py b/coverage/control.py index 4358a541..c40508da 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -581,6 +581,10 @@ class Coverage(object): raise CoverageException( "Cannot switch context, coverage is not started" ) + + if self._collector.should_start_context: + self._warn("Conflicting dynamic contexts", slug="dynamic-conflict", once=True) + self._collector.switch_context(new_context) def clear_exclude(self, which='exclude'): diff --git a/doc/cmd.rst b/doc/cmd.rst index 8b0e2f24..3933c567 100644 --- a/doc/cmd.rst +++ b/doc/cmd.rst @@ -179,6 +179,12 @@ could affect the measurement process. The possible warnings include: are meant to focus measurement on a particular part of your source code, so ``--include`` is ignored in favor of ``--source``. +* ``Conflicting dynamic contexts (dynamic-conflict)`` |br| + The ``[run] dynamic_context`` option is set in the configuration file, but + something (probably a test runner plugin) is also calling the + :meth:`.Coverage.switch_context` function to change the context. Only one of + these mechanisms should be in use at a time. + Individual warnings can be disabled with the `disable_warnings <config_run_disable_warnings>`_ configuration setting. To silence "No data was collected," add this to your .coveragerc file:: diff --git a/tests/test_api.py b/tests/test_api.py index 63a0c7b1..0a6b71b9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -672,6 +672,19 @@ class ApiTest(CoverageTest): data.set_query_context("mysuite|multiply_zero") self.assertEqual([2, 5], sorted(data.lines(suite_filename))) + def test_dynamic_context_conflict(self): + cov = coverage.Coverage(source=["."]) + cov.set_option("run:dynamic_context", "test_function") + cov.start() + # Switch twice, but only get one warning. + cov.switch_context("test1") # pragma: nested + cov.switch_context("test2") # pragma: nested + self.assertEqual( # pragma: nested + self.stderr(), + "Coverage.py warning: Conflicting dynamic contexts (dynamic-conflict)\n" + ) + cov.stop() # pragma: nested + def test_switch_context_unstarted(self): # Coverage must be started to switch context msg = "Cannot switch context, coverage is not started" |