Skip to content

Commit 97f05c9

Browse files
committed
--skip-empty now applies to the XML report also. #976
1 parent d00f254 commit 97f05c9

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

CHANGES.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ Unreleased
3232
``--precision`` option to control the number of decimal points displayed.
3333
Thanks, Teake Nutma (`pull request 982`_).
3434

35-
- The ``coverage report`` command now accepts a ``--no-skip-covered`` option
36-
to negate ``--skip-covered``. Thanks, Anthony Sottile (`issue 779`_ and
37-
`pull request 932`_).
35+
- The ``coverage report`` and ``coverage html`` commands now accept a
36+
``--no-skip-covered`` option to negate ``--skip-covered``. Thanks, Anthony
37+
Sottile (`issue 779`_ and `pull request 932`_).
38+
39+
- The ``--skip-empty`` option is now available for the XML report, closing
40+
`issue 976`_.
3841

3942
- If coverage fails due to the coverage total not reaching the ``--fail-under``
4043
value, it will now print a message making the condition clear. Thanks,
@@ -53,6 +56,7 @@ Unreleased
5356
.. _pull request 982: https://github.com/nedbat/coveragepy/pull/982
5457
.. _issue 779: https://github.com/nedbat/coveragepy/issues/779
5558
.. _issue 858: https://github.com/nedbat/coveragepy/issues/858
59+
.. _issue 976: https://github.com/nedbat/coveragepy/issues/976
5660
.. _issue 990: https://github.com/nedbat/coveragepy/issues/990
5761

5862

coverage/cmdline.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ def get_prog_name(self):
446446
Opts.include,
447447
Opts.omit,
448448
Opts.output_xml,
449+
Opts.skip_empty,
449450
] + GLOBAL_ARGS,
450451
usage="[options] [modules]",
451452
description="Generate an XML report of coverage results."
@@ -616,7 +617,10 @@ def command_line(self, argv):
616617
)
617618
elif options.action == "xml":
618619
outfile = options.outfile
619-
total = self.coverage.xml_report(outfile=outfile, **report_args)
620+
total = self.coverage.xml_report(
621+
outfile=outfile, skip_empty=options.skip_empty,
622+
**report_args
623+
)
620624
elif options.action == "json":
621625
outfile = options.outfile
622626
total = self.coverage.json_report(

coverage/control.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ def html_report(
947947

948948
def xml_report(
949949
self, morfs=None, outfile=None, ignore_errors=None,
950-
omit=None, include=None, contexts=None,
950+
omit=None, include=None, contexts=None, skip_empty=None,
951951
):
952952
"""Generate an XML report of coverage results.
953953
@@ -963,7 +963,7 @@ def xml_report(
963963
"""
964964
with override_config(self,
965965
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
966-
xml_output=outfile, report_contexts=contexts,
966+
xml_output=outfile, report_contexts=contexts, skip_empty=skip_empty,
967967
):
968968
return render_report(self.config.xml_output, XmlReporter(self), morfs)
969969

coverage/xmlreport.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ def report(self, morfs, outfile=None):
142142
def xml_file(self, fr, analysis, has_arcs):
143143
"""Add to the XML report for a single file."""
144144

145+
if self.config.skip_empty:
146+
if analysis.numbers.n_statements == 0:
147+
return
148+
145149
# Create the 'lines' and 'package' XML elements, which
146150
# are populated later. Note that a package == a directory.
147151
filename = fr.filename.replace("\\", "/")

tests/test_cmdline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BaseCmdLineTest(CoverageTest):
4545
)
4646
_defaults.Coverage().xml_report(
4747
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
48-
contexts=None,
48+
contexts=None, skip_empty=None,
4949
)
5050
_defaults.Coverage().json_report(
5151
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,

tests/test_xml.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ def test_empty_file_is_100_not_0(self):
185185
assert len(elts) == 1
186186
assert elts[0].get('line-rate') == '1'
187187

188+
def test_empty_file_is_skipped(self):
189+
cov = self.run_doit()
190+
cov.xml_report(skip_empty=True)
191+
dom = ElementTree.parse("coverage.xml")
192+
elts = dom.findall(".//class[@name='__init__.py']")
193+
assert len(elts) == 0
194+
188195
def test_curdir_source(self):
189196
# With no source= option, the XML report should explain that the source
190197
# is in the current directory.

0 commit comments

Comments
 (0)