blob: b20de31fa976b92c8f33b672bea1a9d37e60af3d [file] [log] [blame]
Edwin Kempind870d462021-01-14 08:32:27 +01001#!/usr/bin/env bash
Han-Wen Nienhuys23ed3482017-05-10 10:04:24 +02002#
3# Usage
4#
5# COVERAGE_CPUS=32 tools/coverage.sh [/path/to/report-directory/]
6#
7# COVERAGE_CPUS defaults to 2, and the default destination is a temp
8# dir.
9
Edwin Kempind870d462021-01-14 08:32:27 +010010bazel_bin=$(which bazelisk 2>/dev/null)
11if [[ -z "$bazel_bin" ]]; then
12 echo "Warning: bazelisk is not installed; falling back to bazel."
13 bazel_bin=bazel
14fi
Edwin Kempin3377cce2021-01-13 10:09:46 +010015
Han-Wen Nienhuys23ed3482017-05-10 10:04:24 +020016genhtml=$(which genhtml)
17if [[ -z "${genhtml}" ]]; then
18 echo "Install 'genhtml' (contained in the 'lcov' package)"
19 exit 1
20fi
21
22destdir="$1"
23if [[ -z "${destdir}" ]]; then
24 destdir=$(mktemp -d /tmp/gerritcov.XXXXXX)
25fi
26
27echo "Running 'bazel coverage'; this may take a while"
28
29# coverage is expensive to run; use --jobs=2 to avoid overloading the
30# machine.
Edwin Kempind870d462021-01-14 08:32:27 +010031${bazel_bin} coverage -k --jobs=${COVERAGE_CPUS:-2} -- ...
Han-Wen Nienhuys23ed3482017-05-10 10:04:24 +020032
33# The coverage data contains filenames relative to the Java root, and
34# genhtml has no logic to search these elsewhere. Workaround this
35# limitation by running genhtml in a directory with the files in the
36# right place. Also -inexplicably- genhtml wants to have the source
37# files relative to the output directory.
Edwin Kempin3a69e822019-03-28 12:07:41 +010038mkdir -p ${destdir}/java
39cp -r {java,javatests}/* ${destdir}/java
40
41mkdir -p ${destdir}/plugins
Edwin Kempin9bb49192020-12-04 14:23:28 +010042for plugin in `find plugins/ -mindepth 1 -maxdepth 1 -type d`
Edwin Kempin3a69e822019-03-28 12:07:41 +010043do
44 mkdir -p ${destdir}/${plugin}/java
Edwin Kempin9bb49192020-12-04 14:23:28 +010045 if [ -e ${plugin}/java/ ]
46 then cp -r ${plugin}/java/* ${destdir}/${plugin}/java
47 fi
48 if [ -e ${plugin}/javatests/ ]
49 then cp -r ${plugin}/javatests/* ${destdir}/${plugin}/java
50 fi
Edwin Kempin3a69e822019-03-28 12:07:41 +010051
52 # for backwards compatibility support plugins with old file structure
53 mkdir -p ${destdir}/${plugin}/src/{main,test}/java
Edwin Kempin9bb49192020-12-04 14:23:28 +010054 if [ -e ${plugin}/src/main/java/ ]
55 then cp -r ${plugin}/src/main/java/* ${destdir}/${plugin}/src/main/java/
56 fi
57 if [ -e ${plugin}/src/test/java/ ]
58 then cp -r ${plugin}/src/test/java/* ${destdir}/${plugin}/src/test/java/
59 fi
Edwin Kempin3a69e822019-03-28 12:07:41 +010060done
Han-Wen Nienhuys23ed3482017-05-10 10:04:24 +020061
Edwin Kempind870d462021-01-14 08:32:27 +010062base=$(${bazel_bin} info bazel-testlogs)
Han-Wen Nienhuys23ed3482017-05-10 10:04:24 +020063for f in $(find ${base} -name 'coverage.dat') ; do
64 cp $f ${destdir}/$(echo $f| sed "s|${base}/||" | sed "s|/|_|g")
65done
66
67cd ${destdir}
68find -name '*coverage.dat' -size 0 -delete
69
70genhtml -o . --ignore-errors source *coverage.dat
71
72echo "coverage report at file://${destdir}/index.html"