taler-android

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

gaps.py (17142B)


      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 # The terminal has no @Preview annotations.  What it has instead is eight
     42 # `internal fun *ScreenContent` composables that render a screen from a fixture
     43 # -- and the only thing that calls them is `ScreenshotActivity`, which lives in
     44 # the `debug` source set.  `sim` is its own build type and does not include
     45 # that source set, so in the APK under test those functions have no caller at
     46 # all and no run can reach them.  Counting them as missed points the ranking at
     47 # work nobody can do: OrderScreenContent alone is 73 lines.
     48 def _body_lines(lines: list[str], decl_index: int) -> set[int]:
     49     """The 1-based line numbers of a declaration and its body."""
     50     start = decl_index
     51     # Back up over the annotations that belong to the declaration.
     52     while start and lines[start - 1].lstrip().startswith("@"):
     53         start -= 1
     54     marked: set[int] = set()
     55     depth, started, j = 0, False, start
     56     while j < len(lines):
     57         marked.add(j + 1)
     58         depth += lines[j].count("{") - lines[j].count("}")
     59         if "{" in lines[j]:
     60             started = True
     61         if started and depth <= 0:
     62             break
     63         j += 1
     64     return marked
     65 
     66 
     67 # Functions that exist in `src/main` and that *nothing* calls -- not the app,
     68 # not the debug source set, not a unit test.  Reported upstream as
     69 # bugs.gnunet.org 11981 rather than quietly skipped here, because a tool
     70 # special-casing code nobody else can see is how a gap stays hidden.
     71 #
     72 # `refundExternalPayment` is deliberately *not* in this list: six cases in
     73 # CheckoutManagerTest call it, so it is covered by the unit-test half of the
     74 # report even though no UI reaches it.  Excluding it would hide a real
     75 # difference between "tested" and "reachable".
     76 DEAD_FUNCTIONS = {
     77     "OrderManager.kt": ("deleteCurrentOrder",),
     78     "CheckoutManager.kt": ("newPaymentId",),
     79 }
     80 
     81 
     82 def dead_lines(repo: Path) -> dict[str, set[int]]:
     83     """Lines of functions with no caller anywhere.  See bugs.gnunet.org 11981."""
     84     import re
     85     out: dict[str, set[int]] = {}
     86     base = repo / "merchant-terminal/src/main/java"
     87     if not base.is_dir():
     88         return out
     89     for path in base.rglob("*.kt"):
     90         names = DEAD_FUNCTIONS.get(path.name)
     91         if not names:
     92             continue
     93         lines = path.read_text(errors="replace").splitlines()
     94         marked: set[int] = set()
     95         for i, line in enumerate(lines):
     96             for name in names:
     97                 if re.search(rf"\bfun\s+{re.escape(name)}\s*\(", line):
     98                     marked |= _body_lines(lines, i)
     99         if marked:
    100             out[path.name] = out.get(path.name, set()) | marked
    101     return out
    102 
    103 
    104 # Compose composables whose only caller is `ScreenshotActivity` and friends in
    105 # the `debug` source set.  `sim` is its own build type and does not include
    106 # that source set, so in the APK under test they have no caller at all and no
    107 # run can reach them.  Counting them as missed points the ranking at work
    108 # nobody can do: `OrderScreenContent` and its lambdas alone are 76 lines.
    109 def debug_only_lines(repo: Path) -> dict[str, set[int]]:
    110     """Lines of functions in `src/main` that only the debug source set calls.
    111 
    112     Matched on the declaration rather than on a naming convention, and on
    113     *any* modifiers rather than `internal` alone: the previous version keyed
    114     on `^internal fun`, required the name to appear exactly once in main, and
    115     so discounted 205 lines where the twelve `*ScreenContent` composables and
    116     their lambdas come to nearly three hundred.  A method reference
    117     (`::name`) counts as a call, which the old version also missed.
    118     """
    119     import re
    120     out: dict[str, set[int]] = {}
    121     for module in ("merchant-terminal",):
    122         main = repo / module / "src/main/java"
    123         debug = repo / module / "src/debug/java"
    124         if not main.is_dir() or not debug.is_dir():
    125             continue
    126         main_text = {q: q.read_text(errors="replace") for q in main.rglob("*.kt")}
    127         debug_text = "\n".join(q.read_text(errors="replace")
    128                                 for q in debug.rglob("*.kt"))
    129         decl = re.compile(r"^\s*(?:(?:internal|private|public|inline|suspend)\s+)*"
    130                           r"fun\s+(\w+)\s*\(", re.M)
    131         for path, text in main_text.items():
    132             lines = text.splitlines()
    133             marked: set[int] = set()
    134             for m in decl.finditer(text):
    135                 name = m.group(1)
    136                 # Any mention at all, not `name(`: a Compose call with a
    137                 # trailing lambda has no parentheses (`PosTheme { ... }`), and
    138                 # keying on the parenthesis marked the app's own theme wrapper
    139                 # as debug-only -- twelve lines of live code that would have
    140                 # been quietly dropped from the denominator.  Erring towards
    141                 # counting a comment as a use is the right way to be wrong
    142                 # here: under-discounting leaves work on the list, while
    143                 # over-discounting hides it.
    144                 used = re.compile(rf"\b{re.escape(name)}\b")
    145                 # Every mention in main, minus the declarations themselves.
    146                 mentions = sum(len(used.findall(t)) for t in main_text.values())
    147                 decls = sum(len(re.findall(rf"\bfun\s+{re.escape(name)}\s*\(", t))
    148                             for t in main_text.values())
    149                 if mentions > decls or not used.search(debug_text):
    150                     continue
    151                 marked |= _body_lines(lines, text[:m.start()].count("\n"))
    152             if marked:
    153                 out[path.name] = out.get(path.name, set()) | marked
    154     return out
    155 
    156 
    157 def preview_lines(repo: Path) -> dict[str, set[int]]:
    158     """Line numbers no instrumented run can reach, per source file name.
    159 
    160     Two sources: an @Preview body, which exists for Android Studio's preview
    161     pane and which nothing calls; and a fixture composable that only the debug
    162     source set calls (see `debug_only_lines`).  The name is historical -- the
    163     terminal has none of the former and eight of the latter.
    164     """
    165     out: dict[str, set[int]] = {}
    166     # `merchant-terminal`, not `terminal` -- the name was wrong and nothing
    167     # matched.  It makes no difference to the figures: the terminal carries no
    168     # @Preview annotations at all, so none of its missed lines were ever being
    169     # excluded.  What looks like a preview block at the end of five of its
    170     # files is the phone half of a screen that picks its layout at runtime,
    171     # which an instrumented run reaches on a phone-sized device and not
    172     # otherwise.
    173     for tree in ("merchant-terminal/src/main/java",
    174                  "merchant-lib/src/main/java",
    175                  "taler-kotlin-android/src/main/java"):
    176         base = repo / tree
    177         if not base.is_dir():
    178             continue
    179         for path in base.rglob("*.kt"):
    180             lines = path.read_text(errors="replace").splitlines()
    181             marked: set[int] = set()
    182             i = 0
    183             while i < len(lines):
    184                 if "@Preview" in lines[i]:
    185                     depth, started, j = 0, False, i
    186                     while j < len(lines):
    187                         marked.add(j + 1)
    188                         depth += lines[j].count("{") - lines[j].count("}")
    189                         if "{" in lines[j]:
    190                             started = True
    191                         if started and depth <= 0:
    192                             break
    193                         j += 1
    194                     i = j
    195                 i += 1
    196             if marked:
    197                 out[path.name] = marked
    198     for name, marked in debug_only_lines(repo).items():
    199         out[name] = out.get(name, set()) | marked
    200     for name, marked in dead_lines(repo).items():
    201         out[name] = out.get(name, set()) | marked
    202     return out
    203 
    204 
    205 def source_misses(path: str, skip: dict[str, set[int]]) -> tuple[int, int, int]:
    206     """(missed, covered, missed-inside-previews) counted line by line."""
    207     root = ET.parse(path).getroot()
    208     missed = covered = in_preview = 0
    209     for pkg in root.findall("package"):
    210         for sf in pkg.findall("sourcefile"):
    211             skipped = skip.get(sf.get("name") or "", ())
    212             for line in sf.findall("line"):
    213                 nr = int(line.get("nr", 0))
    214                 mi, ci = int(line.get("mi", 0)), int(line.get("ci", 0))
    215                 if ci:
    216                     covered += 1
    217                 elif mi:
    218                     missed += 1
    219                     if nr in skipped:
    220                         in_preview += 1
    221     return missed, covered, in_preview
    222 
    223 
    224 def reachable_misses_by_package(path: str, skip: dict[str, set[int]]) -> dict[str, int]:
    225     """Missed lines per package, not counting the ones inside @Preview bodies.
    226 
    227     Ranking packages by raw misses points at work that cannot be done: the
    228     peer package looked like the third-biggest gap in the report and is mostly
    229     four preview functions in one file.
    230     """
    231     root = ET.parse(path).getroot()
    232     out: dict[str, int] = defaultdict(int)
    233     for pkg in root.findall("package"):
    234         pname = (pkg.get("name") or "").replace("/", ".")
    235         for sf in pkg.findall("sourcefile"):
    236             skipped = skip.get(sf.get("name") or "", ())
    237             for line in sf.findall("line"):
    238                 if int(line.get("ci", 0)):
    239                     continue
    240                 if not int(line.get("mi", 0)):
    241                     continue
    242                 if int(line.get("nr", 0)) not in skipped:
    243                     out[pname] += 1
    244     return dict(out)
    245 
    246 
    247 @dataclass
    248 class Unit:
    249     missed: int = 0
    250     covered: int = 0
    251 
    252     @property
    253     def total(self) -> int:
    254         return self.missed + self.covered
    255 
    256     @property
    257     def pct(self) -> float:
    258         return 100.0 * self.covered / self.total if self.total else 100.0
    259 
    260 
    261 def counter(el: ET.Element, kind: str = "LINE") -> tuple[int, int]:
    262     for c in el.findall("counter"):
    263         if c.get("type") == kind:
    264             return int(c.get("missed", 0)), int(c.get("covered", 0))
    265     return 0, 0
    266 
    267 
    268 def read(path: str) -> tuple[dict[str, Unit], dict[str, Unit], Unit]:
    269     root = ET.parse(path).getroot()
    270     packages: dict[str, Unit] = defaultdict(Unit)
    271     classes: dict[str, Unit] = defaultdict(Unit)
    272     overall = Unit()
    273     m, c = counter(root)
    274     overall.missed, overall.covered = m, c
    275     for pkg in root.findall("package"):
    276         pname = (pkg.get("name") or "").replace("/", ".")
    277         pm, pc = counter(pkg)
    278         packages[pname].missed += pm
    279         packages[pname].covered += pc
    280         for cls in pkg.findall("class"):
    281             cname = (cls.get("name") or "").replace("/", ".")
    282             # Fold Compose's synthetic classes into the file they came from.
    283             owner = re.split(SYNTHETIC, cname)[0] or cname
    284             cm, cc = counter(cls)
    285             classes[owner].missed += cm
    286             classes[owner].covered += cc
    287     return dict(packages), dict(classes), overall
    288 
    289 
    290 def main() -> int:
    291     if len(sys.argv) < 2:
    292         print(__doc__)
    293         return 2
    294     packages, classes, overall = read(sys.argv[1])
    295     print(f"overall lines: {overall.covered}/{overall.total} ({overall.pct:.1f}%)")
    296     # What of the shortfall is even reachable.
    297     repo = Path(__file__).resolve().parent.parent.parent
    298     try:
    299         missed, covered, in_preview = source_misses(sys.argv[1], preview_lines(repo))
    300     except Exception:  # noqa: BLE001
    301         in_preview = 0
    302     in_dead = 0
    303     if "--dead" in sys.argv:
    304         # Opt-in: the dead-code search takes a few minutes, which is too slow
    305         # for a report meant to be read while a round runs.
    306         try:
    307             sys.path.insert(0, str(Path(__file__).resolve().parent))
    308             import deadcode
    309             found, _ = deadcode.dead(repo, deadcode.MODULES["terminal"])
    310             dead_by_file: dict[str, set[int]] = defaultdict(set)
    311             for d in found:
    312                 dead_by_file[d.path.name].update(range(d.start, d.end + 1))
    313             root = ET.parse(sys.argv[1]).getroot()
    314             previews = preview_lines(repo)
    315             for pkg in root.findall("package"):
    316                 for sf in pkg.findall("sourcefile"):
    317                     name = sf.get("name") or ""
    318                     pv = previews.get(name, set())
    319                     dd = dead_by_file.get(name, set())
    320                     for line in sf.findall("line"):
    321                         nr = int(line.get("nr", 0))
    322                         if int(line.get("ci", 0)) or not int(line.get("mi", 0)):
    323                             continue
    324                         if nr in dd and nr not in pv:
    325                             in_dead += 1
    326         except Exception as e:  # noqa: BLE001
    327             print(f"  (dead-code pass failed: {e})")
    328     if in_preview:
    329         reachable = covered + missed - in_preview - in_dead
    330         print(f"  of the {missed} missed lines, {in_preview} are in bodies "
    331               f"this build type cannot reach at all")
    332         if in_dead:
    333             print(f"  of the rest, {in_dead} are in declarations nothing "
    334                   f"refers to (deadcode.py)")
    335         print(f"  reachable lines covered: {covered}/{reachable} "
    336               f"({100.0 * covered / reachable:.1f}%); the highest this report "
    337               f"can ever read is {100.0 * reachable / (covered + missed):.1f}%")
    338         if not in_dead:
    339             print("  (pass --dead to discount unreachable declarations too)")
    340     print()
    341 
    342     if "--since" in sys.argv:
    343         before_path = sys.argv[sys.argv.index("--since") + 1]
    344         bp, bc, bo = read(before_path)
    345         print(f"since {before_path}: {bo.covered} -> {overall.covered} lines "
    346               f"({bo.pct:.1f}% -> {overall.pct:.1f}%)\n")
    347         moved = sorted(
    348             ((name, u.covered - bc.get(name, Unit()).covered, u)
    349              for name, u in classes.items()),
    350             key=lambda row: -row[1],
    351         )
    352         print("newly covered (top 15):")
    353         for name, delta, u in moved[:15]:
    354             if delta <= 0:
    355                 break
    356             print(f"  +{delta:5} {u.pct:5.1f}%  {name.split('.')[-1]}")
    357         print()
    358 
    359     try:
    360         reachable_by_pkg = reachable_misses_by_package(sys.argv[1], preview_lines(repo))
    361     except Exception:  # noqa: BLE001
    362         reachable_by_pkg = {}
    363 
    364     print("packages by missed lines that can actually be reached:")
    365     ranked = sorted(packages.items(),
    366                     key=lambda kv: -reachable_by_pkg.get(kv[0], kv[1].missed))
    367     for name, u in ranked[:18]:
    368         short = name.replace("net.taler.merchantpos", "~").replace("net.taler.common", "common")
    369         reach = reachable_by_pkg.get(name)
    370         extra = "" if reach is None else f"  ({u.missed} counting unreachable)"
    371         print(f"  {reach if reach is not None else u.missed:6} missed  "
    372               f"{u.pct:5.1f}%  {short}{extra}")
    373 
    374     print("\nuntouched classes with the most lines (0% covered):")
    375     untouched = [(n, u) for n, u in classes.items() if u.covered == 0 and u.total >= 12]
    376     for name, u in sorted(untouched, key=lambda kv: -kv[1].missed)[:25]:
    377         print(f"  {u.missed:6} lines  {name}")
    378 
    379     print("\npartly covered, biggest remainder:")
    380     partial = [(n, u) for n, u in classes.items() if u.covered > 0 and u.missed >= 25]
    381     for name, u in sorted(partial, key=lambda kv: -kv[1].missed)[:20]:
    382         print(f"  {u.missed:6} missed  {u.pct:5.1f}%  {name.split('.')[-1]}")
    383     return 0
    384 
    385 
    386 if __name__ == "__main__":
    387     sys.exit(main())