diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-10-25 17:40:09 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-10-25 17:40:09 -0400 |
commit | c370442bea855f0f87217db190760629fe559150 (patch) | |
tree | 3fc7a69e49849c5d95f5ee3c3163cdede38cec45 /coverage/summary.py | |
parent | 2825bdb540f1b14ae9855e57ecb0b1e4d1098894 (diff) | |
download | python-coveragepy-c370442bea855f0f87217db190760629fe559150.tar.gz |
Properly handle filenames with non-ASCII characters. #432
Diffstat (limited to 'coverage/summary.py')
-rw-r--r-- | coverage/summary.py | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/coverage/summary.py b/coverage/summary.py index 4dcaa73..f797e30 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -5,6 +5,7 @@ import sys +from coverage import env from coverage.report import Reporter from coverage.results import Numbers from coverage.misc import NotPython, CoverageException @@ -20,38 +21,45 @@ class SummaryReporter(Reporter): def report(self, morfs, outfile=None): """Writes a report summarizing coverage statistics per module. - `outfile` is a file object to write the summary to. + `outfile` is a file object to write the summary to. It must be opened + for native strings (bytes on Python 2, Unicode on Python 3). """ self.find_file_reporters(morfs) # Prepare the formatting strings max_name = max([len(fr.relative_filename()) for fr in self.file_reporters] + [5]) - fmt_name = "%%- %ds " % max_name - fmt_err = "%s %s: %s\n" - fmt_skip_covered = "\n%s file%s skipped due to complete coverage.\n" + fmt_name = u"%%- %ds " % max_name + fmt_err = u"%s %s: %s\n" + fmt_skip_covered = u"\n%s file%s skipped due to complete coverage.\n" - header = (fmt_name % "Name") + " Stmts Miss" - fmt_coverage = fmt_name + "%6d %6d" + header = (fmt_name % "Name") + u" Stmts Miss" + fmt_coverage = fmt_name + u"%6d %6d" if self.branches: - header += " Branch BrPart" - fmt_coverage += " %6d %6d" + header += u" Branch BrPart" + fmt_coverage += u" %6d %6d" width100 = Numbers.pc_str_width() - header += "%*s" % (width100+4, "Cover") - fmt_coverage += "%%%ds%%%%" % (width100+3,) + header += u"%*s" % (width100+4, "Cover") + fmt_coverage += u"%%%ds%%%%" % (width100+3,) if self.config.show_missing: - header += " Missing" - fmt_coverage += " %s" - rule = "-" * len(header) + "\n" - header += "\n" - fmt_coverage += "\n" + header += u" Missing" + fmt_coverage += u" %s" + rule = u"-" * len(header) + u"\n" + header += u"\n" + fmt_coverage += u"\n" - if not outfile: + if outfile is None: outfile = sys.stdout + if env.PY2: + encoding = getattr(outfile, "encoding", None) or sys.getfilesystemencoding() + writeout = lambda u: outfile.write(u.encode(encoding)) + else: + writeout = outfile.write + # Write the header - outfile.write(header) - outfile.write(rule) + writeout(header) + writeout(rule) total = Numbers() skipped_count = 0 @@ -83,7 +91,7 @@ class SummaryReporter(Reporter): missing_fmtd += ", " missing_fmtd += branches_fmtd args += (missing_fmtd,) - outfile.write(fmt_coverage % args) + writeout(fmt_coverage % args) except Exception: report_it = not self.config.ignore_errors if report_it: @@ -93,22 +101,22 @@ class SummaryReporter(Reporter): if typ is NotPython and not fr.should_be_python(): report_it = False if report_it: - outfile.write(fmt_err % (fr.relative_filename(), typ.__name__, msg)) + writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) if total.n_files > 1: - outfile.write(rule) + writeout(rule) args = ("TOTAL", total.n_statements, total.n_missing) if self.branches: args += (total.n_branches, total.n_partial_branches) args += (total.pc_covered_str,) if self.config.show_missing: args += ("",) - outfile.write(fmt_coverage % args) + writeout(fmt_coverage % args) if not total.n_files and not skipped_count: raise CoverageException("No data to report.") if self.config.skip_covered and skipped_count: - outfile.write(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) + writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) return total.n_statements and total.pc_covered |