1#!/bin/bash 2# Copyright 2022 Google LLC 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15################################################################################ 16 17# This script runs all the Bazel tests within a given workspace directory. 18# 19# Users must spcify the WORKSPACE directory. Optionally, the user can specify 20# a set of additional manual targets to run. 21# 22 23# Note: -E extends the trap to shell functions, command substitutions, and 24# commands executed in a subshell environment. 25set -eEo pipefail 26# Print some debug output on error before exiting. 27trap print_debug_output ERR 28 29usage() { 30 echo "Usage: $0 [-mh] [-b <build parameter> ...] [-t <test parameter> ...] \\" 31 echo " <workspace directory> [<manual target> <manual target> ...]" 32 echo " -m: Runs only the manual targets. If set, manual targets must be" 33 echo " provided." 34 echo " -b: Comma separated list of flags to pass to `bazel build`." 35 echo " -t: Comma separated list of flags to pass to `bazel test`." 36 echo " -h: Help. Print this usage information." 37 exit 1 38} 39 40readonly PLATFORM="$(uname | tr '[:upper:]' '[:lower:]')" 41MANUAL_ONLY="false" 42WORKSPACE_DIR= 43MANUAL_TARGETS= 44BAZEL_CMD="bazel" 45BUILD_FLAGS=() 46TEST_FLAGS=() 47 48####################################### 49# Process command line arguments. 50# 51# Globals: 52# WORKSPACE_DIR 53# MANUAL_TARGETS 54####################################### 55process_args() { 56 # Parse options. 57 while getopts "mhb:t:" opt; do 58 case "${opt}" in 59 m) MANUAL_ONLY="true" ;; 60 b) BUILD_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;; 61 t) TEST_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;; 62 *) usage ;; 63 esac 64 done 65 shift $((OPTIND - 1)) 66 67 WORKSPACE_DIR="$1" 68 readonly WORKSPACE_DIR 69 70 if [[ -z "${WORKSPACE_DIR}" ]]; then 71 usage 72 fi 73 74 shift 1 75 MANUAL_TARGETS=("$@") 76 readonly MANUAL_TARGETS 77 78 if [[ "${MANUAL_ONLY}" == "true" ]] && (( ${#MANUAL_TARGETS[@]} == 0 )); then 79 usage 80 fi 81 82 # Use Bazelisk (https://github.com/bazelbuild/bazelisk) if available. 83 if command -v "bazelisk" &> /dev/null; then 84 BAZEL_CMD="bazelisk" 85 fi 86 readonly BAZEL_CMD 87 echo "Using: $(which ${BAZEL_CMD})" 88} 89 90####################################### 91# Print some debugging output. 92####################################### 93print_debug_output() { 94 ls -l 95 df -h 96} 97 98main() { 99 process_args "$@" 100 101 TEST_FLAGS+=( 102 --strategy=TestRunner=standalone 103 --test_output=all 104 ) 105 106 local -r workspace_dir="$(cd ${WORKSPACE_DIR} && pwd)" 107 108 if [[ "${PLATFORM}" == 'darwin' ]]; then 109 TEST_FLAGS+=( --jvmopt="-Djava.net.preferIPv6Addresses=true" ) 110 if [[ "${workspace_dir}" =~ javascript ]]; then 111 BUILD_FLAGS+=( --experimental_inprocess_symlink_creation ) 112 TEST_FLAGS+=( --experimental_inprocess_symlink_creation ) 113 fi 114 fi 115 readonly BUILD_FLAGS 116 readonly TEST_FLAGS 117 ( 118 set -x 119 cd "${workspace_dir}" 120 if [[ "${MANUAL_ONLY}" == "false" ]]; then 121 time "${BAZEL_CMD}" build "${BUILD_FLAGS[@]}" -- ... 122 # Exit code 4 means targets build correctly but no tests were found. See 123 # https://bazel.build/docs/scripts#exit-codes. 124 bazel_test_return=0 125 time "${BAZEL_CMD}" test "${TEST_FLAGS[@]}" -- ... || bazel_test_return="$?" 126 if (( $bazel_test_return != 0 && $bazel_test_return != 4 )); then 127 return "${bazel_test_return}" 128 fi 129 fi 130 # Run specific manual targets. 131 if (( ${#MANUAL_TARGETS[@]} > 0 )); then 132 time "${BAZEL_CMD}" build "${BUILD_FLAGS[@]}" -- "${MANUAL_TARGETS[@]}" 133 time "${BAZEL_CMD}" test "${TEST_FLAGS[@]}" -- "${MANUAL_TARGETS[@]}" 134 fi 135 ) 136} 137 138main "$@" 139