Skip to content

Commit

Permalink
Merge branch 'pr/982'
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 12, 2020
2 parents d7120ba + dc48c72 commit 916a0c9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ want to know what's different in 5.0 since 4.5.x, see :ref:`whatsnew5x`.
Unreleased
----------

Nothing yet.
- The ``coverage report`` and ``coverage html`` commands now accept a
``--precision`` option to control the number of decimal points displayed.
Thanks, Teake Nutma.


.. _changes_51:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Stephen Finucane
Steve Leonard
Steve Peak
S. Y. Lee
Teake Nutma
Ted Wexler
Thijs Triemstra
Titus Brown
Expand Down
12 changes: 12 additions & 0 deletions coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ class Opts(object):
"to be run as 'python -m' would run it."
),
)
precision = optparse.make_option(
'', '--precision', action='store', metavar='N', type=int,
help=(
"Number of digits after the decimal point to display for "
"reported coverage percentages."
),
)
rcfile = optparse.make_option(
'', '--rcfile', action='store',
help=(
Expand Down Expand Up @@ -203,6 +210,7 @@ def __init__(self, *args, **kwargs):
omit=None,
contexts=None,
parallel_mode=None,
precision=None,
pylib=None,
rcfile=True,
show_missing=None,
Expand Down Expand Up @@ -358,6 +366,7 @@ def get_prog_name(self):
Opts.ignore_errors,
Opts.include,
Opts.omit,
Opts.precision,
Opts.show_contexts,
Opts.skip_covered,
Opts.skip_empty,
Expand Down Expand Up @@ -395,6 +404,7 @@ def get_prog_name(self):
Opts.ignore_errors,
Opts.include,
Opts.omit,
Opts.precision,
Opts.show_missing,
Opts.skip_covered,
Opts.skip_empty,
Expand Down Expand Up @@ -583,6 +593,7 @@ def command_line(self, argv):
show_missing=options.show_missing,
skip_covered=options.skip_covered,
skip_empty=options.skip_empty,
precision=options.precision,
**report_args
)
elif options.action == "annotate":
Expand All @@ -594,6 +605,7 @@ def command_line(self, argv):
skip_covered=options.skip_covered,
skip_empty=options.skip_empty,
show_contexts=options.show_contexts,
precision=options.precision,
**report_args
)
elif options.action == "xml":
Expand Down
22 changes: 15 additions & 7 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ 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,
contexts=None, skip_empty=None, precision=None,
):
"""Write a textual summary report to `file`.
Expand Down Expand Up @@ -857,6 +857,9 @@ def report(
expressions (using :func:`re.search <python:re.search>`) will be
included in the report.
`precision` is the number of digits to display after the decimal
point for percentages.
All of the arguments default to the settings read from the
:ref:`configuration file <config>`.
Expand All @@ -868,12 +871,15 @@ def report(
.. versionadded:: 5.0
The `contexts` and `skip_empty` parameters.
.. versionadded:: 5.2
The `precision` 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,
report_contexts=contexts, skip_empty=skip_empty, precision=precision,
):
reporter = SummaryReporter(self)
return reporter.report(morfs, outfile=file)
Expand All @@ -899,10 +905,12 @@ def annotate(
reporter = AnnotateReporter(self)
reporter.report(morfs, directory=directory)

def html_report(self, morfs=None, directory=None, ignore_errors=None,
omit=None, include=None, extra_css=None, title=None,
skip_covered=None, show_contexts=None, contexts=None,
skip_empty=None):
def html_report(
self, morfs=None, directory=None, ignore_errors=None,
omit=None, include=None, extra_css=None, title=None,
skip_covered=None, show_contexts=None, contexts=None,
skip_empty=None, precision=None,
):
"""Generate an HTML report.
The HTML is written to `directory`. The file "index.html" is the
Expand Down Expand Up @@ -930,7 +938,7 @@ def html_report(self, morfs=None, directory=None, ignore_errors=None,
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
html_dir=directory, extra_css=extra_css, html_title=title,
skip_covered=skip_covered, show_contexts=show_contexts, report_contexts=contexts,
skip_empty=skip_empty,
skip_empty=skip_empty, precision=precision,
):
reporter = HtmlReporter(self)
return reporter.report(morfs)
Expand Down
6 changes: 6 additions & 0 deletions doc/cmd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ If you have :ref:`recorded contexts <contexts>`, the ``--contexts`` option lets
you choose which contexts to report on. See :ref:`context_reporting` for
details.

The ``--precision`` option controls the number of digits displayed after the
decimal point in coverage percentages, defaulting to none.

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


Expand Down Expand Up @@ -418,6 +421,9 @@ The ``--skip-covered`` switch will skip any file with 100% coverage, letting
you focus on the files that still need attention. The ``--skip-empty`` switch
will skip any file with no executable statements.

The ``--precision`` option controls the number of digits displayed after the
decimal point in coverage percentages, defaulting to none.

If you have :ref:`recorded contexts <contexts>`, the ``--contexts`` option lets
you choose which contexts to report on, and the ``--show-contexts`` option will
annotate lines with the contexts that ran them. See :ref:`context_reporting`
Expand Down
16 changes: 13 additions & 3 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class BaseCmdLineTest(CoverageTest):
_defaults.Coverage().html_report(
directory=None, ignore_errors=None, include=None, omit=None, morfs=[],
skip_covered=None, show_contexts=None, title=None, contexts=None,
skip_empty=None,
skip_empty=None, precision=None,
)
_defaults.Coverage().report(
ignore_errors=None, include=None, omit=None, morfs=[],
show_missing=None, skip_covered=None, contexts=None, skip_empty=None,
show_missing=None, skip_covered=None, contexts=None, skip_empty=None, precision=None,
)
_defaults.Coverage().xml_report(
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
contexts=None,
)
_defaults.Coverage().json_report(
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
contexts=None, pretty_print=None, show_contexts=None
contexts=None, pretty_print=None, show_contexts=None,
)
_defaults.Coverage(
cover_pylib=None, data_suffix=None, timid=None, branch=None,
Expand Down Expand Up @@ -324,6 +324,11 @@ def test_html(self):
cov.load()
cov.html_report(morfs=["mod1", "mod2", "mod3"])
""")
self.cmd_executes("html --precision=3", """\
cov = Coverage()
cov.load()
cov.html_report(precision=3)
""")
self.cmd_executes("html --title=Hello_there", """\
cov = Coverage()
cov.load()
Expand Down Expand Up @@ -367,6 +372,11 @@ def test_report(self):
cov.load()
cov.report(morfs=["mod1", "mod2", "mod3"])
""")
self.cmd_executes("report --precision=7", """\
cov = Coverage()
cov.load()
cov.report(precision=7)
""")
self.cmd_executes("report --skip-covered", """\
cov = Coverage()
cov.load()
Expand Down

0 comments on commit 916a0c9

Please sign in to comment.