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 """Reporter foundation for coverage.py.""" |
|
5 |
|
6 import os |
|
7 import warnings |
|
8 |
|
9 from coverage.files import prep_patterns, FnmatchMatcher |
|
10 from coverage.misc import CoverageException, NoSource, NotPython, isolate_module |
|
11 |
|
12 os = isolate_module(os) |
|
13 |
|
14 |
|
15 class Reporter(object): |
|
16 """A base class for all reporters.""" |
|
17 |
|
18 def __init__(self, coverage, config): |
|
19 """Create a reporter. |
|
20 |
|
21 `coverage` is the coverage instance. `config` is an instance of |
|
22 CoverageConfig, for controlling all sorts of behavior. |
|
23 |
|
24 """ |
|
25 self.coverage = coverage |
|
26 self.config = config |
|
27 |
|
28 # The directory into which to place the report, used by some derived |
|
29 # classes. |
|
30 self.directory = None |
|
31 |
|
32 # Our method find_file_reporters used to set an attribute that other |
|
33 # code could read. That's been refactored away, but some third parties |
|
34 # were using that attribute. We'll continue to support it in a noisy |
|
35 # way for now. |
|
36 self._file_reporters = [] |
|
37 |
|
38 @property |
|
39 def file_reporters(self): |
|
40 """Keep .file_reporters working for private-grabbing tools.""" |
|
41 warnings.warn( |
|
42 "Report.file_reporters will no longer be available in Coverage.py 4.2", |
|
43 DeprecationWarning, |
|
44 ) |
|
45 return self._file_reporters |
|
46 |
|
47 def find_file_reporters(self, morfs): |
|
48 """Find the FileReporters we'll report on. |
|
49 |
|
50 `morfs` is a list of modules or file names. |
|
51 |
|
52 Returns a list of FileReporters. |
|
53 |
|
54 """ |
|
55 reporters = self.coverage._get_file_reporters(morfs) |
|
56 |
|
57 if self.config.include: |
|
58 matcher = FnmatchMatcher(prep_patterns(self.config.include)) |
|
59 reporters = [fr for fr in reporters if matcher.match(fr.filename)] |
|
60 |
|
61 if self.config.omit: |
|
62 matcher = FnmatchMatcher(prep_patterns(self.config.omit)) |
|
63 reporters = [fr for fr in reporters if not matcher.match(fr.filename)] |
|
64 |
|
65 self._file_reporters = sorted(reporters) |
|
66 return self._file_reporters |
|
67 |
|
68 def report_files(self, report_fn, morfs, directory=None): |
|
69 """Run a reporting function on a number of morfs. |
|
70 |
|
71 `report_fn` is called for each relative morf in `morfs`. It is called |
|
72 as:: |
|
73 |
|
74 report_fn(file_reporter, analysis) |
|
75 |
|
76 where `file_reporter` is the `FileReporter` for the morf, and |
|
77 `analysis` is the `Analysis` for the morf. |
|
78 |
|
79 """ |
|
80 file_reporters = self.find_file_reporters(morfs) |
|
81 |
|
82 if not file_reporters: |
|
83 raise CoverageException("No data to report.") |
|
84 |
|
85 self.directory = directory |
|
86 if self.directory and not os.path.exists(self.directory): |
|
87 os.makedirs(self.directory) |
|
88 |
|
89 for fr in file_reporters: |
|
90 try: |
|
91 report_fn(fr, self.coverage._analyze(fr)) |
|
92 except NoSource: |
|
93 if not self.config.ignore_errors: |
|
94 raise |
|
95 except NotPython: |
|
96 # Only report errors for .py files, and only if we didn't |
|
97 # explicitly suppress those errors. |
|
98 # NotPython is only raised by PythonFileReporter, which has a |
|
99 # should_be_python() method. |
|
100 if fr.should_be_python() and not self.config.ignore_errors: |
|
101 raise |
|
102 |
|
103 # |
|
104 # eflag: FileType = Python2 |
|