Debian Codegraph @

tasks/coverage_pragmas.py

raw
1 """Coverage exclusions keyed on the capability that code needs.
2
3 covdefaults keys its pragmas on ``os.name``, ``sys.platform`` and ``sys.implementation.name``, so code that cannot run
4 for a capability reason has to name a platform as a stand-in. The stand-in goes stale. A ``win32 no cover`` sat on
5 ``os.link``'s follow_symlinks fallback and demanded a branch modern Windows never takes, and the same guess hid real
6 Windows gaps behind platform-agnostic code.
7
8 ``# pragma: needs <capability>`` drops out only where the capability is absent, and ``# pragma: lacks <capability>``
9 only where it is present. Both read the probes in :mod:`capabilities`, as do the tests' skipif gates, so a test cannot
10 skip while coverage still demands its lines.
11
12 List this after covdefaults in ``[tool.coverage] run.plugins``; both merge into the same options.
13 """
14
15 from __future__ import annotations
16
17 from typing import TYPE_CHECKING, Final, cast
18
19 from capabilities import CAPABILITIES
20 from coverage import CoveragePlugin
21
22 if TYPE_CHECKING:
23 from collections.abc import Iterable
24
25 from coverage.plugin_support import Plugins
26 from coverage.types import TConfigurable
27
28
29 # coverage passes the config's plugin options; this plugin takes none.
30 def coverage_init(reg: Plugins, options: dict[str, str]) -> None: # ruff:ignore[unused-function-argument]
31 reg.add_configurer(CapabilityPragmas())
32
33
34 class CapabilityPragmas(CoveragePlugin):
35 """Exclude ``needs``/``lacks`` capability pragmas on the runtimes where that code cannot run."""
36
37 def configure(self, config: TConfigurable) -> None:
38 excluded = [
39 rf"# pragma: {'lacks' if present else 'needs'} {name}\b" for name, present in sorted(CAPABILITIES.items())
40 ]
41 self._extend(config, "report:exclude_lines", [*excluded, *_ALWAYS_EXCLUDED])
42 # A guarded clause header only ever takes one arc, as covdefaults already assumes for its own pragmas.
43 self._extend(config, "report:partial_branches", [_CAPABILITY_PRAGMA, *_ALWAYS_EXCLUDED])
44 unrunnable = [
45 pattern
46 for name, patterns in sorted(_CAPABILITY_MODULES.items())
47 if not CAPABILITIES[name]
48 for pattern in patterns
49 ]
50 if unrunnable:
51 self._extend(config, "report:omit", unrunnable)
52
53 @staticmethod
54 def _extend(config: TConfigurable, option: str, patterns: list[str]) -> None:
55 # get_option is typed for every coverage option; these two always hold regexes.
56 merged: set[str] = set(cast("Iterable[str]", config.get_option(option) or ()))
57 merged.update(patterns)
58 config.set_option(option, sorted(merged))
59
60
61 #: Modules a missing capability makes unrunnable in full; marking every line would restate one module-level gate.
62 _CAPABILITY_MODULES: Final[dict[str, tuple[str, ...]]] = {
63 "hard-link": (
64 "*/tests/test_strict_soft*.py",
65 "*/tests\\test_strict_soft*.py",
66 "*/filelock/_strict.py",
67 "*\\filelock\\_strict.py",
68 ),
69 }
70
71 #: A forked child exits through os._exit without writing coverage data, so no job in the matrix can see these.
72 _ALWAYS_EXCLUDED: Final[tuple[str, ...]] = (r"# pragma: forked child\b",)
73
74 _CAPABILITY_PRAGMA: Final[str] = rf"# pragma: (needs|lacks) ({'|'.join(sorted(CAPABILITIES))})\b"
75
76 __all__ = [
77 "CAPABILITIES",
78 "coverage_init",
79 ]