Skip to content

Commit

Permalink
feat: --format=total writes just the total number
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 6, 2022
1 parent 30f1ecf commit e5a15c1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
10 changes: 7 additions & 3 deletions CHANGES.rst
Expand Up @@ -21,9 +21,13 @@ Unreleased
----------

- Text reporting with ``coverage report`` now has a ``--format=`` option.
The original style (``--format=text``) is the default. Now you can also
use ``--format=markdown`` to get the table in Markdown format, thanks to
Steve Oswald in `pull 1479`_, closing `issue 1418`_.
The original style (``--format=text``) is the default.

- Using ``--format=markdown`` will write the table in Markdown format, thanks
to Steve Oswald in `pull 1479`_, closing `issue 1418`_.

- Using ``--format=total`` will write a single total number to the
output. This can be useful for making badges or writing status updates.

- Fixed a mis-measurement of a strange use of wildcard alternatives in
match/case statements, closing `issue 1421`_.
Expand Down
2 changes: 1 addition & 1 deletion coverage/cmdline.py
Expand Up @@ -98,7 +98,7 @@ class Opts:
)
format = optparse.make_option(
'', '--format', action='store', metavar="FORMAT",
help="Output format, either text (default) or markdown",
help="Output format, either text (default), markdown, or total.",
)
help = optparse.make_option(
'-h', '--help', action='store_true',
Expand Down
2 changes: 1 addition & 1 deletion coverage/control.py
Expand Up @@ -924,7 +924,7 @@ def report(
`file` is a file-like object, suitable for writing.
`output_format` determines the format, either "text" (the default),
or "markdown".
"markdown", or "total".
`include` is a list of file name patterns. Files that match will be
included in the report. Files matching `omit` will not be included in
Expand Down
15 changes: 11 additions & 4 deletions coverage/summary.py
Expand Up @@ -19,6 +19,7 @@ def __init__(self, coverage):
self.config = self.coverage.config
self.branches = coverage.get_data().has_arcs()
self.outfile = None
self.output_format = self.config.format or "text"
self.fr_analysis = []
self.skipped_count = 0
self.empty_count = 0
Expand Down Expand Up @@ -159,6 +160,15 @@ def report(self, morfs, outfile=None):
if not self.total.n_files and not self.skipped_count:
raise NoDataError("No data to report.")

if self.output_format == "total":
self.write(self.total.pc_covered_str)
else:
self.tabular_report()

return self.total.n_statements and self.total.pc_covered

def tabular_report(self):
"""Writes tabular report formats."""
# Prepare the header line and column sorting.
header = ["Name", "Stmts", "Miss"]
if self.branches:
Expand Down Expand Up @@ -221,15 +231,12 @@ def report(self, morfs, outfile=None):
file_suffix = 's' if self.empty_count > 1 else ''
end_lines.append(f"\n{self.empty_count} empty file{file_suffix} skipped.")

text_format = self.config.format or "text"
if text_format == "markdown":
if self.output_format == "markdown":
formatter = self._report_markdown
else:
formatter = self._report_text
formatter(header, lines_values, total_line, end_lines)

return self.total.n_statements and self.total.pc_covered

def report_one_file(self, fr, analysis):
"""Report on just one file, the callback from report()."""
nums = analysis.numbers
Expand Down
11 changes: 7 additions & 4 deletions doc/cmd.rst
Expand Up @@ -518,7 +518,8 @@ as a percentage.
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--fail-under=MIN Exit with a status of 2 if the total coverage is less
than MIN.
--format=FORMAT Output format, either text (default) or markdown
--format=FORMAT Output format, either text (default), markdown, or
total.
-i, --ignore-errors Ignore errors while reading source files.
--include=PAT1,PAT2,...
Include only files whose paths match one of these
Expand All @@ -541,7 +542,7 @@ as a percentage.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 8c671de502a388159689082d906f786a)
.. [[[end]]] (checksum: 167272a29d9e7eb017a592a0e0747a06)
The ``-m`` flag also shows the line numbers of missing statements::

Expand Down Expand Up @@ -592,9 +593,11 @@ decimal point in coverage percentages, defaulting to none.

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

The ``--format`` option controls the style of the table. ``--format=text``
The ``--format`` option controls the style of the report. ``--format=text``
creates plain text tables as shown above. ``--format=markdown`` creates
Markdown tables.
Markdown tables. ``--format=total`` writes out a single number, the total
coverage percentage as shown at the end of the tables, but without a percent
sign.

Other common reporting options are described above in :ref:`cmd_reporting`.
These options can also be set in your .coveragerc file. See
Expand Down
10 changes: 9 additions & 1 deletion tests/test_summary.py
Expand Up @@ -439,6 +439,9 @@ def foo():
squeezed = self.squeezed_lines(report)
assert squeezed[4] == "1 file skipped due to complete coverage."

total = self.get_report(cov, output_format="total", skip_covered=True)
assert total == "100\n"

def test_report_skip_covered_longfilename(self):
self.make_file("long_______________filename.py", """
def foo():
Expand Down Expand Up @@ -829,7 +832,7 @@ def missing(x, y):
cov = coverage.Coverage(source=["."])
self.start_import_stop(cov, "mymissing")
assert self.stdout() == 'y\nz\n'
report = self.get_report(cov,squeeze=False, output_format="markdown", show_missing=True)
report = self.get_report(cov, squeeze=False, output_format="markdown", show_missing=True)

# | Name | Stmts | Miss | Cover | Missing |
# |------------- | -------: | -------: | ------: | --------: |
Expand All @@ -840,6 +843,11 @@ def missing(x, y):
assert report_lines[2] == "| mymissing.py | 14 | 3 | 79% | 3-4, 10 |"
assert report_lines[3] == "| **TOTAL** | **14** | **3** | **79%** | |"

assert self.get_report(cov, output_format="total") == "79\n"
assert self.get_report(cov, output_format="total", precision=2) == "78.57\n"
assert self.get_report(cov, output_format="total", precision=4) == "78.5714\n"


class ReportingReturnValueTest(CoverageTest):
"""Tests of reporting functions returning values."""

Expand Down

0 comments on commit e5a15c1

Please sign in to comment.