summaryrefslogtreecommitdiff
path: root/tests/plugin1.py
blob: 4848eaff59e036ea401abf1798f4ac1b8bee4f7f (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
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""A file tracer plugin for test_plugins.py to import."""

from __future__ import annotations

import os.path

from types import FrameType
from typing import Any, Optional, Set, Tuple, Union

from coverage import CoveragePlugin, FileReporter, FileTracer
from coverage.plugin_support import Plugins
from coverage.types import TLineNo

class Plugin(CoveragePlugin):
    """A file tracer plugin to import, so that it isn't in the test's current directory."""

    def file_tracer(self, filename: str) -> Optional[FileTracer]:
        """Trace only files named xyz.py"""
        if "xyz.py" in filename:
            return MyFileTracer(filename)
        return None

    def file_reporter(self, filename: str) -> Union[FileReporter, str]:
        return MyFileReporter(filename)


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

    def __init__(self, filename: str) -> None:
        """Claim that */*xyz.py was actually sourced from /src/*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) -> str:
        return self._source_filename

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


class MyFileReporter(FileReporter):
    """Dead-simple FileReporter."""
    def lines(self) -> Set[TLineNo]:
        return {105, 106, 107, 205, 206, 207}


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