Skip to content

Commit

Permalink
--skip-empty now applies to the XML report also. #976
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jun 30, 2020
1 parent d00f254 commit 97f05c9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
10 changes: 7 additions & 3 deletions CHANGES.rst
Expand Up @@ -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,
Expand All @@ -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


Expand Down
6 changes: 5 additions & 1 deletion coverage/cmdline.py
Expand Up @@ -446,6 +446,7 @@ def get_prog_name(self):
Opts.include,
Opts.omit,
Opts.output_xml,
Opts.skip_empty,
] + GLOBAL_ARGS,
usage="[options] [modules]",
description="Generate an XML report of coverage results."
Expand Down Expand Up @@ -616,7 +617,10 @@ def command_line(self, argv):
)
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(
Expand Down
4 changes: 2 additions & 2 deletions coverage/control.py
Expand Up @@ -947,7 +947,7 @@ def html_report(

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.
Expand All @@ -963,7 +963,7 @@ def xml_report(
"""
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)

Expand Down
4 changes: 4 additions & 0 deletions coverage/xmlreport.py
Expand Up @@ -142,6 +142,10 @@ def report(self, morfs, outfile=None):
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("\\", "/")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_xml.py
Expand Up @@ -185,6 +185,13 @@ def test_empty_file_is_100_not_0(self):
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.
Expand Down

0 comments on commit 97f05c9

Please sign in to comment.