|
| 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 | +# This script requires the following environment variables to be set: |
| 17 | +# `BUILD_TYPE` should be one of ["presubmit", "continuous"] |
| 18 | +# `TEST_TYPE` should be one of ["docs", "docfx", "prerelease", "unit"] |
| 19 | +# or match the name of the nox session that you want to run. |
| 20 | +# `PY_VERSION` should be one of ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] |
| 21 | + |
| 22 | +# `TEST_TYPE` and `PY_VERSION` are required by the script `ci/run_single_test.sh` |
| 23 | + |
| 24 | +# This script will determine which directories have changed |
| 25 | +# under the `packages` folder. For `BUILD_TYPE=="presubmit"`, |
| 26 | +# we'll compare against the `packages` folder in HEAD, |
| 27 | +# whereas for `BUILD_TYPE=="continuous"` we'll compare changes |
| 28 | +# with HEAD~1. For all directories that have changed files, we will |
| 29 | +# run the script located at `${PROJECT_ROOT}/ci/run_single_test.sh`. |
| 30 | + |
| 31 | +# `-e` enables the script to automatically fail when a command fails |
| 32 | +# `-o pipefail` sets the exit code to non-zero if any command fails, |
| 33 | +# or zero if all commands in the pipeline exit successfully. |
| 34 | +set -eo pipefail |
| 35 | + |
| 36 | +export PROJECT_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")/..) |
| 37 | + |
| 38 | +# A script file for running the test in a sub project. |
| 39 | +test_script="${PROJECT_ROOT}/ci/run_single_test.sh" |
| 40 | + |
| 41 | +if [ ${BUILD_TYPE} == "presubmit" ]; then |
| 42 | + # For presubmit build, we want to know the difference from the |
| 43 | + # common commit in origin/main. |
| 44 | + GIT_DIFF_ARG="origin/main..." |
| 45 | + |
| 46 | + # Then fetch enough history for finding the common commit. |
| 47 | + git fetch origin main --deepen=200 |
| 48 | + |
| 49 | +elif [ ${BUILD_TYPE} == "continuous" ]; then |
| 50 | + # For continuous build, we want to know the difference in the last |
| 51 | + # commit. This assumes we use squash commit when merging PRs. |
| 52 | + GIT_DIFF_ARG="HEAD~.." |
| 53 | + |
| 54 | + # Then fetch one last commit for getting the diff. |
| 55 | + git fetch origin main --deepen=1 |
| 56 | + |
| 57 | +else |
| 58 | + # Run everything. |
| 59 | + GIT_DIFF_ARG="" |
| 60 | +fi |
| 61 | + |
| 62 | +# Then detect changes in the test scripts. |
| 63 | + |
| 64 | +set +e |
| 65 | +git diff --quiet ${GIT_DIFF_ARG} ci |
| 66 | +changed=$? |
| 67 | +set -e |
| 68 | + |
| 69 | +# Now we have a fixed list, but we can change it to autodetect if |
| 70 | +# necessary. |
| 71 | + |
| 72 | +subdirs=( |
| 73 | + packages |
| 74 | +) |
| 75 | + |
| 76 | +RETVAL=0 |
| 77 | + |
| 78 | +for subdir in ${subdirs[@]}; do |
| 79 | + for d in `ls -d ${subdir}/*/`; do |
| 80 | + should_test=false |
| 81 | + if [ -n "${GIT_DIFF_ARG}" ]; then |
| 82 | + echo "checking changes with 'git diff --quiet ${GIT_DIFF_ARG} ${d}'" |
| 83 | + set +e |
| 84 | + git diff --quiet ${GIT_DIFF_ARG} ${d} |
| 85 | + changed=$? |
| 86 | + set -e |
| 87 | + if [[ "${changed}" -eq 0 ]]; then |
| 88 | + echo "no change detected in ${d}, skipping" |
| 89 | + else |
| 90 | + echo "change detected in ${d}" |
| 91 | + should_test=true |
| 92 | + fi |
| 93 | + else |
| 94 | + # If GIT_DIFF_ARG is empty, run all the tests. |
| 95 | + should_test=true |
| 96 | + fi |
| 97 | + if [ "${should_test}" = true ]; then |
| 98 | + echo "running test in ${d}" |
| 99 | + pushd ${d} |
| 100 | + # Temporarily allow failure. |
| 101 | + set +e |
| 102 | + ${test_script} |
| 103 | + ret=$? |
| 104 | + set -e |
| 105 | + if [ ${ret} -ne 0 ]; then |
| 106 | + RETVAL=${ret} |
| 107 | + fi |
| 108 | + popd |
| 109 | + fi |
| 110 | + done |
| 111 | +done |
| 112 | + |
| 113 | +exit ${RETVAL} |
0 commit comments