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
|
# 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
from coverage import CoveragePlugin, FileReporter, FileTracer
from coverage.plugin_support import Plugins
from coverage.types import TLineNo
try:
import third.render # pylint: disable=unused-import
except ImportError:
# This plugin is used in a few tests. One of them has the third.render
# module, but most don't. We need to import it but not use it, so just
# try importing it and it's OK if the module doesn't exist.
pass
class Plugin(CoveragePlugin):
"""A file tracer plugin for testing."""
def file_tracer(self, filename: str) -> FileTracer | None:
if "render.py" in filename:
return RenderFileTracer()
return None
def file_reporter(self, filename: str) -> FileReporter:
return MyFileReporter(filename)
class RenderFileTracer(FileTracer):
"""A FileTracer using information from the caller."""
def has_dynamic_source_filename(self) -> bool:
return True
def dynamic_source_filename(
self,
filename: str,
frame: FrameType,
) -> str | None:
if frame.f_code.co_name != "render":
return None
source_filename: str = os.path.abspath(frame.f_locals['filename'])
return source_filename
def line_number_range(self, frame: FrameType) -> tuple[TLineNo, TLineNo]:
lineno = frame.f_locals['linenum']
return lineno, lineno+1
class MyFileReporter(FileReporter):
"""A goofy file reporter."""
def lines(self) -> set[TLineNo]:
# 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: Plugins,
options: Any, # pylint: disable=unused-argument
) -> None:
"""Called by coverage to initialize the plugins here."""
reg.add_file_tracer(Plugin())
|