1#!/bin/bash 2############################################################################### 3# Copyright 2017 Google LLC 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16################################################################################ 17# 18# Usage 19# 20# COVERAGE_CPUS=32 tools/coverage.sh [/path/to/report-directory/] [targets] 21# 22# COVERAGE_CPUS defaults to 2, and the default destination is a temp 23# dir. 24 25set -eu 26 27genhtml=$(command -v genhtml) 28if [[ -z "${genhtml}" ]]; then 29 echo "Install 'genhtml' (contained in the 'lcov' package)" 30 exit 1 31fi 32 33destdir="$1" 34if [[ -z "${destdir}" ]]; then 35 destdir=$(mktemp -d /tmp/gerritcov.XXXXXX) 36fi 37 38targets="$2" 39if [[ -z "${targets}" ]]; then 40 targets="apps/... java/..." 41fi 42 43echo "Running 'bazel coverage'; this may take a while" 44# coverage is expensive to run; use --jobs=2 to avoid overloading the 45# machine. 46bazel coverage -k --jobs="${COVERAGE_CPUS:-2}" -- "$targets" 47 48# The coverage data contains filenames relative to the Java root, and 49# genhtml has no logic to search these elsewhere. Workaround this 50# limitation by running genhtml in a directory with the files in the 51# right place. Also -inexplicably- genhtml wants to have the source 52# files relative to the output directory. 53rm -rf "${destdir}" || true 54mkdir -p "${destdir}" 55 56for ROOT in java apps/paymentmethodtoken; do 57 rsync -a "${ROOT}/src/main/java/" "${ROOT}/src/test/java/" "${destdir}/" 58done 59 60base=$(bazel info bazel-testlogs) 61 62find "${base}" -name 'coverage.dat' -exec sh -c ' 63 for ff do 64 f=$(printf '%s' "${ff#"$base"/}" | sed "s|/|_|g") 65 cp "$ff" "${destdir}/$f" 66 done 67' find-sh {} + 68 69cd "${destdir}" 70 71find -name '*coverage.dat' -size 0 -exec rm -f {} + 72 73genhtml -o . --ignore-errors source ./*coverage.dat 74printf "coverage report at file://%s/index.html" "${destdir}" 75