Skip to content

Commit

Permalink
Warn if two things change the dynamic context. #901
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Dec 27, 2019
1 parent cc989e2 commit 16ef35f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ def switch_context(self, new_context):
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'):
Expand Down
6 changes: 6 additions & 0 deletions doc/cmd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
13 changes: 13 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,19 @@ def test_switch_context_with_static(self):
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"
Expand Down

0 comments on commit 16ef35f

Please sign in to comment.