aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/coverage
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2016-11-24 12:51:35 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-11-24 13:34:11 +0000
commit8829abaeec8fa0be7ea6d87cbfde656e9c780cf3 (patch)
treef0063ad8b10ec7a4db91948609aba5a50bc2cf94 /tools/coverage
parent74ffaf7a7a1611fb693d5c37469dcd6ea1bf3715 (diff)
Coverage support.
- open source CoverageCommand.java - add a collect-coverage.sh script - update test-setup.sh to be compatible with the coverage collector - update StandaloneTestStrategy to provide the necessary env variables - update StandaloneTestStrategy to set the right command line for coverage - add support for C++ coverage An HTML report can then be generated with genhtml like this: genhtml -o report/ -p "$(readlink -f bazel-<project>)" path/to/coverage.dat Progress on #1118. -- MOS_MIGRATED_REVID=140125715
Diffstat (limited to 'tools/coverage')
-rw-r--r--tools/coverage/BUILD16
-rwxr-xr-xtools/coverage/collect-coverage.sh72
-rw-r--r--tools/coverage/dummy_coverage_report_generator2
3 files changed, 90 insertions, 0 deletions
diff --git a/tools/coverage/BUILD b/tools/coverage/BUILD
new file mode 100644
index 0000000000..2b8bb738ac
--- /dev/null
+++ b/tools/coverage/BUILD
@@ -0,0 +1,16 @@
+package(default_visibility = ["//visibility:public"])
+
+filegroup(
+ name = "coverage_support",
+ srcs = ["collect-coverage.sh"],
+)
+
+filegroup(
+ name = "coverage_report_generator",
+ srcs = ["dummy_coverage_report_generator"],
+)
+
+filegroup(
+ name = "srcs",
+ srcs = glob(["*"]),
+)
diff --git a/tools/coverage/collect-coverage.sh b/tools/coverage/collect-coverage.sh
new file mode 100755
index 0000000000..85875c08a2
--- /dev/null
+++ b/tools/coverage/collect-coverage.sh
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+# Copyright 2016 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+ROOT="$PWD"
+if [[ $COVERAGE_OUTPUT_FILE != /* ]]; then
+ COVERAGE_OUTPUT_FILE="${ROOT}/${COVERAGE_OUTPUT_FILE}"
+fi
+if [[ "$COVERAGE_MANIFEST" != /* ]]; then
+ export COVERAGE_MANIFEST="${ROOT}/${COVERAGE_MANIFEST}"
+fi
+
+export COVERAGE_DIR="$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)"
+trap "{ rm -rf ${COVERAGE_DIR} }" EXIT
+
+# C++ env variables
+export GCOV_PREFIX_STRIP=3
+export GCOV_PREFIX="${COVERAGE_DIR}"
+
+touch "${COVERAGE_OUTPUT_FILE}"
+
+DIR="$TEST_SRCDIR"
+if [ ! -z "$TEST_WORKSPACE" ]; then
+ DIR="$DIR"/"$TEST_WORKSPACE"
+fi
+cd "$DIR" || { echo "Could not chdir $DIR"; exit 1; }
+"$@"
+TEST_STATUS=$?
+
+if [[ ${TEST_STATUS} -ne 0 ]]; then
+ echo "--"
+ echo "Coverage runner: Not collecting coverage for failed test."
+ echo "The following commands failed with status ${TEST_STATUS}:"
+ echo "$@"
+ exit ${TEST_STATUS}
+fi
+
+echo "--"
+echo "Post-processing coverage results:"
+
+cat "${COVERAGE_MANIFEST}" | grep ".gcno$" | while read path; do
+ mkdir -p "${COVERAGE_DIR}/$(dirname ${path})"
+ cp "${ROOT}/${path}" "${COVERAGE_DIR}/${path}"
+done
+
+# Unfortunately, lcov messes up the source file names if it can't find the files
+# at their relative paths. Workaround by creating empty source files according
+# to the manifest (i.e., only for files that are supposed to be instrumented).
+cat "${COVERAGE_MANIFEST}" | egrep ".(cc|h)$" | while read path; do
+ mkdir -p "${COVERAGE_DIR}/$(dirname ${path})"
+ touch "${COVERAGE_DIR}/${path}"
+done
+
+# Run lcov over the .gcno and .gcda files to generate the lcov tracefile.
+/usr/bin/lcov -c --no-external -d "${COVERAGE_DIR}" -o "${COVERAGE_OUTPUT_FILE}"
+
+# The paths are all wrong, because they point to /tmp. Fix up the paths to
+# point to the exec root instead (${ROOT}).
+sed -i -e "s*${COVERAGE_DIR}*${ROOT}*g" "${COVERAGE_OUTPUT_FILE}"
+
diff --git a/tools/coverage/dummy_coverage_report_generator b/tools/coverage/dummy_coverage_report_generator
new file mode 100644
index 0000000000..27c43ad8c2
--- /dev/null
+++ b/tools/coverage/dummy_coverage_report_generator
@@ -0,0 +1,2 @@
+# Dummy file. Bazel requires there to be a single file in the coverage report
+# generator attribute, even though it's currently not used in Bazel.