summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2019-10-10 06:16:39 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-10-10 06:16:39 -0400
commit54d8366cc755ebabdd15b6418f209e7339321847 (patch)
treefceda8f39c8578cbb42f86209ba78a8ba9112b2a
parent032ac34a2388f60777431bb54cbcd7075dae7afd (diff)
downloadpython-coveragepy-git-54d8366cc755ebabdd15b6418f209e7339321847.tar.gz
Warn if asked to show context but none were measured. #851
-rw-r--r--CHANGES.rst6
-rw-r--r--coverage/html.py3
-rw-r--r--tests/test_html.py31
3 files changed, 29 insertions, 11 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 7e607cad..eb130b3e 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -22,6 +22,12 @@ Unreleased
- Added sqlite3 module version information to ``coverage debug sys`` output.
+- Asking the HTML report to show contexts (``[html] show_contexts=True`` or
+ ``coverage html --show-contexts``) will issue a warning if there were no
+ contexts measured (`issue 851`_).
+
+.. _issue 851: https://github.com/nedbat/coveragepy/issues/851
+
.. _changes_50a8:
diff --git a/coverage/html.py b/coverage/html.py
index e560f76b..5b4c6ca9 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -82,6 +82,9 @@ class HtmlDataGeneration(object):
self.config = self.coverage.config
data = self.coverage.get_data()
self.has_arcs = data.has_arcs()
+ if self.config.show_contexts:
+ if data.measured_contexts() == set([""]):
+ self.coverage._warn("No contexts were measured")
data.set_query_contexts(self.config.report_contexts)
def data_for_file(self, fr, analysis):
diff --git a/tests/test_html.py b/tests/test_html.py
index 7c1c74f0..29f81a05 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -1035,18 +1035,18 @@ assert len(math) == 18
)
-def html_data_from_cov(cov, morf):
- """Get HTML report data from a `Coverage` object for a morf."""
- datagen = coverage.html.HtmlDataGeneration(cov)
- for fr, analysis in get_analysis_to_report(cov, [morf]):
- # This will only loop once, so it's fine to return inside the loop.
- file_data = datagen.data_for_file(fr, analysis)
- return file_data
-
-
class HtmlWithContextsTest(HtmlTestHelpers, CoverageTest):
"""Tests of the HTML reports with shown contexts."""
+ def html_data_from_cov(self, cov, morf):
+ """Get HTML report data from a `Coverage` object for a morf."""
+ with self.assert_warnings(cov, []):
+ datagen = coverage.html.HtmlDataGeneration(cov)
+ for fr, analysis in get_analysis_to_report(cov, [morf]):
+ # This will only loop once, so it's fine to return inside the loop.
+ file_data = datagen.data_for_file(fr, analysis)
+ return file_data
+
SOURCE = """\
def helper(lineno):
x = 2
@@ -1082,7 +1082,7 @@ class HtmlWithContextsTest(HtmlTestHelpers, CoverageTest):
cov.set_option("run:dynamic_context", "test_function")
cov.set_option("html:show_contexts", True)
mod = self.start_import_stop(cov, "two_tests")
- d = html_data_from_cov(cov, mod)
+ d = self.html_data_from_cov(cov, mod)
context_labels = ['(empty)', 'two_tests.test_one', 'two_tests.test_two']
expected_lines = [self.OUTER_LINES, self.TEST_ONE_LINES, self.TEST_TWO_LINES]
@@ -1097,10 +1097,19 @@ class HtmlWithContextsTest(HtmlTestHelpers, CoverageTest):
cov.set_option("html:show_contexts", True)
cov.set_option("report:contexts", ["test_one"])
mod = self.start_import_stop(cov, "two_tests")
- d = html_data_from_cov(cov, mod)
+ d = self.html_data_from_cov(cov, mod)
context_labels = ['(empty)', 'two_tests.test_one', 'two_tests.test_two']
expected_lines = [[], self.TEST_ONE_LINES, []]
for label, expected in zip(context_labels, expected_lines):
actual = [ld.number for ld in d.lines if label in (ld.contexts or ())]
assert sorted(expected) == sorted(actual)
+
+ def test_no_contexts_warns_no_contexts(self):
+ # If no contexts were collected, then show_contexts emits a warning.
+ self.make_file("two_tests.py", self.SOURCE)
+ cov = coverage.Coverage(source=["."])
+ cov.set_option("html:show_contexts", True)
+ self.start_import_stop(cov, "two_tests")
+ with self.assert_warnings(cov, ["No contexts were measured"]):
+ cov.html_report()