|
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
|
2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
|
3 |
|
4 """Summary reporting""" |
|
5 |
|
6 import sys |
|
7 |
|
8 from coverage import env |
|
9 from coverage.report import Reporter |
|
10 from coverage.results import Numbers |
|
11 from coverage.misc import NotPython, CoverageException, output_encoding |
|
12 |
|
13 |
|
14 class SummaryReporter(Reporter): |
|
15 """A reporter for writing the summary report.""" |
|
16 |
|
17 def __init__(self, coverage, config): |
|
18 super(SummaryReporter, self).__init__(coverage, config) |
|
19 self.branches = coverage.data.has_arcs() |
|
20 |
|
21 def report(self, morfs, outfile=None): |
|
22 """Writes a report summarizing coverage statistics per module. |
|
23 |
|
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). |
|
26 |
|
27 """ |
|
28 file_reporters = self.find_file_reporters(morfs) |
|
29 |
|
30 # Prepare the formatting strings |
|
31 max_name = max([len(fr.relative_filename()) for fr in file_reporters] + [5]) |
|
32 fmt_name = u"%%- %ds " % max_name |
|
33 fmt_err = u"%s %s: %s" |
|
34 fmt_skip_covered = u"\n%s file%s skipped due to complete coverage." |
|
35 |
|
36 header = (fmt_name % "Name") + u" Stmts Miss" |
|
37 fmt_coverage = fmt_name + u"%6d %6d" |
|
38 if self.branches: |
|
39 header += u" Branch BrPart" |
|
40 fmt_coverage += u" %6d %6d" |
|
41 width100 = Numbers.pc_str_width() |
|
42 header += u"%*s" % (width100+4, "Cover") |
|
43 fmt_coverage += u"%%%ds%%%%" % (width100+3,) |
|
44 if self.config.show_missing: |
|
45 header += u" Missing" |
|
46 fmt_coverage += u" %s" |
|
47 rule = u"-" * len(header) |
|
48 |
|
49 if outfile is None: |
|
50 outfile = sys.stdout |
|
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 |
|
59 # Write the header |
|
60 writeout(header) |
|
61 writeout(rule) |
|
62 |
|
63 total = Numbers() |
|
64 skipped_count = 0 |
|
65 |
|
66 for fr in file_reporters: |
|
67 try: |
|
68 analysis = self.coverage._analyze(fr) |
|
69 nums = analysis.numbers |
|
70 total += nums |
|
71 |
|
72 if self.config.skip_covered: |
|
73 # Don't report on 100% files. |
|
74 no_missing_lines = (nums.n_missing == 0) |
|
75 no_missing_branches = (nums.n_partial_branches == 0) |
|
76 if no_missing_lines and no_missing_branches: |
|
77 skipped_count += 1 |
|
78 continue |
|
79 |
|
80 args = (fr.relative_filename(), nums.n_statements, nums.n_missing) |
|
81 if self.branches: |
|
82 args += (nums.n_branches, nums.n_partial_branches) |
|
83 args += (nums.pc_covered_str,) |
|
84 if self.config.show_missing: |
|
85 missing_fmtd = analysis.missing_formatted() |
|
86 if self.branches: |
|
87 branches_fmtd = analysis.arcs_missing_formatted() |
|
88 if branches_fmtd: |
|
89 if missing_fmtd: |
|
90 missing_fmtd += ", " |
|
91 missing_fmtd += branches_fmtd |
|
92 args += (missing_fmtd,) |
|
93 writeout(fmt_coverage % args) |
|
94 except Exception: |
|
95 report_it = not self.config.ignore_errors |
|
96 if report_it: |
|
97 typ, msg = sys.exc_info()[:2] |
|
98 # NotPython is only raised by PythonFileReporter, which has a |
|
99 # should_be_python() method. |
|
100 if typ is NotPython and not fr.should_be_python(): |
|
101 report_it = False |
|
102 if report_it: |
|
103 writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) |
|
104 |
|
105 if total.n_files > 1: |
|
106 writeout(rule) |
|
107 args = ("TOTAL", total.n_statements, total.n_missing) |
|
108 if self.branches: |
|
109 args += (total.n_branches, total.n_partial_branches) |
|
110 args += (total.pc_covered_str,) |
|
111 if self.config.show_missing: |
|
112 args += ("",) |
|
113 writeout(fmt_coverage % args) |
|
114 |
|
115 if not total.n_files and not skipped_count: |
|
116 raise CoverageException("No data to report.") |
|
117 |
|
118 if self.config.skip_covered and skipped_count: |
|
119 writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) |
|
120 |
|
121 return total.n_statements and total.pc_covered |
|
122 |
|
123 # |
|
124 # eflag: FileType = Python2 |