Skip to content
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

Report descending sort option #1005

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions coverage/cmdline.py
Expand Up @@ -85,6 +85,11 @@ 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: name, stmts, miss, branch, brpart, or cover. "
"Default is name."
)
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 @@ -217,6 +222,7 @@ def __init__(self, *args, **kwargs):
skip_covered=None,
skip_empty=None,
show_contexts=None,
sort=None,
source=None,
timid=None,
title=None,
Expand Down Expand Up @@ -405,6 +411,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 @@ -594,6 +601,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":
Expand Down
1 change: 1 addition & 0 deletions coverage/config.py
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
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
12 changes: 10 additions & 2 deletions coverage/summary.py
Expand Up @@ -104,10 +104,18 @@ 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_option = self.config.sort.lower()
reverse = False
if sort_option[0] == '-':
reverse = True
sort_option = sort_option[1:]
elif sort_option[0] == '+':
sort_option = sort_option[1:]

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
6 changes: 6 additions & 0 deletions tests/test_cmdline.py
Expand Up @@ -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,
Expand Down Expand Up @@ -392,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 ...]
Expand Down