diff options
-rw-r--r-- | CHANGES.txt | 9 | ||||
-rw-r--r-- | coverage/xmlreport.py | 23 | ||||
-rw-r--r-- | tests/test_xml.py | 9 |
3 files changed, 36 insertions, 5 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index fdab08f8..ef7f7ea5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,15 @@ Change history for Coverage.py ------------------------------ +Latest +------ + +- Empty files are now reported as 100% covered in the XML report, not 0% + covered, as reported in `issue 345`_. + +.. _issue 345: https://bitbucket.org/ned/coveragepy/issue/345/xml-reports-line-rate-0-for-empty-files + + Version 4.0a2 --- 14 January 2014 --------------------------------- diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index 92a8975a..45a4652e 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -8,7 +8,10 @@ from coverage.report import Reporter def rate(hit, num): """Return the fraction of `hit`/`num`, as a string.""" - return "%.4g" % (float(hit) / (num or 1.0)) + if num == 0: + return "1" + else: + return "%.4g" % (float(hit) / num) class XmlReporter(Reporter): @@ -80,7 +83,11 @@ class XmlReporter(Reporter): xclasses.appendChild(class_elts[class_name]) xpackage.setAttribute("name", pkg_name.replace(os.sep, '.')) xpackage.setAttribute("line-rate", rate(lhits, lnum)) - xpackage.setAttribute("branch-rate", rate(bhits, bnum)) + if self.arcs: + branch_rate = rate(bhits, bnum) + else: + branch_rate = "0" + xpackage.setAttribute("branch-rate", branch_rate) xpackage.setAttribute("complexity", "0") lnum_tot += lnum @@ -89,7 +96,11 @@ class XmlReporter(Reporter): bhits_tot += bhits xcoverage.setAttribute("line-rate", rate(lhits_tot, lnum_tot)) - xcoverage.setAttribute("branch-rate", rate(bhits_tot, bnum_tot)) + if self.arcs: + branch_rate = rate(bhits_tot, bnum_tot) + else: + branch_rate = "0" + xcoverage.setAttribute("branch-rate", branch_rate) # Use the DOM to write the output file. outfile.write(self.xml_out.toprettyxml()) @@ -158,7 +169,11 @@ class XmlReporter(Reporter): # Finalize the statistics that are collected in the XML DOM. xclass.setAttribute("line-rate", rate(class_hits, class_lines)) - xclass.setAttribute("branch-rate", rate(class_br_hits, class_branches)) + if self.arcs: + branch_rate = rate(class_br_hits, class_branches) + else: + branch_rate = "0" + xclass.setAttribute("branch-rate", branch_rate) package[0][className] = xclass package[1] += class_hits package[2] += class_lines diff --git a/tests/test_xml.py b/tests/test_xml.py index 3c7d2361..bc8c0516 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -96,7 +96,14 @@ class XmlReportTest(CoverageTest): xml = self.stdout() empty_line = re_line(xml, "class.*empty") self.assertIn('filename="empty.py"', empty_line) - self.assertIn('line-rate="0"', empty_line) + self.assertIn('line-rate="1"', empty_line) + + def test_empty_file_is_100_not_0(self): + cov = self.run_doit() + cov.xml_report(outfile="-") + xml = self.stdout() + init_line = re_line(xml, 'filename="sub/__init__.py"') + self.assertIn('line-rate="1"', init_line) def re_line(text, pat): |