Edwin Kempin | d870d46 | 2021-01-14 08:32:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Han-Wen Nienhuys | 23ed348 | 2017-05-10 10:04:24 +0200 | [diff] [blame] | 2 | # |
| 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 Kempin | d870d46 | 2021-01-14 08:32:27 +0100 | [diff] [blame] | 10 | bazel_bin=$(which bazelisk 2>/dev/null) |
| 11 | if [[ -z "$bazel_bin" ]]; then |
| 12 | echo "Warning: bazelisk is not installed; falling back to bazel." |
| 13 | bazel_bin=bazel |
| 14 | fi |
Edwin Kempin | 3377cce | 2021-01-13 10:09:46 +0100 | [diff] [blame] | 15 | |
Han-Wen Nienhuys | 23ed348 | 2017-05-10 10:04:24 +0200 | [diff] [blame] | 16 | genhtml=$(which genhtml) |
| 17 | if [[ -z "${genhtml}" ]]; then |
| 18 | echo "Install 'genhtml' (contained in the 'lcov' package)" |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | destdir="$1" |
| 23 | if [[ -z "${destdir}" ]]; then |
| 24 | destdir=$(mktemp -d /tmp/gerritcov.XXXXXX) |
| 25 | fi |
| 26 | |
| 27 | echo "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 Kempin | d870d46 | 2021-01-14 08:32:27 +0100 | [diff] [blame] | 31 | ${bazel_bin} coverage -k --jobs=${COVERAGE_CPUS:-2} -- ... |
Han-Wen Nienhuys | 23ed348 | 2017-05-10 10:04:24 +0200 | [diff] [blame] | 32 | |
| 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 Kempin | 3a69e82 | 2019-03-28 12:07:41 +0100 | [diff] [blame] | 38 | mkdir -p ${destdir}/java |
| 39 | cp -r {java,javatests}/* ${destdir}/java |
| 40 | |
| 41 | mkdir -p ${destdir}/plugins |
Edwin Kempin | 9bb4919 | 2020-12-04 14:23:28 +0100 | [diff] [blame] | 42 | for plugin in `find plugins/ -mindepth 1 -maxdepth 1 -type d` |
Edwin Kempin | 3a69e82 | 2019-03-28 12:07:41 +0100 | [diff] [blame] | 43 | do |
| 44 | mkdir -p ${destdir}/${plugin}/java |
Edwin Kempin | 9bb4919 | 2020-12-04 14:23:28 +0100 | [diff] [blame] | 45 | 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 Kempin | 3a69e82 | 2019-03-28 12:07:41 +0100 | [diff] [blame] | 51 | |
| 52 | # for backwards compatibility support plugins with old file structure |
| 53 | mkdir -p ${destdir}/${plugin}/src/{main,test}/java |
Edwin Kempin | 9bb4919 | 2020-12-04 14:23:28 +0100 | [diff] [blame] | 54 | 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 Kempin | 3a69e82 | 2019-03-28 12:07:41 +0100 | [diff] [blame] | 60 | done |
Han-Wen Nienhuys | 23ed348 | 2017-05-10 10:04:24 +0200 | [diff] [blame] | 61 | |
Edwin Kempin | d870d46 | 2021-01-14 08:32:27 +0100 | [diff] [blame] | 62 | base=$(${bazel_bin} info bazel-testlogs) |
Han-Wen Nienhuys | 23ed348 | 2017-05-10 10:04:24 +0200 | [diff] [blame] | 63 | for f in $(find ${base} -name 'coverage.dat') ; do |
| 64 | cp $f ${destdir}/$(echo $f| sed "s|${base}/||" | sed "s|/|_|g") |
| 65 | done |
| 66 | |
| 67 | cd ${destdir} |
| 68 | find -name '*coverage.dat' -size 0 -delete |
| 69 | |
| 70 | genhtml -o . --ignore-errors source *coverage.dat |
| 71 | |
| 72 | echo "coverage report at file://${destdir}/index.html" |