From be612f9a70898b47e10d24db49f3406cf0618a6d Mon Sep 17 00:00:00 2001 From: Judson Neer Date: Sun, 3 Jan 2021 18:50:12 -0800 Subject: [PATCH] Add --always-total option. #1086. --- CHANGES.rst | 3 ++- CONTRIBUTORS.txt | 1 + coverage/cmdline.py | 7 +++++++ coverage/config.py | 2 ++ coverage/control.py | 8 ++++++-- coverage/summary.py | 5 +++-- doc/cmd.rst | 3 +++ doc/config.rst | 5 +++++ doc/help/report.rst | 3 ++- tests/test_cmdline.py | 2 +- 10 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 2f6214c43..481551026 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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: diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 3e52e45e9..44b4f557d 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -79,6 +79,7 @@ Jon Chappell Jon Dufresne Joseph Tate Josh Williams +Judson Neer Julian Berman Julien Voisin Justas Sadzevičius diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 9c9ae868a..8c4a3c467 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -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.", @@ -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, @@ -413,6 +418,7 @@ def get_prog_name(self): 'report': CmdOptionParser( "report", [ + Opts.always_total, Opts.contexts, Opts.fail_under, Opts.ignore_errors, @@ -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, diff --git a/coverage/config.py b/coverage/config.py index 2af4a1cc8..584166229 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -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 @@ -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'), diff --git a/coverage/control.py b/coverage/control.py index 086490730..76920bf00 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -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`. @@ -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) diff --git a/coverage/summary.py b/coverage/summary.py index 986cd2f2d..b0490abc6 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -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: diff --git a/doc/cmd.rst b/doc/cmd.rst index f6087fecf..1f117b849 100644 --- a/doc/cmd.rst +++ b/doc/cmd.rst @@ -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`. diff --git a/doc/config.rst b/doc/config.rst index c6cb94dd5..4002d002a 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -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 diff --git a/doc/help/report.rst b/doc/help/report.rst index 3408f2bb4..bef822c4b 100644 --- a/doc/help/report.rst +++ b/doc/help/report.rst @@ -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 @@ -35,4 +37,3 @@ --rcfile=RCFILE Specify configuration file. By default '.coveragerc', 'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried. [env: COVERAGE_RCFILE] - diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 374adb0de..160b7cce7 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -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, )