github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/hack/bazel-test-affected.sh (about) 1 #!/bin/bash 2 # Copyright 2017 The Kubernetes Authors. 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 # Usage: 17 # The script builds and run tests affected by modified files. 18 # 19 # The list of modified files is created based on the git-diff options passed 20 # as parameters to this command. 21 # 22 # Examples: 23 # bazel-test-affected.sh HEAD # Default: Use unstaged/uncommitted changes 24 # bazel-test-affected.sh --staged # Check build/tests affected by staged files 25 # bazel-test-affected.sh origin/master... # Check files changed off master 26 # bazel-test-affected.sh HEAD~ # Check build/tests affected by previous commit 27 # 28 # Please refer to `man git-diff` for more possible syntax. 29 30 set -o errexit 31 set -o nounset 32 set -o pipefail 33 34 # Compute list of modified files in bazel package form. 35 packages=($(bazel query \ 36 --noshow_progress \ 37 "set($(git diff --name-only --diff-filter=d "${@}"))")) 38 if [[ "${#packages[@]}" == 0 ]]; then 39 echo "No bazel packages affected." 40 exit 0 41 fi 42 43 # Build modified packages. 44 buildables=$(bazel query \ 45 --keep_going \ 46 --noshow_progress \ 47 "kind(.*_binary, rdeps(//..., set(${packages[@]})))") 48 if [[ ! -z "${buildables}" ]]; then 49 bazel build ${buildables} 50 fi 51 52 # Run affected tests. 53 tests=$(bazel query \ 54 --keep_going \ 55 --noshow_progress \ 56 "kind(test, rdeps(//..., set(${packages[@]})))") 57 bazel test --test_output=errors ${tests}