diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2020-05-12 06:58:04 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2020-05-12 06:58:04 -0400 |
commit | 916a0c9278d83ad0fafc75fcdc80e03db8c3f257 (patch) | |
tree | de12e34098d991a9e8a347fcb405253ee7193cc2 | |
parent | d7120bab764f0cb817b8846aa33504da66d382a2 (diff) | |
parent | dc48c725fe4a95b64939f67cbb97d259ba47fe9f (diff) | |
download | python-coveragepy-git-916a0c9278d83ad0fafc75fcdc80e03db8c3f257.tar.gz |
Merge branch 'pr/982'
-rw-r--r-- | CHANGES.rst | 4 | ||||
-rw-r--r-- | CONTRIBUTORS.txt | 1 | ||||
-rw-r--r-- | coverage/cmdline.py | 12 | ||||
-rw-r--r-- | coverage/control.py | 22 | ||||
-rw-r--r-- | doc/cmd.rst | 6 | ||||
-rw-r--r-- | tests/test_cmdline.py | 16 |
6 files changed, 50 insertions, 11 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index b63ad42b..49016e9f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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: diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index a3cc9be7..82ec3251 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -127,6 +127,7 @@ Stephen Finucane Steve Leonard Steve Peak S. Y. Lee +Teake Nutma Ted Wexler Thijs Triemstra Titus Brown diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 9fddb6bb..08e116b6 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -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=( @@ -203,6 +210,7 @@ class CoverageOptionParser(optparse.OptionParser, object): omit=None, contexts=None, parallel_mode=None, + precision=None, pylib=None, rcfile=True, show_missing=None, @@ -358,6 +366,7 @@ CMDS = { Opts.ignore_errors, Opts.include, Opts.omit, + Opts.precision, Opts.show_contexts, Opts.skip_covered, Opts.skip_empty, @@ -395,6 +404,7 @@ CMDS = { Opts.ignore_errors, Opts.include, Opts.omit, + Opts.precision, Opts.show_missing, Opts.skip_covered, Opts.skip_empty, @@ -583,6 +593,7 @@ class CoverageScript(object): show_missing=options.show_missing, skip_covered=options.skip_covered, skip_empty=options.skip_empty, + precision=options.precision, **report_args ) elif options.action == "annotate": @@ -594,6 +605,7 @@ class CoverageScript(object): skip_covered=options.skip_covered, skip_empty=options.skip_empty, show_contexts=options.show_contexts, + precision=options.precision, **report_args ) elif options.action == "xml": diff --git a/coverage/control.py b/coverage/control.py index 2b8c3d26..c2f40e70 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -829,7 +829,7 @@ class Coverage(object): 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`. @@ -857,6 +857,9 @@ class Coverage(object): 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>`. @@ -868,12 +871,15 @@ class Coverage(object): .. 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) @@ -899,10 +905,12 @@ class Coverage(object): 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 @@ -930,7 +938,7 @@ class Coverage(object): 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) diff --git a/doc/cmd.rst b/doc/cmd.rst index cbbb26bb..31ae6e01 100644 --- a/doc/cmd.rst +++ b/doc/cmd.rst @@ -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`. @@ -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` diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index f5e8e96a..742df18b 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -37,11 +37,11 @@ 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, @@ -49,7 +49,7 @@ class BaseCmdLineTest(CoverageTest): ) _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, @@ -324,6 +324,11 @@ class CmdLineTest(BaseCmdLineTest): 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() @@ -367,6 +372,11 @@ class CmdLineTest(BaseCmdLineTest): 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() |