taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

summary_html.py (18988B)


      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 """A landing page for a coverage run, over the top of JaCoCo's own report.
     15 
     16 JaCoCo already writes the lcov-genhtml equivalent -- a source-annotated page
     17 per class, green and red down the gutter.  What it cannot write is the part
     18 that needs this harness's own knowledge:
     19 
     20 * **what is actually reachable.**  JaCoCo paints the screenshot-only
     21   composables and the dead functions of bugs.gnunet.org 11981 the same red as
     22   everything else, so its totals and `gaps.py`'s disagree by a couple of
     23   points and a reader cannot tell which lines are worth anyone's time;
     24 * **what produced the figures** -- how many execution-data files, from which
     25   scenarios, against which commit.  A coverage number with no provenance is a
     26   number somebody will quote six weeks later without knowing it came from a
     27   half-finished run.
     28 
     29 So this writes `index.html` beside JaCoCo's, links into it, and says both.
     30 
     31     summary_html.py <out-dir> [ec-dir] [--title NAME] [--also-gaps DIR]
     32 
     33 `--also-gaps` names another harness's `coverage/` directory, whose `gaps.py`
     34 is asked for the lines *its* app cannot reach.  Run over one module's report
     35 nothing needs it; run repo-wide from CI, where the wallet's execution data is
     36 in the same report, the wallet's @Preview bodies have to come off the
     37 denominator too or the page calls Android Studio's preview pane uncovered
     38 work.
     39 
     40 One sharp edge in that: these maps are keyed by source *basename*, so two
     41 modules with a file of the same name share an entry.  Five names collide
     42 today (MainActivity.kt, MainViewModel.kt, Order.kt, PaymentManager.kt,
     43 RefundManager.kt) and none of them carries a discounted line in either
     44 module, so nothing is wrong now -- but adding an @Preview to one of those
     45 five would discount the same line numbers in the other module's file too.
     46 """
     47 
     48 from __future__ import annotations
     49 
     50 import html
     51 import importlib.util
     52 import subprocess
     53 import sys
     54 import time
     55 import xml.etree.ElementTree as ET
     56 from collections import defaultdict
     57 from pathlib import Path
     58 
     59 sys.path.insert(0, str(Path(__file__).resolve().parent))
     60 
     61 import gaps
     62 
     63 REPO = Path(__file__).resolve().parent.parent.parent
     64 KINDS = ("instruction", "branch", "line", "complexity", "method", "class")
     65 
     66 
     67 def counters(path: Path) -> dict[str, tuple[int, int]]:
     68     root = ET.parse(path).getroot()
     69     return {c.get("type", "").lower(): (int(c.get("covered", 0)),
     70                                         int(c.get("missed", 0)))
     71             for c in root.findall("counter")}
     72 
     73 
     74 def scenarios_from(ec_dir: Path | None) -> list[str]:
     75     """Which scenarios contributed execution data, from the file names."""
     76     if ec_dir is None or not ec_dir.is_dir():
     77         return []
     78     import re
     79     names = set()
     80     for f in ec_dir.glob("*.ec"):
     81         # "<scenario>-NN-<label>.ec": the sequence number and whatever the
     82         # dump was called.  Stripping only the two labels this harness
     83         # happened to use listed `login_url_shapes-02` beside
     84         # `login_url_shapes`, and every exploration step as its own scenario.
     85         stem = re.sub(r"-\d{2}(-[\w-]+)?$", "", f.stem)
     86         # The wallet harness names its episodes
     87         # "suite-w0-<stamp>-<scenario>", so without this the repo-wide page
     88         # lists a worker number and a unix timestamp as the scenario's name.
     89         stem = re.sub(r"^(suite|qr|explore)-(w\d+|seed\d+)-\d+-", "", stem)
     90         names.add(stem)
     91     return sorted(names)
     92 
     93 
     94 def gaps_of(coverage_dir: Path):
     95     """Another harness's `gaps.py`, loaded from its path.
     96 
     97     By path and under a name of its own, because every simulator calls the
     98     module `gaps` -- importing a second one normally would hand back the first
     99     out of sys.modules and silently discount the wrong app's lines.
    100     """
    101     module_path = coverage_dir / "gaps.py"
    102     if not module_path.is_file():
    103         print(f"no gaps.py under {coverage_dir}", file=sys.stderr)
    104         return None
    105     name = f"gaps_{coverage_dir.parent.name.replace('-', '_')}"
    106     spec = importlib.util.spec_from_file_location(name, module_path)
    107     if spec is None or spec.loader is None:
    108         return None
    109     module = importlib.util.module_from_spec(spec)
    110     sys.modules[name] = module
    111     spec.loader.exec_module(module)
    112     return module
    113 
    114 
    115 def commit() -> str:
    116     try:
    117         out = subprocess.run(["git", "-C", str(REPO), "log", "-1",
    118                               "--format=%h %s"], capture_output=True,
    119                              text=True, timeout=10)
    120         return out.stdout.strip() or "unknown"
    121     except Exception:
    122         return "unknown"
    123 
    124 
    125 def top_uncovered(path: Path, skip: dict[str, set[int]], limit: int = 25):
    126     """The biggest wholly-uncovered methods that are not known-unreachable."""
    127     root = ET.parse(path).getroot()
    128     rows = []
    129     for pkg in root.findall("package"):
    130         for cls in pkg.findall("class"):
    131             source = cls.get("sourcefilename") or ""
    132             skipped = skip.get(source, set())
    133             for m in cls.findall("method"):
    134                 c = m.find("counter[@type='LINE']")
    135                 if c is None:
    136                     continue
    137                 missed, covered = int(c.get("missed", 0)), int(c.get("covered", 0))
    138                 if covered or missed < 4:
    139                     continue
    140                 first = int(m.get("line", 0) or 0)
    141                 if first and first in skipped:
    142                     continue          # a body this build type cannot enter
    143                 rows.append((missed,
    144                              cls.get("name", "").rsplit("/", 1)[-1],
    145                              m.get("name", ""), source))
    146     rows.sort(reverse=True)
    147     return rows[:limit]
    148 
    149 
    150 def bar(pct: float) -> str:
    151     good = "var(--ok)" if pct >= 80 else ("var(--mid)" if pct >= 55 else "var(--bad)")
    152     return (f'<span class="bar"><span style="width:{pct:.1f}%;'
    153             f'background:{good}"></span></span>')
    154 
    155 
    156 def main(argv: list[str]) -> int:
    157     title = "POS simulator"
    158     if "--title" in argv:
    159         i = argv.index("--title")
    160         title = argv[i + 1]
    161         del argv[i:i + 2]
    162     also: list[Path] = []
    163     while "--also-gaps" in argv:
    164         i = argv.index("--also-gaps")
    165         raw = Path(argv[i + 1])
    166         also.append(raw if raw.is_absolute() else REPO / raw)
    167         del argv[i:i + 2]
    168     out_dir = Path(argv[0]) if argv else Path("out/coverage")
    169     ec_dir = Path(argv[1]) if len(argv) > 1 else None
    170     sim_xml = out_dir / "coverage.xml"
    171     if not sim_xml.exists():
    172         print(f"no coverage.xml in {out_dir}", file=sys.stderr)
    173         return 1
    174     union_xml = out_dir / "coverage-with-units.xml"
    175     has_union = union_xml.exists()
    176 
    177     skip = gaps.preview_lines(REPO)
    178     for coverage_dir in also:
    179         other = gaps_of(coverage_dir)
    180         if other is None:
    181             continue
    182         for name, lines in other.preview_lines(REPO).items():
    183             skip[name] = skip.get(name, set()) | lines
    184     missed, covered, unreachable = gaps.source_misses(str(sim_xml), skip)
    185     reachable_total = covered + missed - unreachable
    186     reachable_pct = 100 * covered / reachable_total if reachable_total else 0.0
    187 
    188     sim = counters(sim_xml)
    189     union = counters(union_xml) if has_union else {}
    190     by_pkg = gaps.reachable_misses_by_package(str(sim_xml), skip)
    191     raw_pkg: dict[str, int] = defaultdict(int)
    192     root = ET.parse(sim_xml).getroot()
    193     pkg_cov: dict[str, tuple[int, int]] = {}
    194     for pkg in root.findall("package"):
    195         name = (pkg.get("name") or "").replace("/", ".")
    196         c = pkg.find("counter[@type='LINE']")
    197         if c is not None:
    198             pkg_cov[name] = (int(c.get("covered", 0)), int(c.get("missed", 0)))
    199             raw_pkg[name] = int(c.get("missed", 0))
    200 
    201     dead = sum(len(v) for v in gaps.dead_lines(REPO).values())
    202     debug_only = sum(len(v) for v in gaps.debug_only_lines(REPO).values())
    203     # Whatever is in the skip map and in neither of those two buckets is an
    204     # @Preview body -- the wallet's, when a repo-wide run passed --also-gaps.
    205     marked_total = sum(len(v) for v in skip.values())
    206     previews = max(0, marked_total - dead - debug_only)
    207     scenarios = scenarios_from(ec_dir)
    208     ec_count = len(list(ec_dir.glob("*.ec"))) if ec_dir and ec_dir.is_dir() else 0
    209 
    210     def pct(pair: tuple[int, int]) -> float:
    211         cov, mis = pair
    212         return 100 * cov / (cov + mis) if cov + mis else 0.0
    213 
    214     rows_counters = []
    215     for kind in KINDS:
    216         if kind not in sim:
    217             continue
    218         cov, mis = sim[kind]
    219         cell_union = ""
    220         if has_union and kind in union:
    221             ucov, umis = union[kind]
    222             cell_union = (f"<td class='num'>{ucov:,}/{ucov + umis:,}</td>"
    223                           f"<td class='num'>{pct(union[kind]):.1f}%</td>"
    224                           f"<td class='num delta'>+{ucov - cov:,}</td>")
    225         rows_counters.append(
    226             f"<tr><th scope='row'>{kind}</th>"
    227             f"<td class='num'>{cov:,}/{cov + mis:,}</td>"
    228             f"<td class='num'>{pct(sim[kind]):.1f}%</td>"
    229             f"<td>{bar(pct(sim[kind]))}</td>{cell_union}</tr>")
    230 
    231     rows_pkg = []
    232     for name in sorted(by_pkg, key=lambda k: -by_pkg[k]):
    233         short = name.replace("net.taler.merchantpos", "~")
    234         cov, mis = pkg_cov.get(name, (0, 0))
    235         p = pct((cov, mis))
    236         hidden = raw_pkg[name] - by_pkg[name]
    237         note = f"<span class='muted'>({hidden} unreachable)</span>" if hidden else ""
    238         rows_pkg.append(
    239             f"<tr><th scope='row'><a href='html/{html.escape(name)}/index.html'>"
    240             f"{html.escape(short)}</a></th>"
    241             f"<td class='num'>{by_pkg[name]:,}</td>"
    242             f"<td class='num'>{p:.1f}%</td><td>{bar(p)}</td><td>{note}</td></tr>")
    243 
    244     rows_methods = []
    245     for miss, cls, meth, source in top_uncovered(sim_xml, skip):
    246         rows_methods.append(
    247             f"<tr><td class='num'>{miss}</td>"
    248             f"<td><code>{html.escape(cls)}.{html.escape(meth)}</code></td>"
    249             f"<td class='muted'>{html.escape(source)}</td></tr>")
    250 
    251     scen_list = ("".join(f"<li>{html.escape(s)}</li>" for s in scenarios)
    252                  or "<li class='muted'>no scenario names in the execution data</li>")
    253     union_head = ("<th scope='col' colspan='3'>with unit tests</th>"
    254                   if has_union else "")
    255     union_sub = ("<th scope='col' class='num'>covered</th>"
    256                  "<th scope='col' class='num'>%</th>"
    257                  "<th scope='col' class='num'>gain</th>" if has_union else "")
    258     union_link = ("<a class='btn' href='html-with-units/index.html'>"
    259                   "JaCoCo report, with unit tests</a>" if has_union else "")
    260 
    261     # Cards only where they mean something.  Run repo-wide from CI there is no
    262     # union file and no execution data from an emulator, and a "--%" card
    263     # beside a "0 scenarios" one reads as a broken report rather than as a
    264     # report of something narrower.
    265     reach_card = ""
    266     if unreachable:
    267         reach_card = (f'<div class="card"><div class="big">{reachable_pct:.1f}%</div>'
    268                       f'<div class="lbl">of the {reachable_total:,} lines this '
    269                       f'build type can reach</div></div>')
    270     union_card = ""
    271     if has_union:
    272         union_card = (f'<div class="card"><div class="big">'
    273                       f'{pct(union.get("line", (0, 0))):.1f}%</div>'
    274                       f'<div class="lbl">lines, with the JVM unit tests '
    275                       f'merged</div></div>')
    276     scen_card = ""
    277     if ec_count:
    278         scen_card = (f'<div class="card"><div class="big">{ec_count}</div>'
    279                      f'<div class="lbl">execution-data files, '
    280                      f'{len(scenarios)} scenarios</div></div>')
    281 
    282     page = TEMPLATE.format(
    283         generated=time.strftime("%Y-%m-%d %H:%M:%S %Z"),
    284         commit=html.escape(commit()),
    285         line_pct=f"{pct(sim.get('line', (0, 0))):.1f}",
    286         line_cov=f"{sim.get('line', (0, 0))[0]:,}",
    287         line_tot=f"{sum(sim.get('line', (0, 0))):,}",
    288         unreachable=f"{unreachable:,}",
    289         debug_only=f"{debug_only:,}",
    290         dead=f"{dead:,}",
    291         marked=f"{marked_total:,}",
    292         previews=(f", and {previews:,} inside <code>@Preview</code> bodies, "
    293                   f"which exist for Android Studio's preview pane and which "
    294                   f"nothing calls" if previews else ""),
    295         rows_counters="".join(rows_counters),
    296         rows_pkg="".join(rows_pkg),
    297         rows_methods="".join(rows_methods) or
    298         "<tr><td colspan='3' class='muted'>nothing reachable above the threshold</td></tr>",
    299         provenance=("<h2>What produced this</h2>"
    300                     f"<ul class='cols'>{scen_list}</ul>" if scenarios else ""),
    301         union_head=union_head,
    302         union_sub=union_sub,
    303         union_link=union_link,
    304         title=html.escape(title),
    305         reach_card=reach_card,
    306         union_card=union_card,
    307         scen_card=scen_card,
    308     )
    309     target = out_dir / "index.html"
    310     target.write_text(page, encoding="utf-8")
    311     return 0
    312 
    313 
    314 TEMPLATE = """<!doctype html>
    315 <html lang="en">
    316 <meta charset="utf-8">
    317 <meta name="viewport" content="width=device-width, initial-scale=1">
    318 <title>{title} coverage</title>
    319 <style>
    320 :root {{
    321   --bg: #fbfbfd; --fg: #15171c; --muted: #5d6470; --line: #dfe3ea;
    322   --card: #ffffff; --accent: #0042b3;
    323   --ok: #1f8a4c; --mid: #b4830e; --bad: #b3261e;
    324   color-scheme: light dark;
    325 }}
    326 @media (prefers-color-scheme: dark) {{
    327   :root:not([data-theme="light"]) {{
    328     --bg: #11131a; --fg: #e8eaf0; --muted: #9aa3b2; --line: #272c38;
    329     --card: #171a22; --accent: #7aa2ff;
    330     --ok: #4cc47f; --mid: #e0ad3a; --bad: #ef6a60;
    331   }}
    332 }}
    333 :root[data-theme="dark"] {{
    334   --bg: #11131a; --fg: #e8eaf0; --muted: #9aa3b2; --line: #272c38;
    335   --card: #171a22; --accent: #7aa2ff;
    336   --ok: #4cc47f; --mid: #e0ad3a; --bad: #ef6a60;
    337 }}
    338 * {{ box-sizing: border-box; }}
    339 body {{
    340   margin: 0; background: var(--bg); color: var(--fg);
    341   font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    342 }}
    343 .wrap {{ max-width: 1040px; margin: 0 auto; padding: 32px 16px 72px; }}
    344 h1 {{ font-size: 1.6rem; margin: 0 0 4px; letter-spacing: -0.01em; }}
    345 h2 {{ font-size: 1.05rem; margin: 36px 0 10px; letter-spacing: -0.005em; }}
    346 p.sub {{ color: var(--muted); margin: 0 0 28px; }}
    347 .cards {{ display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }}
    348 .card {{ background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }}
    349 .card .big {{ font-size: 2rem; font-weight: 650; letter-spacing: -0.02em; }}
    350 .card .lbl {{ color: var(--muted); font-size: 0.82rem; }}
    351 table {{ width: 100%; border-collapse: collapse; background: var(--card);
    352          border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }}
    353 th, td {{ text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); }}
    354 thead th {{ font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em;
    355             color: var(--muted); font-weight: 600; }}
    356 tbody tr:last-child td, tbody tr:last-child th {{ border-bottom: 0; }}
    357 td.num, th.num {{ text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }}
    358 td.delta {{ color: var(--muted); }}
    359 .muted {{ color: var(--muted); }}
    360 code {{ font: 0.86em ui-monospace, SFMono-Regular, Menlo, monospace; }}
    361 a {{ color: var(--accent); }}
    362 .bar {{ display: block; width: 100%; min-width: 80px; height: 7px; border-radius: 4px;
    363         background: color-mix(in srgb, var(--line) 80%, transparent); overflow: hidden; }}
    364 .bar > span {{ display: block; height: 100%; }}
    365 .btn {{ display: inline-block; padding: 9px 14px; border-radius: 9px;
    366         border: 1px solid var(--line); background: var(--card);
    367         text-decoration: none; margin: 0 8px 8px 0; font-weight: 550; }}
    368 ul.cols {{ columns: 3 200px; margin: 0; padding-left: 1.1rem; color: var(--muted); font-size: .9rem; }}
    369 @media (max-width: 560px) {{ .wrap {{ padding: 20px 16px 56px; }} }}
    370 </style>
    371 <div class="wrap">
    372 <h1>{title} coverage</h1>
    373 <p class="sub">{generated} &middot; {commit}</p>
    374 
    375 <div class="cards">
    376   <div class="card"><div class="big">{line_pct}%</div>
    377     <div class="lbl">lines, {line_cov} of {line_tot}</div></div>
    378   {reach_card}{union_card}{scen_card}
    379 </div>
    380 
    381 <h2>Totals</h2>
    382 <table>
    383 <thead><tr><th scope="col">counter</th>
    384 <th scope="col" colspan="3">simulator</th>{union_head}</tr>
    385 <tr><th scope="col"></th>
    386 <th scope="col" class="num">covered</th><th scope="col" class="num">%</th>
    387 <th scope="col"></th>{union_sub}</tr></thead>
    388 <tbody>{rows_counters}</tbody>
    389 </table>
    390 
    391 <h2>Packages, ranked by what can still be reached</h2>
    392 <p class="sub">Lines inside screenshot-only composables and the dead functions of
    393 <a href="https://bugs.gnunet.org/view?id=11981">11981</a> are left out of the
    394 ranking: counting them points the next round at work nobody can do. JaCoCo's
    395 own pages below still paint them red, because JaCoCo does not know about them.</p>
    396 <table>
    397 <thead><tr><th scope="col">package</th><th scope="col" class="num">reachable missed</th>
    398 <th scope="col" class="num">covered</th><th scope="col"></th><th scope="col"></th></tr></thead>
    399 <tbody>{rows_pkg}</tbody>
    400 </table>
    401 
    402 <h2>Biggest untouched methods, known-unreachable ones removed</h2>
    403 <p class="sub">Still includes what needs hardware or a credential this build
    404 does not have &mdash; <code>sumup</code> wants an affiliate key and a card
    405 reader, <code>devices</code> a Bluetooth or USB display, and
    406 <code>reportNfcPayload</code> an NFC adapter no emulator provides. Those are
    407 reachable in principle and not by us.</p>
    408 <table>
    409 <thead><tr><th scope="col" class="num">lines</th><th scope="col">method</th>
    410 <th scope="col">file</th></tr></thead>
    411 <tbody>{rows_methods}</tbody>
    412 </table>
    413 
    414 <h2>Known unreachable</h2>
    415 <p class="sub">{marked} source lines are marked as impossible for a
    416 <code>sim</code> build to enter &mdash; {debug_only} in composables whose only
    417 caller is the <code>debug</code> source set, {dead} in functions with no caller
    418 at all (<a href="https://bugs.gnunet.org/view?id=11981">11981</a>){previews}. Of those,
    419 <strong>{unreachable}</strong> are lines JaCoCo counted as missed, and it is
    420 that figure which comes off the denominator above.</p>
    421 
    422 <h2>The full report</h2>
    423 <p><a class="btn" href="html/index.html">JaCoCo report, simulator only</a>
    424 {union_link}</p>
    425 
    426 {provenance}
    427 </div>
    428 </html>
    429 """
    430 
    431 
    432 if __name__ == "__main__":
    433     sys.exit(main(sys.argv[1:]))