Coverage for tests / plugin2.py: 64.103%
35 statements
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
1# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
4"""A file tracer plugin for test_plugins.py to import."""
6from __future__ import annotations 1abcdefghijklmnopqrstuvwxyzA
8import os.path 1abcdefghijklmnopqrstuvwxyzA
10from types import FrameType 1abcdefghijklmnopqrstuvwxyzA
11from typing import Any 1abcdefghijklmnopqrstuvwxyzA
13from coverage import CoveragePlugin, FileReporter, FileTracer 1abcdefghijklmnopqrstuvwxyzA
14from coverage.plugin_support import Plugins 1abcdefghijklmnopqrstuvwxyzA
15from coverage.types import TLineNo 1abcdefghijklmnopqrstuvwxyzA
17try: 1abcdefghijklmnopqrstuvwxyzA
18 import third.render # pylint: disable=unused-import 1abcdefghijklmnopqrstuvwxyzA
19except ImportError: 1abcdefghijklmnopqrstuvwxyzA
20 # This plugin is used in a few tests. One of them has the third.render
21 # module, but most don't. We need to import it but not use it, so just
22 # try importing it and it's OK if the module doesn't exist.
23 pass 1abcdefghijklmnopqrstuvwxyzA
26class Plugin(CoveragePlugin): 1abcdefghijklmnopqrstuvwxyzA
27 """A file tracer plugin for testing."""
29 def file_tracer(self, filename: str) -> FileTracer | None: 1abcdefghijklmnopqrstuvwxyzA
30 if "render.py" in filename:
31 return RenderFileTracer()
32 return None
34 def file_reporter(self, filename: str) -> FileReporter: 1abcdefghijklmnopqrstuvwxyzA
35 return MyFileReporter(filename) 1abcdefghijklmnopqrstuvwxyzA
38class RenderFileTracer(FileTracer): 1abcdefghijklmnopqrstuvwxyzA
39 """A FileTracer using information from the caller."""
41 def has_dynamic_source_filename(self) -> bool: 1abcdefghijklmnopqrstuvwxyzA
42 return True
44 def dynamic_source_filename( 1abcdefghijklmnopqrstuvwxyzA
45 self,
46 filename: str,
47 frame: FrameType,
48 ) -> str | None:
49 if frame.f_code.co_name != "render":
50 return None
51 source_filename: str = os.path.abspath(frame.f_locals["filename"])
52 return source_filename
54 def line_number_range(self, frame: FrameType) -> tuple[TLineNo, TLineNo]: 1abcdefghijklmnopqrstuvwxyzA
55 lineno = frame.f_locals["linenum"]
56 return lineno, lineno + 1
59class MyFileReporter(FileReporter): 1abcdefghijklmnopqrstuvwxyzA
60 """A goofy file reporter."""
62 def lines(self) -> set[TLineNo]: 1abcdefghijklmnopqrstuvwxyzA
63 # Goofy test arrangement: claim that the file has as many lines as the
64 # number in its name.
65 num = os.path.basename(self.filename).split(".")[0].split("_")[1] 1abcdefghijklmnopqrstuvwxyzA
66 return set(range(1, int(num) + 1)) 1abcdefghijklmnopqrstuvwxyzA
69def coverage_init( 1abcdefghijklmnopqrstuvwxyzA
70 reg: Plugins,
71 options: Any, # pylint: disable=unused-argument
72) -> None:
73 """Called by coverage to initialize the plugins here."""
74 reg.add_file_tracer(Plugin()) 1abcdefghijklmnopqrstuvwxyzA