diff options
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | coverage/xmlreport.py | 7 | ||||
-rw-r--r-- | tests/test_xml.py | 17 |
3 files changed, 26 insertions, 2 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 10ad0b22..c7e25b28 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -28,6 +28,9 @@ Change history for Coverage.py ``~build/src``, for example), and with environment variable expansion (``build/$BUILDNUM/src``). +- Trying to create an XML report with the nose plugin, but no files to report + on, would cause a ZeroDivideError, but no longer does, fixing `issue 250`_. + - When running a threaded program under the Python tracer, coverage no longer issues a spurious warning about the trace function changing: "Trace function changed, measurement is likely wrong: None." This fixes `issue 164`_. @@ -47,6 +50,7 @@ Change history for Coverage.py .. _issue 207: https://bitbucket.org/ned/coveragepy/issue/207/run-m-cannot-find-module-or-package-in .. _issue 242: https://bitbucket.org/ned/coveragepy/issue/242/running-a-two-level-package-doesnt-work .. _issue 218: https://bitbucket.org/ned/coveragepy/issue/218/run-command-does-not-respect-the-omit-flag +.. _issue 250: https://bitbucket.org/ned/coveragepy/issue/250/uncaught-zerodivisionerror-when-generating .. _issue 255: https://bitbucket.org/ned/coveragepy/issue/255/directory-level-__main__py-not-included-in .. _issue 259: https://bitbucket.org/ned/coveragepy/issue/259/allow-use-of-system-installed-third-party .. _issue 261: https://bitbucket.org/ned/coveragepy/issue/261/pyw-files-arent-reported-properly diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index 301bc865..78375249 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -85,7 +85,12 @@ class XmlReporter(Reporter): outfile.write(self.xml_out.toprettyxml()) # Return the total percentage. - return 100.0 * (lhits_tot + bhits_tot) / (lnum_tot + bnum_tot) + denom = lnum_tot + bnum_tot + if denom == 0: + pct = 0.0 + else: + pct = 100.0 * (lhits_tot + bhits_tot) / denom + return pct def xml_file(self, cu, analysis): """Add to the XML report for a single file.""" diff --git a/tests/test_xml.py b/tests/test_xml.py index 1659aa0a..0801bad3 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -1,10 +1,12 @@ """Tests for XML reports from coverage.py.""" -import os, re +import os +import re import coverage from tests.coveragetest import CoverageTest + class XmlReportTest(CoverageTest): """Tests of the XML reports from coverage.py.""" @@ -77,8 +79,21 @@ class XmlReportTest(CoverageTest): doit_line = re_line(xml, "class.*doit") self.assertIn('filename="sub/doit.py"', doit_line) + def test_reporting_on_nothing(self): + # Used to raise a zero division error: + # https://bitbucket.org/ned/coveragepy/issue/250 + self.make_file("empty.py", "") + cov = coverage.coverage() + empty = self.start_import_stop(cov, "empty") + cov.xml_report([empty], outfile="-") + xml = self.stdout() + empty_line = re_line(xml, "class.*empty") + self.assertIn('filename="empty.py"', empty_line) + self.assertIn('line-rate="0"', empty_line) + def re_line(text, pat): """Return the one line in `text` that matches regex `pat`.""" lines = [l for l in text.splitlines() if re.search(pat, l)] + assert len(lines) == 1 return lines[0] |