blob: 33aa2e373cd405059e238ef145abebfbcb9548a8 [file] [log] [blame]
Yue Ganaf1e3c22016-12-05 14:05:15 +00001#!/bin/bash
2
3# Copyright 2016 The Bazel Authors. All rights reserved.
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# Wrapper script for collecting code coverage during test execution.
18#
19# Expected environment:
20# COVERAGE_MANIFEST - mandatory, location of the instrumented file manifest
21# LCOV_MERGER - mandatory, location of the LcovMerger
22# COVERAGE_DIR - optional, location of the coverage temp directory
23# COVERAGE_OUTPUT_FILE - optional, location of the final lcov file
24#
25# Script expects that it will be started in the execution root directory and
26# not in the test's runfiles directory.
27
28if [[ -z "$COVERAGE_MANIFEST" ]]; then
29 echo --
30 echo Coverage runner: \$COVERAGE_MANIFEST is not set
31 echo Current environment:
32 env | sort
33 exit 1
34fi
35
36# When collect_coverage.sh is used, test runner must be instructed not to cd
37# to the test's runfiles directory.
38ROOT="$PWD"
39
40if [[ "$COVERAGE_MANIFEST" != /* ]]; then
41 # Canonicalize the path to coverage manifest so that tests can find it.
42 export COVERAGE_MANIFEST="$ROOT/$COVERAGE_MANIFEST"
43fi
44
45# write coverage data outside of the runfiles tree
46export COVERAGE_DIR=${COVERAGE_DIR:-"$ROOT/coverage"}
47# make COVERAGE_DIR an absolute path
48if ! [[ $COVERAGE_DIR == $ROOT* ]]; then
49 COVERAGE_DIR=$ROOT/$COVERAGE_DIR
50fi
51
52mkdir -p "$COVERAGE_DIR"
53COVERAGE_OUTPUT_FILE=${COVERAGE_OUTPUT_FILE:-"$COVERAGE_DIR/_coverage.dat"}
54# make COVERAGE_OUTPUT_FILE an absolute path
55if ! [[ $COVERAGE_OUTPUT_FILE == $ROOT* ]]; then
56 COVERAGE_OUTPUT_FILE=$ROOT/$COVERAGE_OUTPUT_FILE
57fi
58
59
60# Java
61# --------------------------------------
62export JAVA_COVERAGE_FILE=$COVERAGE_DIR/jvcov.dat
63# Let tests know that it is a coverage run
64export COVERAGE=1
65export BULK_COVERAGE_RUN=1
66
67
68# Only check if file exists when LCOV_MERGER is set
69if [[ ! -z "$LCOV_MERGER" ]]; then
70 for name in "$LCOV_MERGER"; do
71 if [[ ! -e $name ]]; then
72 echo --
73 echo Coverage runner: cannot locate file $name
74 exit 1
75 fi
76 done
77fi
78
79
80cd "$TEST_SRCDIR"
81"$@"
82TEST_STATUS=$?
83
84# If LCOV_MERGER is not set, coverage is not supported.
85if [[ -z "$LCOV_MERGER" ]]; then
86 exit $TEST_STATUS
87fi
88
89
90# always create output files
91touch $COVERAGE_OUTPUT_FILE
92
93if [[ $TEST_STATUS -ne 0 ]]; then
94 echo --
95 echo Coverage runner: Not collecting coverage for failed test.
96 echo The following commands failed with status $TEST_STATUS
97 echo "$@"
98 exit $TEST_STATUS
99fi
100
101cd $ROOT
102
103export LCOV_MERGER_CMD="java -jar ${LCOV_MERGER} --coverage_dir=${COVERAGE_DIR} \
104--output_file=${COVERAGE_OUTPUT_FILE}"
105
106
107if [[ $DISPLAY_LCOV_CMD ]] ; then
108 echo "Running lcov_merger"
109 echo $LCOV_MERGER_CMD
110 echo "-----------------"
111fi
112
113exec $LCOV_MERGER_CMD