summaryrefslogtreecommitdiff
path: root/coverage/report.py
blob: a1d5082854d486e7f15e75e02c307b20075b6f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Reporter foundation for Coverage."""

import fnmatch, os
from coverage.backward import string_class
from coverage.codeunit import code_unit_factory
from coverage.misc import CoverageException, NoSource

class Reporter(object):
    """A base class for all reporters."""

    def __init__(self, coverage, ignore_errors=False):
        """Create a reporter.

        `coverage` is the coverage instance. `ignore_errors` controls how
        skittish the reporter will be during file processing.

        """
        self.coverage = coverage
        self.ignore_errors = ignore_errors

        # The code units to report on.  Set by find_code_units.
        self.code_units = []

        # The directory into which to place the report, used by some derived
        # classes.
        self.directory = None

    def find_code_units(self, morfs, omit, include):
        """Find the code units we'll report on.

        `morfs` is a list of modules or filenames.

        See `coverage.report()` for other arguments.

        """
        morfs = morfs or self.coverage.data.executed_files()
        file_locator = self.coverage.file_locator
        self.code_units = code_unit_factory(morfs, file_locator)

        if include:
            assert not isinstance(include, string_class) # common mistake
            patterns = [file_locator.abs_file(p) for p in include]
            filtered = []
            for cu in self.code_units:
                for pattern in patterns:
                    if fnmatch.fnmatch(cu.filename, pattern):
                        filtered.append(cu)
                        break
            self.code_units = filtered

        if omit:
            assert not isinstance(omit, string_class) # common mistake
            patterns = [file_locator.abs_file(p) for p in omit]
            filtered = []
            for cu in self.code_units:
                for pattern in patterns:
                    if fnmatch.fnmatch(cu.filename, pattern):
                        break
                else:
                    filtered.append(cu)
            self.code_units = filtered

        self.code_units.sort()

    def report_files(self, report_fn, morfs, directory=None,
                        omit=None, include=None):
        """Run a reporting function on a number of morfs.

        `report_fn` is called for each relative morf in `morfs`.

        `include` is a list of filename patterns. CodeUnits that match
        those patterns will be included in the list. CodeUnits that match
        `omit` will be omitted from the list.

        """
        self.find_code_units(morfs, omit, include)

        if not self.code_units:
            raise CoverageException("No data to report.")

        self.directory = directory
        if self.directory and not os.path.exists(self.directory):
            os.makedirs(self.directory)

        for cu in self.code_units:
            try:
                report_fn(cu, self.coverage._analyze(cu))
            except NoSource:
                if not self.ignore_errors:
                    raise