3 |
3 |
4 """Summary reporting""" |
4 """Summary reporting""" |
5 |
5 |
6 import sys |
6 import sys |
7 |
7 |
|
8 from coverage import env |
8 from coverage.report import Reporter |
9 from coverage.report import Reporter |
9 from coverage.results import Numbers |
10 from coverage.results import Numbers |
10 from coverage.misc import NotPython, CoverageException |
11 from coverage.misc import NotPython, CoverageException, output_encoding |
11 |
12 |
12 |
13 |
13 class SummaryReporter(Reporter): |
14 class SummaryReporter(Reporter): |
14 """A reporter for writing the summary report.""" |
15 """A reporter for writing the summary report.""" |
15 |
16 |
18 self.branches = coverage.data.has_arcs() |
19 self.branches = coverage.data.has_arcs() |
19 |
20 |
20 def report(self, morfs, outfile=None): |
21 def report(self, morfs, outfile=None): |
21 """Writes a report summarizing coverage statistics per module. |
22 """Writes a report summarizing coverage statistics per module. |
22 |
23 |
23 `outfile` is a file object to write the summary to. |
24 `outfile` is a file object to write the summary to. It must be opened |
|
25 for native strings (bytes on Python 2, Unicode on Python 3). |
24 |
26 |
25 """ |
27 """ |
26 self.find_file_reporters(morfs) |
28 file_reporters = self.find_file_reporters(morfs) |
27 |
29 |
28 # Prepare the formatting strings |
30 # Prepare the formatting strings |
29 max_name = max([len(fr.relative_filename()) for fr in self.file_reporters] + [5]) |
31 max_name = max([len(fr.relative_filename()) for fr in file_reporters] + [5]) |
30 fmt_name = "%%- %ds " % max_name |
32 fmt_name = u"%%- %ds " % max_name |
31 fmt_err = "%s %s: %s\n" |
33 fmt_err = u"%s %s: %s" |
32 fmt_skip_covered = "\n%s file%s skipped due to complete coverage.\n" |
34 fmt_skip_covered = u"\n%s file%s skipped due to complete coverage." |
33 |
35 |
34 header = (fmt_name % "Name") + " Stmts Miss" |
36 header = (fmt_name % "Name") + u" Stmts Miss" |
35 fmt_coverage = fmt_name + "%6d %6d" |
37 fmt_coverage = fmt_name + u"%6d %6d" |
36 if self.branches: |
38 if self.branches: |
37 header += " Branch BrPart" |
39 header += u" Branch BrPart" |
38 fmt_coverage += " %6d %6d" |
40 fmt_coverage += u" %6d %6d" |
39 width100 = Numbers.pc_str_width() |
41 width100 = Numbers.pc_str_width() |
40 header += "%*s" % (width100+4, "Cover") |
42 header += u"%*s" % (width100+4, "Cover") |
41 fmt_coverage += "%%%ds%%%%" % (width100+3,) |
43 fmt_coverage += u"%%%ds%%%%" % (width100+3,) |
42 if self.config.show_missing: |
44 if self.config.show_missing: |
43 header += " Missing" |
45 header += u" Missing" |
44 fmt_coverage += " %s" |
46 fmt_coverage += u" %s" |
45 rule = "-" * len(header) + "\n" |
47 rule = u"-" * len(header) |
46 header += "\n" |
|
47 fmt_coverage += "\n" |
|
48 |
48 |
49 if not outfile: |
49 if outfile is None: |
50 outfile = sys.stdout |
50 outfile = sys.stdout |
51 |
51 |
|
52 def writeout(line): |
|
53 """Write a line to the output, adding a newline.""" |
|
54 if env.PY2: |
|
55 line = line.encode(output_encoding()) |
|
56 outfile.write(line.rstrip()) |
|
57 outfile.write("\n") |
|
58 |
52 # Write the header |
59 # Write the header |
53 outfile.write(header) |
60 writeout(header) |
54 outfile.write(rule) |
61 writeout(rule) |
55 |
62 |
56 total = Numbers() |
63 total = Numbers() |
57 skipped_count = 0 |
64 skipped_count = 0 |
58 |
65 |
59 for fr in self.file_reporters: |
66 for fr in file_reporters: |
60 try: |
67 try: |
61 analysis = self.coverage._analyze(fr) |
68 analysis = self.coverage._analyze(fr) |
62 nums = analysis.numbers |
69 nums = analysis.numbers |
|
70 total += nums |
63 |
71 |
64 if self.config.skip_covered: |
72 if self.config.skip_covered: |
65 # Don't report on 100% files. |
73 # Don't report on 100% files. |
66 no_missing_lines = (nums.n_missing == 0) |
74 no_missing_lines = (nums.n_missing == 0) |
67 no_missing_branches = (nums.n_partial_branches == 0) |
75 no_missing_branches = (nums.n_partial_branches == 0) |
80 if branches_fmtd: |
88 if branches_fmtd: |
81 if missing_fmtd: |
89 if missing_fmtd: |
82 missing_fmtd += ", " |
90 missing_fmtd += ", " |
83 missing_fmtd += branches_fmtd |
91 missing_fmtd += branches_fmtd |
84 args += (missing_fmtd,) |
92 args += (missing_fmtd,) |
85 outfile.write(fmt_coverage % args) |
93 writeout(fmt_coverage % args) |
86 total += nums |
|
87 except Exception: |
94 except Exception: |
88 report_it = not self.config.ignore_errors |
95 report_it = not self.config.ignore_errors |
89 if report_it: |
96 if report_it: |
90 typ, msg = sys.exc_info()[:2] |
97 typ, msg = sys.exc_info()[:2] |
91 # NotPython is only raised by PythonFileReporter, which has a |
98 # NotPython is only raised by PythonFileReporter, which has a |
92 # should_be_python() method. |
99 # should_be_python() method. |
93 if typ is NotPython and not fr.should_be_python(): |
100 if typ is NotPython and not fr.should_be_python(): |
94 report_it = False |
101 report_it = False |
95 if report_it: |
102 if report_it: |
96 outfile.write(fmt_err % (fr.relative_filename(), typ.__name__, msg)) |
103 writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) |
97 |
104 |
98 if total.n_files > 1: |
105 if total.n_files > 1: |
99 outfile.write(rule) |
106 writeout(rule) |
100 args = ("TOTAL", total.n_statements, total.n_missing) |
107 args = ("TOTAL", total.n_statements, total.n_missing) |
101 if self.branches: |
108 if self.branches: |
102 args += (total.n_branches, total.n_partial_branches) |
109 args += (total.n_branches, total.n_partial_branches) |
103 args += (total.pc_covered_str,) |
110 args += (total.pc_covered_str,) |
104 if self.config.show_missing: |
111 if self.config.show_missing: |
105 args += ("",) |
112 args += ("",) |
106 outfile.write(fmt_coverage % args) |
113 writeout(fmt_coverage % args) |
107 |
114 |
108 if not total.n_files and not skipped_count: |
115 if not total.n_files and not skipped_count: |
109 raise CoverageException("No data to report.") |
116 raise CoverageException("No data to report.") |
110 |
117 |
111 if self.config.skip_covered and skipped_count: |
118 if self.config.skip_covered and skipped_count: |
112 outfile.write(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) |
119 writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) |
113 |
120 |
114 return total.n_statements and total.pc_covered |
121 return total.n_statements and total.pc_covered |