report.sh (4378B)
1 #!/bin/bash 2 # This file is part of GNU Taler 3 # (C) 2026 Taler Systems S.A. 4 # 5 # GNU Taler is free software; you can redistribute it and/or modify it under the 6 # terms of the GNU General Public License as published by the Free Software 7 # Foundation; either version 3, or (at your option) any later version. 8 # 9 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License along with 14 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 # 16 # Turn collected .ec files into a coverage report. 17 # 18 # Runs on the *build* host, not on the emulator host: the report needs the 19 # uninstrumented class files and the sources, which only exist where the APK 20 # was built. Collect the .ec files first (driver/coverage.py on the emulator 21 # host, then rsync them here). 22 # 23 # Usage: report.sh <dir-with-.ec-files> [out-dir] 24 # 25 # One warning about the input: jacoco matches execution data to classes by a 26 # checksum of the class file, so data recorded by one APK is dropped (with 27 # "Execution data for class … does not match") when reported against a 28 # different build. Resources are enough to do it -- an added res/ file shifts 29 # the inlined R constants -- so rebuilding the APK starts a new coverage 30 # baseline, and .ec files from before it have to be reported separately or not 31 # at all. 32 33 set -eu 34 35 EC_DIR=${1:?usage: report.sh <dir-with-.ec-files> [out-dir]} 36 OUT_DIR=${2:-$(dirname "$0")/../out/coverage} 37 REPO=$(cd "$(dirname "$0")/../.." && pwd) 38 VARIANT=${SIM_VARIANT:-fdroidSim} 39 JACOCO_VERSION=${JACOCO_VERSION:-0.8.14} 40 CACHE=${SIM_TOOLS:-$HOME/.cache/taler-sim} 41 CLI="$CACHE/org.jacoco.cli-$JACOCO_VERSION-nodeps.jar" 42 43 mkdir -p "$CACHE" "$OUT_DIR" 44 if [ ! -s "$CLI" ]; then 45 echo "fetching jacoco cli $JACOCO_VERSION" >&2 46 curl -sSfL -o "$CLI" \ 47 "https://repo1.maven.org/maven2/org/jacoco/org.jacoco.cli/$JACOCO_VERSION/org.jacoco.cli-$JACOCO_VERSION-nodeps.jar" 48 fi 49 50 # One truncated .ec -- a dump whose app was killed mid-write -- makes jacoco's 51 # `merge` abort over every other file, so the files are checked first and only 52 # the complete ones go in. 53 mapfile -t EC_FILES < <(python3 "$(dirname "$0")/validate_ec.py" "$EC_DIR") 54 [ "${#EC_FILES[@]}" -gt 0 ] || { echo "no usable .ec files under $EC_DIR" >&2; exit 1; } 55 echo "merging ${#EC_FILES[@]} execution data file(s)" >&2 56 57 # JAVA_HOME when it points at a usable JVM, otherwise whatever is on PATH: 58 # this used to hard-code the workstation's JDK, so a run on the emulator host 59 # -- where JAVA_HOME is not set -- died with "no such file or directory" after 60 # validating all 2400 dumps. 61 if [ -n "${JAVA_HOME:-}" ] && [ -x "$JAVA_HOME/bin/java" ]; then 62 JAVA="$JAVA_HOME/bin/java" 63 else 64 JAVA=$(command -v java) || 65 { echo "no java on PATH and no usable JAVA_HOME" >&2; exit 1; } 66 fi 67 MERGED="$OUT_DIR/merged.exec" 68 # `merge` appends to an existing --destfile, which silently doubles everything 69 # on a second run. 70 rm -f "$MERGED" 71 "$JAVA" -jar "$CLI" merge "${EC_FILES[@]}" --destfile "$MERGED" 72 73 # Kotlin and Java classes land in separate trees; both belong in the report, 74 # and both must be the uninstrumented ones. 75 CLASS_ARGS=() 76 for d in "$REPO/wallet/build/tmp/kotlin-classes/$VARIANT" \ 77 "$REPO/wallet/build/intermediates/javac/$VARIANT/classes"; do 78 [ -d "$d" ] && CLASS_ARGS+=(--classfiles "$d") 79 done 80 [ "${#CLASS_ARGS[@]}" -gt 0 ] || { echo "no class files for variant $VARIANT" >&2; exit 1; } 81 82 "$JAVA" -jar "$CLI" report "$MERGED" \ 83 "${CLASS_ARGS[@]}" \ 84 --sourcefiles "$REPO/wallet/src/main/java" \ 85 --sourcefiles "$REPO/taler-kotlin-android/src/main/java" \ 86 --html "$OUT_DIR/html" \ 87 --xml "$OUT_DIR/coverage.xml" \ 88 --name "Taler wallet simulator ($VARIANT)" 89 90 python3 - "$OUT_DIR/coverage.xml" <<'PY' 91 import sys, xml.etree.ElementTree as ET 92 root = ET.parse(sys.argv[1]).getroot() 93 print() 94 for counter in root.findall("counter"): 95 t = counter.get("type") 96 covered, missed = int(counter.get("covered")), int(counter.get("missed")) 97 total = covered + missed 98 if total: 99 print(f"{t.lower():12} {covered:7}/{total:<7} {100 * covered / total:5.1f}%") 100 PY 101 echo 102 echo "report: $OUT_DIR/html/index.html" >&2