eric6/DebugClients/Python/coverage/summary.py

changeset 7427
362cd1b6f81a
parent 6942
2602857055c5
child 7702
f8b97639deb5
--- a/eric6/DebugClients/Python/coverage/summary.py	Wed Feb 19 19:38:36 2020 +0100
+++ b/eric6/DebugClients/Python/coverage/summary.py	Sat Feb 22 14:27:42 2020 +0100
@@ -1,22 +1,36 @@
 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
-# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
+# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
 
 """Summary reporting"""
 
 import sys
 
 from coverage import env
-from coverage.report import Reporter
+from coverage.report import get_analysis_to_report
 from coverage.results import Numbers
-from coverage.misc import NotPython, CoverageException, output_encoding, StopEverything
+from coverage.misc import NotPython, CoverageException, output_encoding
 
 
-class SummaryReporter(Reporter):
+class SummaryReporter(object):
     """A reporter for writing the summary report."""
 
-    def __init__(self, coverage, config):
-        super(SummaryReporter, self).__init__(coverage, config)
-        self.branches = coverage.data.has_arcs()
+    def __init__(self, coverage):
+        self.coverage = coverage
+        self.config = self.coverage.config
+        self.branches = coverage.get_data().has_arcs()
+        self.outfile = None
+        self.fr_analysis = []
+        self.skipped_count = 0
+        self.empty_count = 0
+        self.total = Numbers()
+        self.fmt_err = u"%s   %s: %s"
+
+    def writeout(self, line):
+        """Write a line to the output, adding a newline."""
+        if env.PY2:
+            line = line.encode(output_encoding())
+        self.outfile.write(line.rstrip())
+        self.outfile.write("\n")
 
     def report(self, morfs, outfile=None):
         """Writes a report summarizing coverage statistics per module.
@@ -25,54 +39,17 @@
         for native strings (bytes on Python 2, Unicode on Python 3).
 
         """
-        if outfile is None:
-            outfile = sys.stdout
-
-        def writeout(line):
-            """Write a line to the output, adding a newline."""
-            if env.PY2:
-                line = line.encode(output_encoding())
-            outfile.write(line.rstrip())
-            outfile.write("\n")
-
-        fr_analysis = []
-        skipped_count = 0
-        total = Numbers()
-
-        fmt_err = u"%s   %s: %s"
-
-        for fr in self.find_file_reporters(morfs):
-            try:
-                analysis = self.coverage._analyze(fr)
-                nums = analysis.numbers
-                total += nums
+        self.outfile = outfile or sys.stdout
 
-                if self.config.skip_covered:
-                    # Don't report on 100% files.
-                    no_missing_lines = (nums.n_missing == 0)
-                    no_missing_branches = (nums.n_partial_branches == 0)
-                    if no_missing_lines and no_missing_branches:
-                        skipped_count += 1
-                        continue
-                fr_analysis.append((fr, analysis))
-            except StopEverything:
-                # Don't report this on single files, it's a systemic problem.
-                raise
-            except Exception:
-                report_it = not self.config.ignore_errors
-                if report_it:
-                    typ, msg = sys.exc_info()[:2]
-                    # NotPython is only raised by PythonFileReporter, which has a
-                    # should_be_python() method.
-                    if issubclass(typ, NotPython) and not fr.should_be_python():
-                        report_it = False
-                if report_it:
-                    writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg))
+        self.coverage.get_data().set_query_contexts(self.config.report_contexts)
+        for fr, analysis in get_analysis_to_report(self.coverage, morfs):
+            self.report_one_file(fr, analysis)
 
         # Prepare the formatting strings, header, and column sorting.
-        max_name = max([len(fr.relative_filename()) for (fr, analysis) in fr_analysis] + [5])
+        max_name = max([len(fr.relative_filename()) for (fr, analysis) in self.fr_analysis] + [5])
         fmt_name = u"%%- %ds  " % max_name
         fmt_skip_covered = u"\n%s file%s skipped due to complete coverage."
+        fmt_skip_empty = u"\n%s empty file%s skipped."
 
         header = (fmt_name % "Name") + u" Stmts   Miss"
         fmt_coverage = fmt_name + u"%6d %6d"
@@ -92,15 +69,15 @@
             column_order.update(dict(branch=3, brpart=4))
 
         # Write the header
-        writeout(header)
-        writeout(rule)
+        self.writeout(header)
+        self.writeout(rule)
 
         # `lines` is a list of pairs, (line text, line values).  The line text
         # is a string that will be printed, and line values is a tuple of
         # sortable values.
         lines = []
 
-        for (fr, analysis) in fr_analysis:
+        for (fr, analysis) in self.fr_analysis:
             try:
                 nums = analysis.numbers
 
@@ -109,14 +86,7 @@
                     args += (nums.n_branches, nums.n_partial_branches)
                 args += (nums.pc_covered_str,)
                 if self.config.show_missing:
-                    missing_fmtd = analysis.missing_formatted()
-                    if self.branches:
-                        branches_fmtd = analysis.arcs_missing_formatted()
-                        if branches_fmtd:
-                            if missing_fmtd:
-                                missing_fmtd += ", "
-                            missing_fmtd += branches_fmtd
-                    args += (missing_fmtd,)
+                    args += (analysis.missing_formatted(branches=True),)
                 text = fmt_coverage % args
                 # Add numeric percent coverage so that sorting makes sense.
                 args += (nums.pc_covered,)
@@ -130,34 +100,56 @@
                     if typ is NotPython and not fr.should_be_python():
                         report_it = False
                 if report_it:
-                    writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg))
+                    self.writeout(self.fmt_err % (fr.relative_filename(), typ.__name__, msg))
 
         # Sort the lines and write them out.
         if getattr(self.config, 'sort', None):
             position = column_order.get(self.config.sort.lower())
             if position is None:
-                raise CoverageException("Invalid sorting option: {0!r}".format(self.config.sort))
+                raise CoverageException("Invalid sorting option: {!r}".format(self.config.sort))
             lines.sort(key=lambda l: (l[1][position], l[0]))
 
         for line in lines:
-            writeout(line[0])
+            self.writeout(line[0])
 
         # Write a TOTAl line if we had more than one file.
-        if total.n_files > 1:
-            writeout(rule)
-            args = ("TOTAL", total.n_statements, total.n_missing)
+        if self.total.n_files > 1:
+            self.writeout(rule)
+            args = ("TOTAL", self.total.n_statements, self.total.n_missing)
             if self.branches:
-                args += (total.n_branches, total.n_partial_branches)
-            args += (total.pc_covered_str,)
+                args += (self.total.n_branches, self.total.n_partial_branches)
+            args += (self.total.pc_covered_str,)
             if self.config.show_missing:
                 args += ("",)
-            writeout(fmt_coverage % args)
+            self.writeout(fmt_coverage % args)
 
         # Write other final lines.
-        if not total.n_files and not skipped_count:
+        if not self.total.n_files and not self.skipped_count:
             raise CoverageException("No data to report.")
 
-        if self.config.skip_covered and skipped_count:
-            writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else ''))
+        if self.config.skip_covered and self.skipped_count:
+            self.writeout(
+                fmt_skip_covered % (self.skipped_count, 's' if self.skipped_count > 1 else '')
+                )
+        if self.config.skip_empty and self.empty_count:
+            self.writeout(
+                fmt_skip_empty % (self.empty_count, 's' if self.empty_count > 1 else '')
+                )
+
+        return self.total.n_statements and self.total.pc_covered
 
-        return total.n_statements and total.pc_covered
+    def report_one_file(self, fr, analysis):
+        """Report on just one file, the callback from report()."""
+        nums = analysis.numbers
+        self.total += nums
+
+        no_missing_lines = (nums.n_missing == 0)
+        no_missing_branches = (nums.n_partial_branches == 0)
+        if self.config.skip_covered and no_missing_lines and no_missing_branches:
+            # Don't report on 100% files.
+            self.skipped_count += 1
+        elif self.config.skip_empty and nums.n_statements == 0:
+            # Don't report on empty files.
+            self.empty_count += 1
+        else:
+            self.fr_analysis.append((fr, analysis))

eric ide

mercurial