summary.py 2.9 KB

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