summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2020-06-30 08:03:45 -0400
committerNed Batchelder <ned@nedbatchelder.com>2020-06-30 08:03:45 -0400
commit97f05c988545f8ca588253fd325401c4834543b6 (patch)
tree41b223ce1a1bf1f33aaad9ffd41351156ad92da6
parentd00f254315ef4e97e86e230c448d6325c97e08dc (diff)
downloadpython-coveragepy-git-nedbat/bug976.tar.gz
--skip-empty now applies to the XML report also. #976nedbat/bug976
-rw-r--r--CHANGES.rst10
-rw-r--r--coverage/cmdline.py6
-rw-r--r--coverage/control.py4
-rw-r--r--coverage/xmlreport.py4
-rw-r--r--tests/test_cmdline.py2
-rw-r--r--tests/test_xml.py7
6 files changed, 26 insertions, 7 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index a68a0f06..46f86798 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -32,9 +32,12 @@ Unreleased
``--precision`` option to control the number of decimal points displayed.
Thanks, Teake Nutma (`pull request 982`_).
-- The ``coverage report`` command now accepts a ``--no-skip-covered`` option
- to negate ``--skip-covered``. Thanks, Anthony Sottile (`issue 779`_ and
- `pull request 932`_).
+- The ``coverage report`` and ``coverage html`` commands now accept a
+ ``--no-skip-covered`` option to negate ``--skip-covered``. Thanks, Anthony
+ Sottile (`issue 779`_ and `pull request 932`_).
+
+- The ``--skip-empty`` option is now available for the XML report, closing
+ `issue 976`_.
- If coverage fails due to the coverage total not reaching the ``--fail-under``
value, it will now print a message making the condition clear. Thanks,
@@ -53,6 +56,7 @@ Unreleased
.. _pull request 982: https://github.com/nedbat/coveragepy/pull/982
.. _issue 779: https://github.com/nedbat/coveragepy/issues/779
.. _issue 858: https://github.com/nedbat/coveragepy/issues/858
+.. _issue 976: https://github.com/nedbat/coveragepy/issues/976
.. _issue 990: https://github.com/nedbat/coveragepy/issues/990
diff --git a/coverage/cmdline.py b/coverage/cmdline.py
index 7af7913e..059d42a2 100644
--- a/coverage/cmdline.py
+++ b/coverage/cmdline.py
@@ -446,6 +446,7 @@ CMDS = {
Opts.include,
Opts.omit,
Opts.output_xml,
+ Opts.skip_empty,
] + GLOBAL_ARGS,
usage="[options] [modules]",
description="Generate an XML report of coverage results."
@@ -616,7 +617,10 @@ class CoverageScript(object):
)
elif options.action == "xml":
outfile = options.outfile
- total = self.coverage.xml_report(outfile=outfile, **report_args)
+ total = self.coverage.xml_report(
+ outfile=outfile, skip_empty=options.skip_empty,
+ **report_args
+ )
elif options.action == "json":
outfile = options.outfile
total = self.coverage.json_report(
diff --git a/coverage/control.py b/coverage/control.py
index 14c22eb1..bd7ba2e6 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -947,7 +947,7 @@ class Coverage(object):
def xml_report(
self, morfs=None, outfile=None, ignore_errors=None,
- omit=None, include=None, contexts=None,
+ omit=None, include=None, contexts=None, skip_empty=None,
):
"""Generate an XML report of coverage results.
@@ -963,7 +963,7 @@ class Coverage(object):
"""
with override_config(self,
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
- xml_output=outfile, report_contexts=contexts,
+ xml_output=outfile, report_contexts=contexts, skip_empty=skip_empty,
):
return render_report(self.config.xml_output, XmlReporter(self), morfs)
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index ad44775f..6d012ee6 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -142,6 +142,10 @@ class XmlReporter(object):
def xml_file(self, fr, analysis, has_arcs):
"""Add to the XML report for a single file."""
+ if self.config.skip_empty:
+ if analysis.numbers.n_statements == 0:
+ return
+
# Create the 'lines' and 'package' XML elements, which
# are populated later. Note that a package == a directory.
filename = fr.filename.replace("\\", "/")
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index e84f6dac..db70a994 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -45,7 +45,7 @@ class BaseCmdLineTest(CoverageTest):
)
_defaults.Coverage().xml_report(
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
- contexts=None,
+ contexts=None, skip_empty=None,
)
_defaults.Coverage().json_report(
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
diff --git a/tests/test_xml.py b/tests/test_xml.py
index 0d789fca..8c399594 100644
--- a/tests/test_xml.py
+++ b/tests/test_xml.py
@@ -185,6 +185,13 @@ class XmlReportTest(XmlTestHelpers, CoverageTest):
assert len(elts) == 1
assert elts[0].get('line-rate') == '1'
+ def test_empty_file_is_skipped(self):
+ cov = self.run_doit()
+ cov.xml_report(skip_empty=True)
+ dom = ElementTree.parse("coverage.xml")
+ elts = dom.findall(".//class[@name='__init__.py']")
+ assert len(elts) == 0
+
def test_curdir_source(self):
# With no source= option, the XML report should explain that the source
# is in the current directory.