Skip to content

Commit c4ec069

Browse files
committed
test: adapt to coverage.py 6.0 changes
Coverage.py 6.0 issues true Python warnings instead of just printing to stderr, so the tests have to adapt based on the coverage version.
1 parent 4ab844c commit c4ec069

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

tests/plugin_test.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,27 @@ def assert_plugin_disabled(self, msg):
231231
"""Assert that our plugin was disabled during an operation."""
232232
# self.run_django_coverage will raise PluginDisabled if the plugin
233233
# was disabled.
234-
with self.assertRaises(PluginDisabled):
235-
yield
236-
stderr = self.stderr()
234+
# Coverage.py 6.0 made the warnings real warnings, so we have to adapt
235+
# how we test the warnings based on the version.
236+
if coverage.version.version_info >= (6, 0):
237+
import coverage.exceptions as cov_exc
238+
ctxmgr = self.assertWarns(cov_exc.CoverageWarning)
239+
else:
240+
ctxmgr = nullcontext()
241+
with ctxmgr as cw:
242+
with self.assertRaises(PluginDisabled):
243+
yield
244+
245+
if cw is not None:
246+
warn_text = "\n".join(str(w.message) for w in cw.warnings)
247+
else:
248+
warn_text = self.stderr()
237249
self.assertIn(
238-
"Coverage.py warning: "
239250
"Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' "
240251
"due to an exception:",
241-
stderr
242-
)
243-
self.assertIn(
244-
"DjangoTemplatePluginException: " + msg,
245-
stderr
252+
warn_text
246253
)
254+
self.assertIn("DjangoTemplatePluginException: " + msg, warn_text)
247255

248256

249257
def squashed(s):
@@ -282,3 +290,9 @@ def test_thing(self):
282290
class PluginDisabled(Exception):
283291
"""Raised if we find that our plugin has been disabled."""
284292
pass
293+
294+
295+
@contextlib.contextmanager
296+
def nullcontext():
297+
"""For when we need a context manager to do nothing."""
298+
yield None

0 commit comments

Comments
 (0)