|
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
|
2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt |
|
3 |
|
4 """Summary reporting""" |
|
5 |
|
6 import sys |
|
7 |
|
8 from coverage.exceptions import ConfigError, NoDataError |
|
9 from coverage.misc import human_sorted_items |
|
10 from coverage.report import get_analysis_to_report |
|
11 from coverage.results import Numbers |
|
12 |
|
13 |
|
14 class SummaryReporter: |
|
15 """A reporter for writing the summary report.""" |
|
16 |
|
17 def __init__(self, coverage): |
|
18 self.coverage = coverage |
|
19 self.config = self.coverage.config |
|
20 self.branches = coverage.get_data().has_arcs() |
|
21 self.outfile = None |
|
22 self.fr_analysis = [] |
|
23 self.skipped_count = 0 |
|
24 self.empty_count = 0 |
|
25 self.total = Numbers(precision=self.config.precision) |
|
26 self.fmt_err = "%s %s: %s" |
|
27 |
|
28 def writeout(self, line): |
|
29 """Write a line to the output, adding a newline.""" |
|
30 self.outfile.write(line.rstrip()) |
|
31 self.outfile.write("\n") |
|
32 |
|
33 def report(self, morfs, outfile=None): |
|
34 """Writes a report summarizing coverage statistics per module. |
|
35 |
|
36 `outfile` is a file object to write the summary to. It must be opened |
|
37 for native strings (bytes on Python 2, Unicode on Python 3). |
|
38 |
|
39 """ |
|
40 self.outfile = outfile or sys.stdout |
|
41 |
|
42 self.coverage.get_data().set_query_contexts(self.config.report_contexts) |
|
43 for fr, analysis in get_analysis_to_report(self.coverage, morfs): |
|
44 self.report_one_file(fr, analysis) |
|
45 |
|
46 # Prepare the formatting strings, header, and column sorting. |
|
47 max_name = max([len(fr.relative_filename()) for (fr, analysis) in self.fr_analysis] + [5]) |
|
48 fmt_name = "%%- %ds " % max_name |
|
49 fmt_skip_covered = "\n%s file%s skipped due to complete coverage." |
|
50 fmt_skip_empty = "\n%s empty file%s skipped." |
|
51 |
|
52 header = (fmt_name % "Name") + " Stmts Miss" |
|
53 fmt_coverage = fmt_name + "%6d %6d" |
|
54 if self.branches: |
|
55 header += " Branch BrPart" |
|
56 fmt_coverage += " %6d %6d" |
|
57 width100 = Numbers(precision=self.config.precision).pc_str_width() |
|
58 header += "%*s" % (width100+4, "Cover") |
|
59 fmt_coverage += "%%%ds%%%%" % (width100+3,) |
|
60 if self.config.show_missing: |
|
61 header += " Missing" |
|
62 fmt_coverage += " %s" |
|
63 rule = "-" * len(header) |
|
64 |
|
65 column_order = dict(name=0, stmts=1, miss=2, cover=-1) |
|
66 if self.branches: |
|
67 column_order.update(dict(branch=3, brpart=4)) |
|
68 |
|
69 # Write the header |
|
70 self.writeout(header) |
|
71 self.writeout(rule) |
|
72 |
|
73 # `lines` is a list of pairs, (line text, line values). The line text |
|
74 # is a string that will be printed, and line values is a tuple of |
|
75 # sortable values. |
|
76 lines = [] |
|
77 |
|
78 for (fr, analysis) in self.fr_analysis: |
|
79 nums = analysis.numbers |
|
80 |
|
81 args = (fr.relative_filename(), nums.n_statements, nums.n_missing) |
|
82 if self.branches: |
|
83 args += (nums.n_branches, nums.n_partial_branches) |
|
84 args += (nums.pc_covered_str,) |
|
85 if self.config.show_missing: |
|
86 args += (analysis.missing_formatted(branches=True),) |
|
87 text = fmt_coverage % args |
|
88 # Add numeric percent coverage so that sorting makes sense. |
|
89 args += (nums.pc_covered,) |
|
90 lines.append((text, args)) |
|
91 |
|
92 # Sort the lines and write them out. |
|
93 sort_option = (self.config.sort or "name").lower() |
|
94 reverse = False |
|
95 if sort_option[0] == '-': |
|
96 reverse = True |
|
97 sort_option = sort_option[1:] |
|
98 elif sort_option[0] == '+': |
|
99 sort_option = sort_option[1:] |
|
100 |
|
101 if sort_option == "name": |
|
102 lines = human_sorted_items(lines, reverse=reverse) |
|
103 else: |
|
104 position = column_order.get(sort_option) |
|
105 if position is None: |
|
106 raise ConfigError(f"Invalid sorting option: {self.config.sort!r}") |
|
107 lines.sort(key=lambda l: (l[1][position], l[0]), reverse=reverse) |
|
108 |
|
109 for line in lines: |
|
110 self.writeout(line[0]) |
|
111 |
|
112 # Write a TOTAL line if we had at least one file. |
|
113 if self.total.n_files > 0: |
|
114 self.writeout(rule) |
|
115 args = ("TOTAL", self.total.n_statements, self.total.n_missing) |
|
116 if self.branches: |
|
117 args += (self.total.n_branches, self.total.n_partial_branches) |
|
118 args += (self.total.pc_covered_str,) |
|
119 if self.config.show_missing: |
|
120 args += ("",) |
|
121 self.writeout(fmt_coverage % args) |
|
122 |
|
123 # Write other final lines. |
|
124 if not self.total.n_files and not self.skipped_count: |
|
125 raise NoDataError("No data to report.") |
|
126 |
|
127 if self.config.skip_covered and self.skipped_count: |
|
128 self.writeout( |
|
129 fmt_skip_covered % (self.skipped_count, 's' if self.skipped_count > 1 else '') |
|
130 ) |
|
131 if self.config.skip_empty and self.empty_count: |
|
132 self.writeout( |
|
133 fmt_skip_empty % (self.empty_count, 's' if self.empty_count > 1 else '') |
|
134 ) |
|
135 |
|
136 return self.total.n_statements and self.total.pc_covered |
|
137 |
|
138 def report_one_file(self, fr, analysis): |
|
139 """Report on just one file, the callback from report().""" |
|
140 nums = analysis.numbers |
|
141 self.total += nums |
|
142 |
|
143 no_missing_lines = (nums.n_missing == 0) |
|
144 no_missing_branches = (nums.n_partial_branches == 0) |
|
145 if self.config.skip_covered and no_missing_lines and no_missing_branches: |
|
146 # Don't report on 100% files. |
|
147 self.skipped_count += 1 |
|
148 elif self.config.skip_empty and nums.n_statements == 0: |
|
149 # Don't report on empty files. |
|
150 self.empty_count += 1 |
|
151 else: |
|
152 self.fr_analysis.append((fr, analysis)) |