Skip to content

Commit aa02ed7

Browse files
committed
test: more checks of coverage warnings
1 parent 5b19217 commit aa02ed7

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

tests/plugin_test.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -226,31 +226,52 @@ def get_xml_report(self, name=None):
226226
return xml_coverage
227227

228228
@contextlib.contextmanager
229-
def assert_plugin_disabled(self, msg):
230-
"""Assert that our plugin was disabled during an operation."""
231-
# self.run_django_coverage will raise PluginDisabled if the plugin
232-
# was disabled.
229+
def assert_coverage_warnings(self, *msgs, min_cov=None):
230+
"""Assert that coverage.py warnings are raised that contain all msgs.
231+
232+
If coverage version isn't at least min_cov, then no warnings are expected.
233+
234+
"""
233235
# Coverage.py 6.0 made the warnings real warnings, so we have to adapt
234236
# how we test the warnings based on the version.
235-
if coverage.version.version_info >= (6, 0):
237+
if min_cov is not None and coverage.version_info < min_cov:
238+
# Don't check for warnings on lower versions of coverage
239+
yield
240+
return
241+
elif coverage.version_info >= (6, 0):
236242
import coverage.exceptions as cov_exc
237243
ctxmgr = self.assertWarns(cov_exc.CoverageWarning)
238244
else:
239-
ctxmgr = nullcontext()
245+
ctxmgr = contextlib.nullcontext()
240246
with ctxmgr as cw:
241-
with self.assertRaises(PluginDisabled):
242-
yield
247+
yield
243248

244249
if cw is not None:
245250
warn_text = "\n".join(str(w.message) for w in cw.warnings)
246251
else:
247252
warn_text = self.stderr()
248-
self.assertIn(
249-
"Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' "
250-
"due to an exception:",
251-
warn_text
252-
)
253-
self.assertIn("DjangoTemplatePluginException: " + msg, warn_text)
253+
for msg in msgs:
254+
self.assertIn(msg, warn_text)
255+
256+
@contextlib.contextmanager
257+
def assert_plugin_disabled(self, msg):
258+
"""Assert that our plugin was disabled during an operation."""
259+
# self.run_django_coverage will raise PluginDisabled if the plugin
260+
# was disabled.
261+
msgs = [
262+
"Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' due to an exception:",
263+
"DjangoTemplatePluginException: " + msg,
264+
]
265+
with self.assert_coverage_warnings(*msgs):
266+
with self.assertRaises(PluginDisabled):
267+
yield
268+
269+
@contextlib.contextmanager
270+
def assert_no_data(self, min_cov=None):
271+
"""Assert that coverage warns no data was collected."""
272+
warn_msg = "No data was collected. (no-data-collected)"
273+
with self.assert_coverage_warnings(warn_msg, min_cov=min_cov):
274+
yield
254275

255276

256277
def squashed(s):
@@ -289,9 +310,3 @@ def test_thing(self):
289310
class PluginDisabled(Exception):
290311
"""Raised if we find that our plugin has been disabled."""
291312
pass
292-
293-
294-
@contextlib.contextmanager
295-
def nullcontext():
296-
"""For when we need a context manager to do nothing."""
297-
yield None

tests/test_engines.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_file_template(self):
3333
self.assert_analysis([1])
3434

3535
def test_string_template(self):
36-
text = self.run_django_coverage(text='Hello', using='other')
36+
with self.assert_no_data():
37+
text = self.run_django_coverage(text='Hello', using='other')
3738
self.assertEqual(text, 'Hello')
3839

3940
def test_third_engine_not_debug(self):

tests/test_simple.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,14 @@ class StringTemplateTest(DjangoPluginTestCase):
245245
run_in_temp_dir = False
246246

247247
def test_string_template(self):
248-
text = self.run_django_coverage(
249-
text="Hello, {{name}}!",
250-
context={'name': 'World'},
251-
options={},
252-
)
248+
# I don't understand why coverage 6 warns about no data,
249+
# but coverage 5 does not.
250+
with self.assert_no_data(min_cov=(6, 0)):
251+
text = self.run_django_coverage(
252+
text="Hello, {{name}}!",
253+
context={'name': 'World'},
254+
options={},
255+
)
253256
self.assertEqual(text, "Hello, World!")
254257

255258

tests/test_source.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ def test_non_utf8_ignored(self):
133133

134134
self.assert_measured_files("main.html", "static{}changelog.txt".format(os.sep))
135135
self.assert_analysis([1], name="main.html")
136-
self.cov.html_report()
136+
warn_msg = (
137+
"'utf-8' codec can't decode byte 0xf6 in position 2: " +
138+
"invalid start byte (couldnt-parse)"
139+
)
140+
with self.assert_coverage_warnings(warn_msg, min_cov=(6, 0)):
141+
self.cov.html_report()
137142

138143
def test_htmlcov_isnt_measured(self):
139144
# We used to find the HTML report and think it was template files.

0 commit comments

Comments
 (0)