Yue Gan | af1e3c2 | 2016-12-05 14:05:15 +0000 | [diff] [blame] | 1 | #!/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 | |
| 28 | if [[ -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 |
| 34 | fi |
| 35 | |
| 36 | # When collect_coverage.sh is used, test runner must be instructed not to cd |
| 37 | # to the test's runfiles directory. |
| 38 | ROOT="$PWD" |
| 39 | |
| 40 | if [[ "$COVERAGE_MANIFEST" != /* ]]; then |
| 41 | # Canonicalize the path to coverage manifest so that tests can find it. |
| 42 | export COVERAGE_MANIFEST="$ROOT/$COVERAGE_MANIFEST" |
| 43 | fi |
| 44 | |
| 45 | # write coverage data outside of the runfiles tree |
| 46 | export COVERAGE_DIR=${COVERAGE_DIR:-"$ROOT/coverage"} |
| 47 | # make COVERAGE_DIR an absolute path |
| 48 | if ! [[ $COVERAGE_DIR == $ROOT* ]]; then |
| 49 | COVERAGE_DIR=$ROOT/$COVERAGE_DIR |
| 50 | fi |
| 51 | |
| 52 | mkdir -p "$COVERAGE_DIR" |
| 53 | COVERAGE_OUTPUT_FILE=${COVERAGE_OUTPUT_FILE:-"$COVERAGE_DIR/_coverage.dat"} |
| 54 | # make COVERAGE_OUTPUT_FILE an absolute path |
| 55 | if ! [[ $COVERAGE_OUTPUT_FILE == $ROOT* ]]; then |
| 56 | COVERAGE_OUTPUT_FILE=$ROOT/$COVERAGE_OUTPUT_FILE |
| 57 | fi |
| 58 | |
| 59 | |
| 60 | # Java |
| 61 | # -------------------------------------- |
| 62 | export JAVA_COVERAGE_FILE=$COVERAGE_DIR/jvcov.dat |
| 63 | # Let tests know that it is a coverage run |
| 64 | export COVERAGE=1 |
| 65 | export BULK_COVERAGE_RUN=1 |
| 66 | |
| 67 | |
| 68 | # Only check if file exists when LCOV_MERGER is set |
| 69 | if [[ ! -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 |
| 77 | fi |
| 78 | |
| 79 | |
| 80 | cd "$TEST_SRCDIR" |
| 81 | "$@" |
| 82 | TEST_STATUS=$? |
| 83 | |
| 84 | # If LCOV_MERGER is not set, coverage is not supported. |
| 85 | if [[ -z "$LCOV_MERGER" ]]; then |
| 86 | exit $TEST_STATUS |
| 87 | fi |
| 88 | |
| 89 | |
| 90 | # always create output files |
| 91 | touch $COVERAGE_OUTPUT_FILE |
| 92 | |
| 93 | if [[ $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 |
| 99 | fi |
| 100 | |
| 101 | cd $ROOT |
| 102 | |
| 103 | export LCOV_MERGER_CMD="java -jar ${LCOV_MERGER} --coverage_dir=${COVERAGE_DIR} \ |
| 104 | --output_file=${COVERAGE_OUTPUT_FILE}" |
| 105 | |
| 106 | |
| 107 | if [[ $DISPLAY_LCOV_CMD ]] ; then |
| 108 | echo "Running lcov_merger" |
| 109 | echo $LCOV_MERGER_CMD |
| 110 | echo "-----------------" |
| 111 | fi |
| 112 | |
| 113 | exec $LCOV_MERGER_CMD |