report.sh (7405B)
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 # The emulator is not the only thing that covers this app: the wallet module 74 # carries JVM unit tests, and a report that cannot see them invites us to 75 # spend emulator time re-covering what a unit test already reaches. So both 76 # are reported -- the simulator alone, which is what a round of scenarios 77 # achieved, and the union, which is what is actually tested. 78 # 79 # `enableUnitTestCoverage` on the `sim` build type in wallet/build.gradle is 80 # what produces the second file, and the build type matters: jacoco matches 81 # execution data to classes by checksum, so the unit tests have to have run 82 # against the same variant the emulator did 83 # (`./gradlew :wallet:testFdroidSimUnitTest`). SIM_UNIT_EXEC=0 leaves it 84 # out, or names one explicitly. 85 UNIT_EXEC=${SIM_UNIT_EXEC:-auto} 86 if [ "$UNIT_EXEC" = "auto" ]; then 87 # This variant's own file, and not whichever one the directory lists 88 # first: a tree that has also built fdroidDebug has an exec file beside 89 # it, and merging that one in is worse than merging none -- jacoco drops 90 # every class whose checksum does not match and says so in a line nobody 91 # reads, so the report would quietly show the simulator's figure as the 92 # union. 93 UNIT_EXEC=$(ls -1 \ 94 "$REPO/wallet/build/outputs/unit_test_code_coverage/${VARIANT}UnitTest/"*.exec \ 95 2>/dev/null | head -1 || true) 96 fi 97 UNION="" 98 if [ -n "$UNIT_EXEC" ] && [ "$UNIT_EXEC" != "0" ] && [ -s "$UNIT_EXEC" ]; then 99 UNION="$OUT_DIR/merged-with-units.exec" 100 rm -f "$UNION" 101 "$JAVA" -jar "$CLI" merge "${EC_FILES[@]}" "$UNIT_EXEC" --destfile "$UNION" 102 echo "including unit-test coverage from $UNIT_EXEC" >&2 103 else 104 echo "no unit-test coverage merged (run :wallet:testFdroidSimUnitTest)" >&2 105 fi 106 107 # Kotlin and Java classes land in separate trees; both belong in the report, 108 # and both must be the uninstrumented ones. 109 CLASS_ARGS=() 110 for d in "$REPO/wallet/build/tmp/kotlin-classes/$VARIANT" \ 111 "$REPO/wallet/build/intermediates/javac/$VARIANT/classes"; do 112 [ -d "$d" ] && CLASS_ARGS+=(--classfiles "$d") 113 done 114 [ "${#CLASS_ARGS[@]}" -gt 0 ] || { echo "no class files for variant $VARIANT" >&2; exit 1; } 115 116 "$JAVA" -jar "$CLI" report "$MERGED" \ 117 "${CLASS_ARGS[@]}" \ 118 --sourcefiles "$REPO/wallet/src/main/java" \ 119 --sourcefiles "$REPO/taler-kotlin-android/src/main/java" \ 120 --html "$OUT_DIR/html" \ 121 --xml "$OUT_DIR/coverage.xml" \ 122 --name "Taler wallet simulator ($VARIANT)" 123 124 python3 - "$OUT_DIR/coverage.xml" <<'PY' 125 import sys, xml.etree.ElementTree as ET 126 root = ET.parse(sys.argv[1]).getroot() 127 print() 128 for counter in root.findall("counter"): 129 t = counter.get("type") 130 covered, missed = int(counter.get("covered")), int(counter.get("missed")) 131 total = covered + missed 132 if total: 133 print(f"{t.lower():12} {covered:7}/{total:<7} {100 * covered / total:5.1f}%") 134 PY 135 if [ -n "$UNION" ]; then 136 "$JAVA" -jar "$CLI" report "$UNION" \ 137 "${CLASS_ARGS[@]}" \ 138 --sourcefiles "$REPO/wallet/src/main/java" \ 139 --sourcefiles "$REPO/taler-kotlin-android/src/main/java" \ 140 --html "$OUT_DIR/html-with-units" \ 141 --xml "$OUT_DIR/coverage-with-units.xml" \ 142 --name "Taler wallet simulator + unit tests ($VARIANT)" >/dev/null 143 echo 144 echo "simulator and unit tests together:" 145 python3 "$(dirname "$0")/union_summary.py" \ 146 "$OUT_DIR/coverage-with-units.xml" "$OUT_DIR/coverage.xml" 147 else 148 # A stale pair from an earlier run would otherwise make the landing page 149 # claim a union it did not measure. 150 rm -f "$OUT_DIR/coverage-with-units.xml" 151 rm -rf "$OUT_DIR/html-with-units" 152 fi 153 154 # A landing page over the top of JaCoCo's own, because JaCoCo cannot know 155 # which of the red lines no run could ever enter, nor what produced the 156 # figures. Failure here must not fail the run: the numbers above are the 157 # deliverable and this is a convenience. 158 python3 "$(dirname "$0")/summary_html.py" "$OUT_DIR" "$EC_DIR" \ 159 ${SIM_SUMMARY_ARGS:-} || echo "summary page not written" >&2 160 161 echo 162 echo "summary: $OUT_DIR/index.html" >&2 163 echo "report: $OUT_DIR/html/index.html" >&2 164 if [ -n "$UNION" ]; then echo "union: $OUT_DIR/html-with-units/index.html" >&2; fi