• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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] [-c bazel_cache_name] [-b <build parameter> ...] \\"
31  echo "         [-t <test parameter> ...] <workspace directory> \\"
32  echo "         [<manual target> <manual target> ...]"
33  echo "  -m: Runs only the manual targets. If set, manual targets must be"
34  echo "      provided."
35  echo "  -b: Comma separated list of flags to pass to `bazel build`."
36  echo "  -t: Comma separated list of flags to pass to `bazel test`."
37  echo "  -h: Help. Print this usage information."
38  echo "  -c: Bazel cache to use; creadentials are expected to be in a"
39  echo "      cache_key file."
40  exit 1
41}
42
43readonly PLATFORM="$(uname | tr '[:upper:]' '[:lower:]')"
44MANUAL_ONLY="false"
45WORKSPACE_DIR=
46MANUAL_TARGETS=
47BAZEL_CMD="bazel"
48BUILD_FLAGS=()
49TEST_FLAGS=()
50CACHE_FLAGS=()
51
52#######################################
53# Process command line arguments.
54#
55# Globals:
56#   WORKSPACE_DIR
57#   MANUAL_TARGETS
58#######################################
59process_args() {
60  # Parse options.
61  while getopts "mb:t:c:" opt; do
62    case "${opt}" in
63      m) MANUAL_ONLY="true" ;;
64      b) BUILD_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;;
65      t) TEST_FLAGS=($(echo "${OPTARG}" | tr ',' '\n')) ;;
66      c) CACHE_FLAGS=(
67           "--remote_cache=https://storage.googleapis.com/${OPTARG}"
68           "--google_credentials=$(realpath ./cache_key)"
69         );;
70      *) usage ;;
71    esac
72  done
73  shift $((OPTIND - 1))
74
75  WORKSPACE_DIR="$1"
76  readonly WORKSPACE_DIR
77
78  if [[ -z "${WORKSPACE_DIR}" ]]; then
79    usage
80  fi
81
82  shift 1
83  MANUAL_TARGETS=("$@")
84  readonly MANUAL_TARGETS
85
86  if [[ "${MANUAL_ONLY}" == "true" ]] && (( ${#MANUAL_TARGETS[@]} == 0 )); then
87    usage
88  fi
89
90  # Use Bazelisk (https://github.com/bazelbuild/bazelisk) if available.
91  if command -v "bazelisk" &> /dev/null; then
92    BAZEL_CMD="bazelisk"
93  fi
94  readonly BAZEL_CMD
95  echo "Using: $(which ${BAZEL_CMD})"
96
97  readonly CACHE_FLAGS
98}
99
100#######################################
101# Print some debugging output.
102#######################################
103print_debug_output() {
104  ls -l
105  df -h
106}
107
108main() {
109  process_args "$@"
110
111  TEST_FLAGS+=(
112    --strategy=TestRunner=standalone
113    --test_output=all
114  )
115
116  local -r workspace_dir="$(cd ${WORKSPACE_DIR} && pwd)"
117
118  if [[ "${PLATFORM}" == 'darwin' ]]; then
119    TEST_FLAGS+=( --jvmopt="-Djava.net.preferIPv6Addresses=true" )
120    if [[ "${workspace_dir}" =~ javascript ]]; then
121      BUILD_FLAGS+=( --experimental_inprocess_symlink_creation )
122      TEST_FLAGS+=( --experimental_inprocess_symlink_creation )
123    fi
124  fi
125  readonly BUILD_FLAGS
126  readonly TEST_FLAGS
127  (
128    set -x
129    cd "${workspace_dir}"
130    if [[ "${MANUAL_ONLY}" == "false" ]]; then
131      time "${BAZEL_CMD}" build "${CACHE_FLAGS[@]}" "${BUILD_FLAGS[@]}" -- ...
132      # Exit code 4 means targets build correctly but no tests were found. See
133      # https://bazel.build/docs/scripts#exit-codes.
134      bazel_test_return=0
135      time "${BAZEL_CMD}" test "${CACHE_FLAGS[@]}" "${TEST_FLAGS[@]}" -- ... \
136        || bazel_test_return="$?"
137      if (( $bazel_test_return != 0 && $bazel_test_return != 4 )); then
138        return "${bazel_test_return}"
139      fi
140    fi
141    # Run specific manual targets.
142    if (( ${#MANUAL_TARGETS[@]} > 0 )); then
143      time "${BAZEL_CMD}" build "${CACHE_FLAGS[@]}" "${BUILD_FLAGS[@]}" -- \
144        "${MANUAL_TARGETS[@]}"
145      time "${BAZEL_CMD}" test "${CACHE_FLAGS[@]}" "${TEST_FLAGS[@]}"  -- \
146        "${MANUAL_TARGETS[@]}"
147    fi
148  )
149}
150
151main "$@"
152