report.sh (7142B)
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 writes them on 21 # the emulator host; 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:-$(cd "$(dirname "$0")/.." && pwd)/out/coverage} 37 REPO=$(cd "$(dirname "$0")/../.." && pwd) 38 VARIANT=${POS_VARIANT:-sim} 39 JACOCO_VERSION=${JACOCO_VERSION:-0.8.14} 40 CACHE=${POS_TOOLS:-$HOME/.cache/taler-pos-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 if [ -n "${JAVA_HOME:-}" ] && [ -x "$JAVA_HOME/bin/java" ]; then 58 JAVA="$JAVA_HOME/bin/java" 59 else 60 JAVA=$(command -v java) || 61 { echo "no java on PATH and no usable JAVA_HOME" >&2; exit 1; } 62 fi 63 MERGED="$OUT_DIR/merged.exec" 64 # `merge` appends to an existing --destfile, which silently doubles everything 65 # on a second run. 66 rm -f "$MERGED" 67 "$JAVA" -jar "$CLI" merge "${EC_FILES[@]}" --destfile "$MERGED" 68 69 # The emulator is not the only thing that covers this app: merchant-terminal 70 # carries ~290 JVM unit tests, and a report that cannot see them invites us to 71 # spend emulator time re-covering what a unit test already reaches. So both 72 # are reported: the simulator alone, which is what a round of scenarios 73 # achieved, and the union, which is what is actually tested. 74 # 75 # `enableUnitTestCoverage` in merchant-terminal/build.gradle is what produces 76 # the second file; POS_UNIT_EXEC=0 leaves it out, or names one explicitly. 77 UNIT_EXEC=${POS_UNIT_EXEC:-auto} 78 if [ "$UNIT_EXEC" = "auto" ]; then 79 # This variant's own file, and not whichever one the directory lists 80 # first: a tree that has also built the debug tests has an exec file 81 # beside it, and merging that one in is worse than merging none -- jacoco 82 # drops every class whose checksum does not match and says so in a line 83 # nobody reads, so the report would quietly show the simulator's figure as 84 # the union. 85 UNIT_EXEC=$(ls -1 \ 86 "$REPO/merchant-terminal/build/outputs/unit_test_code_coverage/${VARIANT}UnitTest/"*.exec \ 87 2>/dev/null | head -1 || true) 88 fi 89 UNION="" 90 if [ -n "$UNIT_EXEC" ] && [ "$UNIT_EXEC" != "0" ] && [ -s "$UNIT_EXEC" ]; then 91 UNION="$OUT_DIR/merged-with-units.exec" 92 rm -f "$UNION" 93 "$JAVA" -jar "$CLI" merge "${EC_FILES[@]}" "$UNIT_EXEC" --destfile "$UNION" 94 echo "including unit-test coverage from $UNIT_EXEC" >&2 95 else 96 echo "no unit-test coverage merged (run :merchant-terminal:testSimUnitTest)" >&2 97 fi 98 99 # Kotlin and Java classes land in separate trees; both belong in the report, 100 # and both must be the uninstrumented ones. 101 CLASS_ARGS=() 102 for d in "$REPO/merchant-terminal/build/tmp/kotlin-classes/$VARIANT" \ 103 "$REPO/merchant-terminal/build/intermediates/javac/$VARIANT/classes"; do 104 [ -d "$d" ] && CLASS_ARGS+=(--classfiles "$d") 105 done 106 [ "${#CLASS_ARGS[@]}" -gt 0 ] || { echo "no class files for variant $VARIANT" >&2; exit 1; } 107 108 # The terminal's own sources plus the two libraries it is a thin layer over: 109 # merchant-lib is where every backend call lives and taler-kotlin-android 110 # holds Amount and the NFC service, so leaving them out would overstate how 111 # much of what runs is covered. 112 "$JAVA" -jar "$CLI" report "$MERGED" \ 113 "${CLASS_ARGS[@]}" \ 114 --sourcefiles "$REPO/merchant-terminal/src/main/java" \ 115 --sourcefiles "$REPO/merchant-lib/src/main/java" \ 116 --sourcefiles "$REPO/taler-kotlin-android/src/main/java" \ 117 --html "$OUT_DIR/html" \ 118 --xml "$OUT_DIR/coverage.xml" \ 119 --name "Taler POS simulator ($VARIANT)" 120 121 python3 - "$OUT_DIR/coverage.xml" <<'PY' 122 import sys, xml.etree.ElementTree as ET 123 root = ET.parse(sys.argv[1]).getroot() 124 print() 125 for counter in root.findall("counter"): 126 t = counter.get("type") 127 covered, missed = int(counter.get("covered")), int(counter.get("missed")) 128 total = covered + missed 129 if total: 130 print(f"{t.lower():12} {covered:7}/{total:<7} {100 * covered / total:5.1f}%") 131 PY 132 if [ -n "$UNION" ]; then 133 "$JAVA" -jar "$CLI" report "$UNION" \ 134 "${CLASS_ARGS[@]}" \ 135 --sourcefiles "$REPO/merchant-terminal/src/main/java" \ 136 --sourcefiles "$REPO/merchant-lib/src/main/java" \ 137 --sourcefiles "$REPO/taler-kotlin-android/src/main/java" \ 138 --html "$OUT_DIR/html-with-units" \ 139 --xml "$OUT_DIR/coverage-with-units.xml" \ 140 --name "Taler POS simulator + unit tests ($VARIANT)" >/dev/null 141 echo 142 echo "simulator and unit tests together:" 143 python3 "$(dirname "$0")/union_summary.py" \ 144 "$OUT_DIR/coverage-with-units.xml" "$OUT_DIR/coverage.xml" 145 fi 146 147 # A landing page over the top of JaCoCo's own, because JaCoCo cannot know 148 # which of the red lines this build type could never enter, nor what produced 149 # the figures. Failure here must not fail the run: the numbers above are the 150 # deliverable and this is a convenience. 151 python3 "$(dirname "$0")/summary_html.py" "$OUT_DIR" "$EC_DIR" \ 152 || echo "summary page not written" >&2 153 154 echo 155 echo "summary: $OUT_DIR/index.html" >&2 156 echo "report: $OUT_DIR/html/index.html" >&2 157 [ -n "$UNION" ] && echo "union: $OUT_DIR/html-with-units/index.html" >&2 158