summary.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. """Summary reporting"""
  4. import sys
  5. from coverage import env
  6. from coverage.report import Reporter
  7. from coverage.results import Numbers
  8. from coverage.misc import NotPython, CoverageException, output_encoding, StopEverything
  9. class SummaryReporter(Reporter):
  10. """A reporter for writing the summary report."""
  11. def __init__(self, coverage, config):
  12. super(SummaryReporter, self).__init__(coverage, config)
  13. self.branches = coverage.data.has_arcs()
  14. def report(self, morfs, outfile=None):
  15. """Writes a report summarizing coverage statistics per module.
  16. `outfile` is a file object to write the summary to. It must be opened
  17. for native strings (bytes on Python 2, Unicode on Python 3).
  18. """
  19. if outfile is None:
  20. outfile = sys.stdout
  21. def writeout(line):
  22. """Write a line to the output, adding a newline."""
  23. if env.PY2:
  24. line = line.encode(output_encoding())
  25. outfile.write(line.rstrip())
  26. outfile.write("\n")
  27. fr_analysis = []
  28. skipped_count = 0
  29. total = Numbers()
  30. fmt_err = u"%s %s: %s"
  31. for fr in self.find_file_reporters(morfs):
  32. try:
  33. analysis = self.coverage._analyze(fr)
  34. nums = analysis.numbers
  35. total += nums
  36. if self.config.skip_covered:
  37. # Don't report on 100% files.
  38. no_missing_lines = (nums.n_missing == 0)
  39. no_missing_branches = (nums.n_partial_branches == 0)
  40. if no_missing_lines and no_missing_branches:
  41. skipped_count += 1
  42. continue
  43. fr_analysis.append((fr, analysis))
  44. except StopEverything:
  45. # Don't report this on single files, it's a systemic problem.
  46. raise
  47. except Exception:
  48. report_it = not self.config.ignore_errors
  49. if report_it:
  50. typ, msg = sys.exc_info()[:2]
  51. # NotPython is only raised by PythonFileReporter, which has a
  52. # should_be_python() method.
  53. if issubclass(typ, NotPython) and not fr.should_be_python():
  54. report_it = False
  55. if report_it:
  56. writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg))
  57. # Prepare the formatting strings, header, and column sorting.
  58. max_name = max([len(fr.relative_filename()) for (fr, analysis) in fr_analysis] + [5])
  59. fmt_name = u"%%- %ds " % max_name
  60. fmt_skip_covered = u"\n%s file%s skipped due to complete coverage."
  61. header = (fmt_name % "Name") + u" Stmts Miss"
  62. fmt_coverage = fmt_name + u"%6d %6d"
  63. if self.branches:
  64. header += u" Branch BrPart"
  65. fmt_coverage += u" %6d %6d"
  66. width100 = Numbers.pc_str_width()
  67. header += u"%*s" % (width100+4, "Cover")
  68. fmt_coverage += u"%%%ds%%%%" % (width100+3,)
  69. if self.config.show_missing:
  70. header += u" Missing"
  71. fmt_coverage += u" %s"
  72. rule = u"-" * len(header)
  73. column_order = dict(name=0, stmts=1, miss=2, cover=-1)
  74. if self.branches:
  75. column_order.update(dict(branch=3, brpart=4))
  76. # Write the header
  77. writeout(header)
  78. writeout(rule)
  79. # `lines` is a list of pairs, (line text, line values). The line text
  80. # is a string that will be printed, and line values is a tuple of
  81. # sortable values.
  82. lines = []
  83. for (fr, analysis) in fr_analysis:
  84. try:
  85. nums = analysis.numbers
  86. args = (fr.relative_filename(), nums.n_statements, nums.n_missing)
  87. if self.branches:
  88. args += (nums.n_branches, nums.n_partial_branches)
  89. args += (nums.pc_covered_str,)
  90. if self.config.show_missing:
  91. missing_fmtd = analysis.missing_formatted()
  92. if self.branches:
  93. branches_fmtd = analysis.arcs_missing_formatted()
  94. if branches_fmtd:
  95. if missing_fmtd:
  96. missing_fmtd += ", "
  97. missing_fmtd += branches_fmtd
  98. args += (missing_fmtd,)
  99. text = fmt_coverage % args
  100. # Add numeric percent coverage so that sorting makes sense.
  101. args += (nums.pc_covered,)
  102. lines.append((text, args))
  103. except Exception:
  104. report_it = not self.config.ignore_errors
  105. if report_it:
  106. typ, msg = sys.exc_info()[:2]
  107. # NotPython is only raised by PythonFileReporter, which has a
  108. # should_be_python() method.
  109. if typ is NotPython and not fr.should_be_python():
  110. report_it = False
  111. if report_it:
  112. writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg))
  113. # Sort the lines and write them out.
  114. if getattr(self.config, 'sort', None):
  115. position = column_order.get(self.config.sort.lower())
  116. if position is None:
  117. raise CoverageException("Invalid sorting option: {0!r}".format(self.config.sort))
  118. lines.sort(key=lambda l: (l[1][position], l[0]))
  119. for line in lines:
  120. writeout(line[0])
  121. # Write a TOTAl line if we had more than one file.
  122. if total.n_files > 1:
  123. writeout(rule)
  124. args = ("TOTAL", total.n_statements, total.n_missing)
  125. if self.branches:
  126. args += (total.n_branches, total.n_partial_branches)
  127. args += (total.pc_covered_str,)
  128. if self.config.show_missing:
  129. args += ("",)
  130. writeout(fmt_coverage % args)
  131. # Write other final lines.
  132. if not total.n_files and not skipped_count:
  133. raise CoverageException("No data to report.")
  134. if self.config.skip_covered and skipped_count:
  135. writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else ''))
  136. return total.n_statements and total.pc_covered