kythe.io@v0.0.68-0.20240422202219-7225dbc01741/tools/git/test-affected-targets.sh (about) 1 #!/bin/bash 2 # Copyright 2021 The Kythe Authors. All rights reserved. 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 # A pre-commit hook which will determine and run affected test targets using the 17 # files passed on the command line. 18 # Usage: test-affected-targets.sh file... 19 set -e 20 OMIT_TAGS=(manual broken arc-ignore docker) 21 22 function join_by { 23 local d="$1" 24 shift 25 local f="$1" 26 shift 27 printf %s "$f" "${@/#/$d}"; 28 } 29 30 TAG_RE="\\b($(join_by '|' "${OMIT_TAGS[@]}"))\\b" 31 32 readarray -t TARGETS < <(bazel query \ 33 --keep_going \ 34 --noshow_progress \ 35 "let exclude = attr('tags', '$TAG_RE', //...) in rdeps(//... except \$exclude, set($*)) except \$exclude") 36 37 if [[ "${#TARGETS[@]}" -gt 0 ]]; then 38 echo "Building targets" 39 bazel build --config=prepush "${TARGETS[@]}" 40 fi 41 42 readarray -t TESTS < <(bazel query \ 43 --keep_going \ 44 --noshow_progress \ 45 "let expr = tests(set(${TARGETS[*]})) in \$expr except attr('tags', '$TAG_RE', \$expr)") 46 47 if [[ "${#TESTS[@]}" -gt 0 ]]; then 48 echo "Running tests" 49 bazel test --config=prepush "${TESTS[@]}" 50 fi