2 |
2 |
3 import sys |
3 import sys |
4 |
4 |
5 from .report import Reporter |
5 from .report import Reporter |
6 from .results import Numbers |
6 from .results import Numbers |
|
7 from .misc import NotPython |
7 |
8 |
8 |
9 |
9 class SummaryReporter(Reporter): |
10 class SummaryReporter(Reporter): |
10 """A reporter for writing the summary report.""" |
11 """A reporter for writing the summary report.""" |
11 |
12 |
12 def __init__(self, coverage, show_missing=True, ignore_errors=False): |
13 def __init__(self, coverage, config): |
13 super(SummaryReporter, self).__init__(coverage, ignore_errors) |
14 super(SummaryReporter, self).__init__(coverage, config) |
14 self.show_missing = show_missing |
|
15 self.branches = coverage.data.has_arcs() |
15 self.branches = coverage.data.has_arcs() |
16 |
16 |
17 def report(self, morfs, omit_prefixes=None, outfile=None): |
17 def report(self, morfs, outfile=None): |
18 """Writes a report summarizing coverage statistics per module.""" |
18 """Writes a report summarizing coverage statistics per module. |
19 |
19 |
20 self.find_code_units(morfs, omit_prefixes) |
20 `outfile` is a file object to write the summary to. |
|
21 |
|
22 """ |
|
23 self.find_code_units(morfs) |
21 |
24 |
22 # Prepare the formatting strings |
25 # Prepare the formatting strings |
23 max_name = max([len(cu.name) for cu in self.code_units] + [5]) |
26 max_name = max([len(cu.name) for cu in self.code_units] + [5]) |
24 fmt_name = "%%- %ds " % max_name |
27 fmt_name = "%%- %ds " % max_name |
25 fmt_err = "%s %s: %s\n" |
28 fmt_err = "%s %s: %s\n" |
26 header = (fmt_name % "Name") + " Stmts Exec" |
29 header = (fmt_name % "Name") + " Stmts Miss" |
27 fmt_coverage = fmt_name + "%6d %6d" |
30 fmt_coverage = fmt_name + "%6d %6d" |
28 if self.branches: |
31 if self.branches: |
29 header += " Branch BrExec" |
32 header += " Branch BrMiss" |
30 fmt_coverage += " %6d %6d" |
33 fmt_coverage += " %6d %6d" |
31 header += " Cover" |
34 width100 = Numbers.pc_str_width() |
32 fmt_coverage += " %5d%%" |
35 header += "%*s" % (width100+4, "Cover") |
33 if self.show_missing: |
36 fmt_coverage += "%%%ds%%%%" % (width100+3,) |
|
37 if self.config.show_missing: |
34 header += " Missing" |
38 header += " Missing" |
35 fmt_coverage += " %s" |
39 fmt_coverage += " %s" |
36 rule = "-" * len(header) + "\n" |
40 rule = "-" * len(header) + "\n" |
37 header += "\n" |
41 header += "\n" |
38 fmt_coverage += "\n" |
42 fmt_coverage += "\n" |
48 |
52 |
49 for cu in self.code_units: |
53 for cu in self.code_units: |
50 try: |
54 try: |
51 analysis = self.coverage._analyze(cu) |
55 analysis = self.coverage._analyze(cu) |
52 nums = analysis.numbers |
56 nums = analysis.numbers |
53 args = (cu.name, nums.n_statements, nums.n_executed) |
57 args = (cu.name, nums.n_statements, nums.n_missing) |
54 if self.branches: |
58 if self.branches: |
55 args += (nums.n_branches, nums.n_executed_branches) |
59 args += (nums.n_branches, nums.n_missing_branches) |
56 args += (nums.pc_covered,) |
60 args += (nums.pc_covered_str,) |
57 if self.show_missing: |
61 if self.config.show_missing: |
58 args += (analysis.missing_formatted(),) |
62 args += (analysis.missing_formatted(),) |
59 outfile.write(fmt_coverage % args) |
63 outfile.write(fmt_coverage % args) |
60 total += nums |
64 total += nums |
61 except KeyboardInterrupt: #pragma: no cover |
65 except KeyboardInterrupt: # pragma: not covered |
62 raise |
66 raise |
63 except: |
67 except: |
64 if not self.ignore_errors: |
68 report_it = not self.config.ignore_errors |
|
69 if report_it: |
65 typ, msg = sys.exc_info()[:2] |
70 typ, msg = sys.exc_info()[:2] |
|
71 if typ is NotPython and not cu.should_be_python(): |
|
72 report_it = False |
|
73 if report_it: |
66 outfile.write(fmt_err % (cu.name, typ.__name__, msg)) |
74 outfile.write(fmt_err % (cu.name, typ.__name__, msg)) |
67 |
75 |
68 if total.n_files > 1: |
76 if total.n_files > 1: |
69 outfile.write(rule) |
77 outfile.write(rule) |
70 args = ("TOTAL", total.n_statements, total.n_executed) |
78 args = ("TOTAL", total.n_statements, total.n_missing) |
71 if self.branches: |
79 if self.branches: |
72 args += (total.n_branches, total.n_executed_branches) |
80 args += (total.n_branches, total.n_missing_branches) |
73 args += (total.pc_covered,) |
81 args += (total.pc_covered_str,) |
74 if self.show_missing: |
82 if self.config.show_missing: |
75 args += ("",) |
83 args += ("",) |
76 outfile.write(fmt_coverage % args) |
84 outfile.write(fmt_coverage % args) |
77 |
85 |
78 # |
86 return total.pc_covered |
79 # eflag: FileType = Python2 |
|