Skip to content

Report descending sort option #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions coverage/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down