summaryrefslogtreecommitdiff
path: root/tests/plugin1.py
blob: f9da35c89ea70016dd0e1671c4e092a35bd3bc0a (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 to import, so that it isn't in the test's current directory."""

    def file_tracer(self, filename):
        """Trace only files named xyz.py"""
        if "xyz.py" in filename:
            return FileTracer(filename)

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


class FileTracer(coverage.plugin.FileTracer):
    """A FileTracer emulating a simple static plugin."""

    def __init__(self, filename):
        """Claim that xyz.py was actually sourced from ABC.zz"""
        self._filename = filename
        self._source_filename = os.path.join(
            "/src",
            os.path.basename(filename.replace("xyz.py", "ABC.zz"))
        )

    def source_filename(self):
        return self._source_filename

    def line_number_range(self, frame):
        """Map the line number X to X05,X06,X07."""
        lineno = frame.f_lineno
        return lineno*100+5, lineno*100+7


class FileReporter(coverage.plugin.FileReporter):
    """Dead-simple FileReporter."""
    def statements(self):
        return set([105, 106, 107, 205, 206, 207])

    def excluded_statements(self):
        return set([])