From 045a42045eb63f70516b6ac3b103fbdd9a9cfa37 Mon Sep 17 00:00:00 2001 From: Jerin Peter George Date: Mon, 29 Jun 2020 23:02:12 +0530 Subject: [PATCH 1/6] added descending sort option in coverage report --- coverage/summary.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/coverage/summary.py b/coverage/summary.py index 97d9fff07..b857fbf38 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -104,10 +104,21 @@ def report(self, morfs, outfile=None): # Sort the lines and write them out. if getattr(self.config, 'sort', None): - position = column_order.get(self.config.sort.lower()) + _sort = self.config.sort.lower() + if _sort[0] == '-': + reverse = True + sort_option = _sort[1:] + elif _sort[0] == '+': + reverse = False + sort_option = _sort[1:] + else: + reverse = False + sort_option = _sort + + position = column_order.get(sort_option) if position is None: raise CoverageException("Invalid sorting option: {!r}".format(self.config.sort)) - lines.sort(key=lambda l: (l[1][position], l[0])) + lines.sort(key=lambda l: (l[1][position], l[0]), reverse=reverse) for line in lines: self.writeout(line[0]) From 0228c1ace38d79b2e09d0394cc3af7f40a725af3 Mon Sep 17 00:00:00 2001 From: Jerin Peter George Date: Mon, 29 Jun 2020 23:51:27 +0530 Subject: [PATCH 2/6] commandline option for report sort added --- coverage/cmdline.py | 6 ++++++ coverage/config.py | 1 + coverage/control.py | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 62edc2074..1fbcf3a12 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -85,6 +85,10 @@ class Opts(object): "which isn't done by default." ), ) + sort = optparse.make_option( + '--sort', action='store', metavar='COLUMN', + help="Sort the report by the named column" + ) show_missing = optparse.make_option( '-m', '--show-missing', action='store_true', help="Show line numbers of statements in each module that weren't executed.", @@ -405,6 +409,7 @@ def get_prog_name(self): Opts.include, Opts.omit, Opts.precision, + Opts.sort, Opts.show_missing, Opts.skip_covered, Opts.skip_empty, @@ -579,6 +584,7 @@ def command_line(self, argv): omit=omit, include=include, contexts=contexts, + sort=options.sort ) # We need to be able to import from the current directory, because diff --git a/coverage/config.py b/coverage/config.py index 6d336d1f9..84d9758bf 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -211,6 +211,7 @@ def __init__(self): self.show_missing = False self.skip_covered = False self.skip_empty = False + self.sort = None # Defaults for [html] self.extra_css = None diff --git a/coverage/control.py b/coverage/control.py index 14c22eb1d..c505a8e45 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -831,7 +831,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, precision=None, + contexts=None, skip_empty=None, precision=None, sort=None ): """Write a textual summary report to `file`. @@ -882,6 +882,7 @@ def report( 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 ): reporter = SummaryReporter(self) return reporter.report(morfs, outfile=file) From cb90e3746d5f0b50cc477a221e657b6fcebe40d0 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 30 Jun 2020 07:02:27 -0400 Subject: [PATCH 3/6] Fix tests for pull #1005 --- coverage/cmdline.py | 3 ++- tests/test_cmdline.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 1fbcf3a12..0db4ea5ab 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -221,6 +221,7 @@ def __init__(self, *args, **kwargs): skip_covered=None, skip_empty=None, show_contexts=None, + sort=None, source=None, timid=None, title=None, @@ -584,7 +585,6 @@ def command_line(self, argv): omit=omit, include=include, contexts=contexts, - sort=options.sort ) # We need to be able to import from the current directory, because @@ -600,6 +600,7 @@ def command_line(self, argv): skip_covered=options.skip_covered, skip_empty=options.skip_empty, precision=options.precision, + sort=options.sort, **report_args ) elif options.action == "annotate": diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 2406d93d3..e50a4fa85 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -42,6 +42,7 @@ class BaseCmdLineTest(CoverageTest): _defaults.Coverage().report( ignore_errors=None, include=None, omit=None, morfs=[], show_missing=None, skip_covered=None, contexts=None, skip_empty=None, precision=None, + sort=None, ) _defaults.Coverage().xml_report( ignore_errors=None, include=None, omit=None, morfs=[], outfile=None, From ca2f30ef894b85f63c3d79c5d477f06d4f4d9a58 Mon Sep 17 00:00:00 2001 From: Jerin Peter George Date: Tue, 30 Jun 2020 22:23:15 +0530 Subject: [PATCH 4/6] conditional statements improved --- coverage/summary.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/coverage/summary.py b/coverage/summary.py index b857fbf38..986cd2f2d 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -104,16 +104,13 @@ def report(self, morfs, outfile=None): # Sort the lines and write them out. if getattr(self.config, 'sort', None): - _sort = self.config.sort.lower() - if _sort[0] == '-': + sort_option = self.config.sort.lower() + reverse = False + if sort_option[0] == '-': reverse = True - sort_option = _sort[1:] - elif _sort[0] == '+': - reverse = False - sort_option = _sort[1:] - else: - reverse = False - sort_option = _sort + sort_option = sort_option[1:] + elif sort_option[0] == '+': + sort_option = sort_option[1:] position = column_order.get(sort_option) if position is None: From ebb40988aa2652b5067bc25137d80c5b147f7760 Mon Sep 17 00:00:00 2001 From: Jerin Peter George Date: Tue, 30 Jun 2020 22:33:21 +0530 Subject: [PATCH 5/6] sort option help updated with choices --- coverage/cmdline.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 0db4ea5ab..3846705c4 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -87,7 +87,8 @@ class Opts(object): ) sort = optparse.make_option( '--sort', action='store', metavar='COLUMN', - help="Sort the report by the named column" + help="Sort the report by the named column: name, stmts, miss, branch, brpart, or cover. " + "Default is name." ) show_missing = optparse.make_option( '-m', '--show-missing', action='store_true', From 2ec292dd7182a31ee213d481bc61eaa658be8c38 Mon Sep 17 00:00:00 2001 From: Jerin Peter George Date: Fri, 3 Jul 2020 21:53:07 +0530 Subject: [PATCH 6/6] commandline test for sort added --- tests/test_cmdline.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index e50a4fa85..706aea311 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -393,6 +393,11 @@ def test_report(self): cov.load() cov.report(contexts=["foo", "bar"]) """) + self.cmd_executes("report --sort=-foo", """\ + cov = Coverage() + cov.load() + cov.report(sort='-foo') + """) def test_run(self): # coverage run [-p] [-L] [--timid] MODULE.py [ARG1 ARG2 ...]