taler-android

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

coverage.py (3351B)


      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 """Pull JaCoCo execution data out of a running terminal.
     15 
     16 The simulator drives the app from outside, so AGP's own instrumented-test
     17 coverage plumbing never runs.  Instead the "sim" build type carries the JaCoCo
     18 agent plus the PosSimHooks receiver, and this module asks the agent for what it
     19 has collected so far.  Data accumulates in the live process, so dumping is
     20 cheap and can be done between episodes; the file only has to be collected
     21 before the process dies.
     22 """
     23 
     24 from __future__ import annotations
     25 
     26 import subprocess
     27 from pathlib import Path
     28 
     29 from device import ADB, HOOKS, PKG, Device
     30 
     31 ACTION = f"{PKG}.sim.DUMP_COVERAGE"
     32 
     33 # Every jacoco .exec stream starts with a block header and this magic.
     34 MAGIC = b"\x01\xc0\xc0"
     35 
     36 
     37 def dump(dev: Device, out_path: str | Path, name: str = "coverage.ec") -> Path:
     38     """Dump coverage from the app and copy it off the device.
     39 
     40     Raises if the agent is missing, which means the APK under test was not
     41     built from the "sim" build type.
     42     """
     43     out = Path(out_path)
     44     out.parent.mkdir(parents=True, exist_ok=True)
     45     res = dev.shell(f"am broadcast -a {ACTION} -n {HOOKS} --es file {name}")
     46     if "Broadcast completed" not in res:
     47         raise RuntimeError(f"coverage broadcast not delivered: {res.strip()}")
     48     # run-as is the only way into the app's private files; the sim build is
     49     # debuggable, so it is allowed.
     50     data = subprocess.run(
     51         [ADB, "-s", dev.serial, "exec-out", "run-as", PKG, "cat", f"files/{name}"],
     52         capture_output=True, timeout=120,
     53     )
     54     if not data.stdout:
     55         raise RuntimeError(
     56             f"no coverage data (stderr: {data.stderr.decode(errors='replace').strip()}); "
     57             "is this the sim APK?")
     58     # A dump whose process was killed while writing leaves a truncated file,
     59     # and jacoco's `merge` aborts on the first one it cannot read -- losing
     60     # every other file's data with it.  So a dump that does not at least begin
     61     # like execution data never gets the final name.
     62     if not data.stdout.startswith(MAGIC):
     63         raise RuntimeError(
     64             f"coverage data for {name} is not jacoco execution data "
     65             f"({len(data.stdout)} bytes, starts {data.stdout[:8]!r}); "
     66             "the app was probably killed mid-dump")
     67     part = out.with_suffix(out.suffix + ".part")
     68     part.write_bytes(data.stdout)
     69     part.replace(out)
     70     return out
     71 
     72 
     73 if __name__ == "__main__":
     74     import sys
     75     d = Device(sys.argv[2] if len(sys.argv) > 2 else None)
     76     target = sys.argv[1] if len(sys.argv) > 1 \
     77         else "/opt/taler-pos-sim/run/coverage/manual.ec"
     78     p = dump(d, target)
     79     print(f"{p} ({p.stat().st_size} bytes)")