summaryrefslogtreecommitdiff
path: root/tests/plugin2.py
blob: 6e875e5e9cb9379b84faf7f62a2a6c573804eeed (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
"""A plugin for test_plugins.py to import."""

import os.path

import coverage


class Plugin(coverage.CoveragePlugin):
    """A plugin for testing."""
    def file_tracer(self, filename):
        if "render.py" in filename:
            return RenderFileTracer()

    def file_reporter(self, filename):
        return FileReporter(filename)


class RenderFileTracer(coverage.plugin.FileTracer):
    """A FileTracer using information from the caller."""

    def has_dynamic_source_filename(self):
        return True

    def dynamic_source_filename(self, filename, frame):
        if frame.f_code.co_name != "render":
            return None
        source_filename = os.path.abspath(frame.f_locals['filename'])
        return source_filename

    def line_number_range(self, frame):
        lineno = frame.f_locals['linenum']
        return lineno, lineno+1


class FileReporter(coverage.plugin.FileReporter):
    """A goofy file reporter."""
    def statements(self):
        # Goofy test arrangement: claim that the file has as many lines as the
        # number in its name.
        num = os.path.basename(self.filename).split(".")[0].split("_")[1]
        return set(range(1, int(num)+1))


def coverage_init(reg, options):        # pylint: disable=unused-argument
    """Called by coverage to initialize the plugins here."""
    reg.add_file_tracer(Plugin())