Skip to content

Commit

Permalink
Add --always-total option. nedbat#1086.
Browse files Browse the repository at this point in the history
  • Loading branch information
lordjabez committed Jan 4, 2021
1 parent 7ff93a9 commit be612f9
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -24,7 +24,8 @@ want to know what's different in 5.0 since 4.5.x, see :ref:`whatsnew5x`.
Unreleased
----------

Nothing yet.
- The TOTAL output line can now always be output, even when only one file
has been measured, by providing the `--always-total` option.


.. _changes_531:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -79,6 +79,7 @@ Jon Chappell
Jon Dufresne
Joseph Tate
Josh Williams
Judson Neer
Julian Berman
Julien Voisin
Justas Sadzevičius
Expand Down
7 changes: 7 additions & 0 deletions coverage/cmdline.py
Expand Up @@ -31,6 +31,10 @@ class Opts(object):
'-a', '--append', action='store_true',
help="Append coverage data to .coverage, otherwise it starts clean each time.",
)
always_total = optparse.make_option(
'', '--always-total', action='store_true',
help="Always output the TOTAL line, even when only one file is measured.",
)
branch = optparse.make_option(
'', '--branch', action='store_true',
help="Measure branch coverage in addition to statement coverage.",
Expand Down Expand Up @@ -204,6 +208,7 @@ def __init__(self, *args, **kwargs):
add_help_option=False, *args, **kwargs
)
self.set_defaults(
always_total=None,
action=None,
append=None,
branch=None,
Expand Down Expand Up @@ -413,6 +418,7 @@ def get_prog_name(self):
'report': CmdOptionParser(
"report",
[
Opts.always_total,
Opts.contexts,
Opts.fail_under,
Opts.ignore_errors,
Expand Down Expand Up @@ -607,6 +613,7 @@ def command_line(self, argv):
total = None
if options.action == "report":
total = self.coverage.report(
always_total=options.always_total,
show_missing=options.show_missing,
skip_covered=options.skip_covered,
skip_empty=options.skip_empty,
Expand Down
2 changes: 2 additions & 0 deletions coverage/config.py
Expand Up @@ -200,6 +200,7 @@ def __init__(self):
self._crash = None

# Defaults for [report]
self.always_total = False
self.exclude_list = DEFAULT_EXCLUDE[:]
self.fail_under = 0.0
self.ignore_errors = False
Expand Down Expand Up @@ -367,6 +368,7 @@ def copy(self):
('_crash', 'run:_crash'),

# [report]
('always_total', 'report:always_total', 'boolean'),
('exclude_list', 'report:exclude_lines', 'regexlist'),
('fail_under', 'report:fail_under', 'float'),
('ignore_errors', 'report:ignore_errors', 'boolean'),
Expand Down
8 changes: 6 additions & 2 deletions coverage/control.py
Expand Up @@ -846,7 +846,8 @@ def _get_file_reporters(self, morfs=None):
def report(
self, morfs=None, show_missing=None, ignore_errors=None,
file=None, omit=None, include=None, skip_covered=None,
contexts=None, skip_empty=None, precision=None, sort=None
contexts=None, skip_empty=None, precision=None, sort=None,
always_total=None
):
"""Write a textual summary report to `file`.
Expand Down Expand Up @@ -891,13 +892,16 @@ def report(
.. versionadded:: 5.2
The `precision` parameter.
.. versionadded:: 5.4
The `always_total` parameter.
"""
with override_config(
self,
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
show_missing=show_missing, skip_covered=skip_covered,
report_contexts=contexts, skip_empty=skip_empty, precision=precision,
sort=sort
sort=sort, always_total=always_total
):
reporter = SummaryReporter(self)
return reporter.report(morfs, outfile=file)
Expand Down
5 changes: 3 additions & 2 deletions coverage/summary.py
Expand Up @@ -120,8 +120,9 @@ def report(self, morfs, outfile=None):
for line in lines:
self.writeout(line[0])

# Write a TOTAl line if we had more than one file.
if self.total.n_files > 1:
# Write a TOTAl line if we had more than one file,
# or if configured to always total
if self.total.n_files > 1 or self.config.always_total:
self.writeout(rule)
args = ("TOTAL", self.total.n_statements, self.total.n_missing)
if self.branches:
Expand Down
3 changes: 3 additions & 0 deletions doc/cmd.rst
Expand Up @@ -402,6 +402,9 @@ decimal point in coverage percentages, defaulting to none.

The ``--sort`` option is the name of a column to sort the report by.

The ``--always-total`` option will always output the TOTAL line, even when
only one file is measured.

Other common reporting options are described above in :ref:`cmd_reporting`.


Expand Down
5 changes: 5 additions & 0 deletions doc/config.rst
Expand Up @@ -264,6 +264,11 @@ See :ref:`cmd_combine` for more information.

Values common to many kinds of reporting.

.. _config_report_always_total:

``always_total`` (boolean, default False): always output the TOTAL line,
even when only one file is measured.

.. _config_report_exclude_lines:

``exclude_lines`` (multi-string): a list of regular expressions. Any line of
Expand Down
3 changes: 2 additions & 1 deletion doc/help/report.rst
Expand Up @@ -7,6 +7,8 @@
Report coverage statistics on modules.
Options:
--always-total Always output the TOTAL line, even when only one file
is measured.
--contexts=REGEX1,REGEX2,...
Only display data from lines covered in the given
contexts. Accepts Python regexes, which must be
Expand Down Expand Up @@ -35,4 +37,3 @@
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Expand Up @@ -40,7 +40,7 @@ class BaseCmdLineTest(CoverageTest):
skip_empty=None, precision=None,
)
_defaults.Coverage().report(
ignore_errors=None, include=None, omit=None, morfs=[],
always_total=None, ignore_errors=None, include=None, omit=None, morfs=[],
show_missing=None, skip_covered=None, contexts=None, skip_empty=None, precision=None,
sort=None,
)
Expand Down

0 comments on commit be612f9

Please sign in to comment.