gaps.py (10473B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """Where the simulator has not been yet. 15 16 Reads the JaCoCo XML report and ranks what is still untouched, so the next 17 iteration can aim at a feature rather than at random. Compose generates a lot 18 of synthetic classes, so lambdas and `*ComposableSingletons` are folded into 19 their owner and the ranking is by missed *lines* per package and per class. 20 21 python3 gaps.py out/coverage/coverage.xml # ranking 22 python3 gaps.py out/coverage/coverage.xml --since old.xml # what moved 23 """ 24 25 from __future__ import annotations 26 27 import re 28 import sys 29 import xml.etree.ElementTree as ET 30 from pathlib import Path 31 from collections import defaultdict 32 from dataclasses import dataclass, field 33 34 SYNTHETIC = re.compile(r"\$\$|ComposableSingletons|\$lambda|\$WhenMappings|Kt\$\d") 35 36 37 # Lines inside an @Preview body cannot be covered by an instrumented app run: 38 # the function exists for Android Studio's preview pane and nothing calls it. 39 # They are 27 % of everything this report calls "missed", so counting them 40 # makes every ranking point at work that cannot be done. 41 def preview_lines(repo: Path) -> dict[str, set[int]]: 42 """Line numbers belonging to @Preview functions, per source file name.""" 43 out: dict[str, set[int]] = {} 44 for tree in ("wallet/src/main/java", "taler-kotlin-android/src/main/java"): 45 base = repo / tree 46 if not base.is_dir(): 47 continue 48 for path in base.rglob("*.kt"): 49 lines = path.read_text(errors="replace").splitlines() 50 marked: set[int] = set() 51 i = 0 52 while i < len(lines): 53 if "@Preview" in lines[i]: 54 depth, started, j = 0, False, i 55 while j < len(lines): 56 marked.add(j + 1) 57 depth += lines[j].count("{") - lines[j].count("}") 58 if "{" in lines[j]: 59 started = True 60 if started and depth <= 0: 61 break 62 j += 1 63 i = j 64 i += 1 65 if marked: 66 out[path.name] = marked 67 return out 68 69 70 def source_misses(path: str, skip: dict[str, set[int]]) -> tuple[int, int, int]: 71 """(missed, covered, missed-inside-previews) counted line by line.""" 72 root = ET.parse(path).getroot() 73 missed = covered = in_preview = 0 74 for pkg in root.findall("package"): 75 for sf in pkg.findall("sourcefile"): 76 skipped = skip.get(sf.get("name") or "", ()) 77 for line in sf.findall("line"): 78 nr = int(line.get("nr", 0)) 79 mi, ci = int(line.get("mi", 0)), int(line.get("ci", 0)) 80 if ci: 81 covered += 1 82 elif mi: 83 missed += 1 84 if nr in skipped: 85 in_preview += 1 86 return missed, covered, in_preview 87 88 89 def reachable_misses_by_package(path: str, skip: dict[str, set[int]]) -> dict[str, int]: 90 """Missed lines per package, not counting the ones inside @Preview bodies. 91 92 Ranking packages by raw misses points at work that cannot be done: the 93 peer package looked like the third-biggest gap in the report and is mostly 94 four preview functions in one file. 95 """ 96 root = ET.parse(path).getroot() 97 out: dict[str, int] = defaultdict(int) 98 for pkg in root.findall("package"): 99 pname = (pkg.get("name") or "").replace("/", ".") 100 for sf in pkg.findall("sourcefile"): 101 skipped = skip.get(sf.get("name") or "", ()) 102 for line in sf.findall("line"): 103 if int(line.get("ci", 0)): 104 continue 105 if not int(line.get("mi", 0)): 106 continue 107 if int(line.get("nr", 0)) not in skipped: 108 out[pname] += 1 109 return dict(out) 110 111 112 @dataclass 113 class Unit: 114 missed: int = 0 115 covered: int = 0 116 117 @property 118 def total(self) -> int: 119 return self.missed + self.covered 120 121 @property 122 def pct(self) -> float: 123 return 100.0 * self.covered / self.total if self.total else 100.0 124 125 126 def counter(el: ET.Element, kind: str = "LINE") -> tuple[int, int]: 127 for c in el.findall("counter"): 128 if c.get("type") == kind: 129 return int(c.get("missed", 0)), int(c.get("covered", 0)) 130 return 0, 0 131 132 133 def read(path: str) -> tuple[dict[str, Unit], dict[str, Unit], Unit]: 134 root = ET.parse(path).getroot() 135 packages: dict[str, Unit] = defaultdict(Unit) 136 classes: dict[str, Unit] = defaultdict(Unit) 137 overall = Unit() 138 m, c = counter(root) 139 overall.missed, overall.covered = m, c 140 for pkg in root.findall("package"): 141 pname = (pkg.get("name") or "").replace("/", ".") 142 pm, pc = counter(pkg) 143 packages[pname].missed += pm 144 packages[pname].covered += pc 145 for cls in pkg.findall("class"): 146 cname = (cls.get("name") or "").replace("/", ".") 147 # Fold Compose's synthetic classes into the file they came from. 148 owner = re.split(SYNTHETIC, cname)[0] or cname 149 cm, cc = counter(cls) 150 classes[owner].missed += cm 151 classes[owner].covered += cc 152 return dict(packages), dict(classes), overall 153 154 155 def main() -> int: 156 if len(sys.argv) < 2: 157 print(__doc__) 158 return 2 159 packages, classes, overall = read(sys.argv[1]) 160 print(f"overall lines: {overall.covered}/{overall.total} ({overall.pct:.1f}%)") 161 # What of the shortfall is even reachable. 162 repo = Path(__file__).resolve().parent.parent.parent 163 try: 164 missed, covered, in_preview = source_misses(sys.argv[1], preview_lines(repo)) 165 except Exception: # noqa: BLE001 166 in_preview = 0 167 in_dead = 0 168 if "--dead" in sys.argv: 169 # Opt-in: the dead-code search takes a few minutes, which is too slow 170 # for a report meant to be read while a round runs. 171 try: 172 sys.path.insert(0, str(Path(__file__).resolve().parent)) 173 import deadcode 174 found, _ = deadcode.dead(repo, deadcode.MODULES["wallet"]) 175 dead_by_file: dict[str, set[int]] = defaultdict(set) 176 for d in found: 177 dead_by_file[d.path.name].update(range(d.start, d.end + 1)) 178 root = ET.parse(sys.argv[1]).getroot() 179 previews = preview_lines(repo) 180 for pkg in root.findall("package"): 181 for sf in pkg.findall("sourcefile"): 182 name = sf.get("name") or "" 183 pv = previews.get(name, set()) 184 dd = dead_by_file.get(name, set()) 185 for line in sf.findall("line"): 186 nr = int(line.get("nr", 0)) 187 if int(line.get("ci", 0)) or not int(line.get("mi", 0)): 188 continue 189 if nr in dd and nr not in pv: 190 in_dead += 1 191 except Exception as e: # noqa: BLE001 192 print(f" (dead-code pass failed: {e})") 193 if in_preview: 194 reachable = covered + missed - in_preview - in_dead 195 print(f" of the {missed} missed lines, {in_preview} are inside @Preview " 196 f"bodies and cannot run at all") 197 if in_dead: 198 print(f" of the rest, {in_dead} are in declarations nothing " 199 f"refers to (deadcode.py)") 200 print(f" reachable lines covered: {covered}/{reachable} " 201 f"({100.0 * covered / reachable:.1f}%); the highest this report " 202 f"can ever read is {100.0 * reachable / (covered + missed):.1f}%") 203 if not in_dead: 204 print(" (pass --dead to discount unreachable declarations too)") 205 print() 206 207 if "--since" in sys.argv: 208 before_path = sys.argv[sys.argv.index("--since") + 1] 209 bp, bc, bo = read(before_path) 210 print(f"since {before_path}: {bo.covered} -> {overall.covered} lines " 211 f"({bo.pct:.1f}% -> {overall.pct:.1f}%)\n") 212 moved = sorted( 213 ((name, u.covered - bc.get(name, Unit()).covered, u) 214 for name, u in classes.items()), 215 key=lambda row: -row[1], 216 ) 217 print("newly covered (top 15):") 218 for name, delta, u in moved[:15]: 219 if delta <= 0: 220 break 221 print(f" +{delta:5} {u.pct:5.1f}% {name.split('.')[-1]}") 222 print() 223 224 try: 225 reachable_by_pkg = reachable_misses_by_package(sys.argv[1], preview_lines(repo)) 226 except Exception: # noqa: BLE001 227 reachable_by_pkg = {} 228 229 print("packages by missed lines that can actually be reached:") 230 ranked = sorted(packages.items(), 231 key=lambda kv: -reachable_by_pkg.get(kv[0], kv[1].missed)) 232 for name, u in ranked[:18]: 233 short = name.replace("net.taler.wallet", "~").replace("net.taler.common", "common") 234 reach = reachable_by_pkg.get(name) 235 extra = "" if reach is None else f" ({u.missed} counting previews)" 236 print(f" {reach if reach is not None else u.missed:6} missed " 237 f"{u.pct:5.1f}% {short}{extra}") 238 239 print("\nuntouched classes with the most lines (0% covered):") 240 untouched = [(n, u) for n, u in classes.items() if u.covered == 0 and u.total >= 12] 241 for name, u in sorted(untouched, key=lambda kv: -kv[1].missed)[:25]: 242 print(f" {u.missed:6} lines {name}") 243 244 print("\npartly covered, biggest remainder:") 245 partial = [(n, u) for n, u in classes.items() if u.covered > 0 and u.missed >= 25] 246 for name, u in sorted(partial, key=lambda kv: -kv[1].missed)[:20]: 247 print(f" {u.missed:6} missed {u.pct:5.1f}% {name.split('.')[-1]}") 248 return 0 249 250 251 if __name__ == "__main__": 252 sys.exit(main())