--- a/DebugClients/Python/coverage/summary.py Sat Apr 07 13:17:06 2018 +0200 +++ b/DebugClients/Python/coverage/summary.py Sat Apr 07 13:35:10 2018 +0200 @@ -8,7 +8,7 @@ from coverage import env from coverage.report import Reporter from coverage.results import Numbers -from coverage.misc import NotPython, CoverageException, output_encoding +from coverage.misc import NotPython, CoverageException, output_encoding, StopEverything class SummaryReporter(Reporter): @@ -25,12 +25,53 @@ for native strings (bytes on Python 2, Unicode on Python 3). """ - file_reporters = self.find_file_reporters(morfs) + if outfile is None: + outfile = sys.stdout + + def writeout(line): + """Write a line to the output, adding a newline.""" + if env.PY2: + line = line.encode(output_encoding()) + outfile.write(line.rstrip()) + outfile.write("\n") + + fr_analysis = [] + skipped_count = 0 + total = Numbers() + + fmt_err = u"%s %s: %s" + + for fr in self.find_file_reporters(morfs): + try: + analysis = self.coverage._analyze(fr) + nums = analysis.numbers + total += nums - # Prepare the formatting strings - max_name = max([len(fr.relative_filename()) for fr in file_reporters] + [5]) + if self.config.skip_covered: + # Don't report on 100% files. + no_missing_lines = (nums.n_missing == 0) + no_missing_branches = (nums.n_partial_branches == 0) + if no_missing_lines and no_missing_branches: + skipped_count += 1 + continue + fr_analysis.append((fr, analysis)) + except StopEverything: + # Don't report this on single files, it's a systemic problem. + raise + except Exception: + report_it = not self.config.ignore_errors + if report_it: + typ, msg = sys.exc_info()[:2] + # NotPython is only raised by PythonFileReporter, which has a + # should_be_python() method. + if issubclass(typ, NotPython) and not fr.should_be_python(): + report_it = False + if report_it: + writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) + + # Prepare the formatting strings, header, and column sorting. + max_name = max([len(fr.relative_filename()) for (fr, analysis) in fr_analysis] + [5]) fmt_name = u"%%- %ds " % max_name - fmt_err = u"%s %s: %s" fmt_skip_covered = u"\n%s file%s skipped due to complete coverage." header = (fmt_name % "Name") + u" Stmts Miss" @@ -46,36 +87,22 @@ fmt_coverage += u" %s" rule = u"-" * len(header) - if outfile is None: - outfile = sys.stdout - - def writeout(line): - """Write a line to the output, adding a newline.""" - if env.PY2: - line = line.encode(output_encoding()) - outfile.write(line.rstrip()) - outfile.write("\n") + column_order = dict(name=0, stmts=1, miss=2, cover=-1) + if self.branches: + column_order.update(dict(branch=3, brpart=4)) # Write the header writeout(header) writeout(rule) - total = Numbers() - skipped_count = 0 + # `lines` is a list of pairs, (line text, line values). The line text + # is a string that will be printed, and line values is a tuple of + # sortable values. + lines = [] - for fr in file_reporters: + for (fr, analysis) in fr_analysis: try: - analysis = self.coverage._analyze(fr) nums = analysis.numbers - total += nums - - if self.config.skip_covered: - # Don't report on 100% files. - no_missing_lines = (nums.n_missing == 0) - no_missing_branches = (nums.n_partial_branches == 0) - if no_missing_lines and no_missing_branches: - skipped_count += 1 - continue args = (fr.relative_filename(), nums.n_statements, nums.n_missing) if self.branches: @@ -90,7 +117,10 @@ missing_fmtd += ", " missing_fmtd += branches_fmtd args += (missing_fmtd,) - writeout(fmt_coverage % args) + text = fmt_coverage % args + # Add numeric percent coverage so that sorting makes sense. + args += (nums.pc_covered,) + lines.append((text, args)) except Exception: report_it = not self.config.ignore_errors if report_it: @@ -102,6 +132,17 @@ if report_it: writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) + # Sort the lines and write them out. + if getattr(self.config, 'sort', None): + position = column_order.get(self.config.sort.lower()) + if position is None: + raise CoverageException("Invalid sorting option: {0!r}".format(self.config.sort)) + lines.sort(key=lambda l: (l[1][position], l[0])) + + for line in lines: + writeout(line[0]) + + # Write a TOTAl line if we had more than one file. if total.n_files > 1: writeout(rule) args = ("TOTAL", total.n_statements, total.n_missing) @@ -112,6 +153,7 @@ args += ("",) writeout(fmt_coverage % args) + # Write other final lines. if not total.n_files and not skipped_count: raise CoverageException("No data to report.")