github.com/GoogleCloudPlatform/testgrid@v0.0.174/hack/check-pr.sh (about)

     1  #!/bin/bash
     2  # Copyright 2018 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: check-pr [ref]
    17  #
    18  # Ideally if check-pr passes then your golang PR will pass presubmit tests.
    19  
    20  set -o nounset
    21  set -o errexit
    22  set -o pipefail
    23  
    24  cd $(git rev-parse --show-toplevel)
    25  
    26  dirs=()
    27  tests=()
    28  ref="${1:-HEAD}"
    29  echo -n "Packages changed since $ref: "
    30  for d in $(git diff --name-only "$ref" | xargs -n 1 dirname | sort -u); do
    31      if ! ls "./$d/"*.go &> /dev/null; then
    32          continue
    33      fi
    34      echo -n "$d "
    35      dirs+=("./$d")
    36      tests+=("//$d:all")
    37  done
    38  
    39  if [[ ${#dirs[@]} == 0 ]]; then
    40      echo NONE
    41      exit 0
    42  fi
    43  echo
    44  
    45  failing=()
    46  # step <name> <command ...> runs command and prints the output if it fails.
    47  # if no <command> is specified, <name> is used as the command.
    48  step() {
    49      name="$1"
    50      shift
    51      cmd="$@"
    52  
    53      echo -n "Running ${name}... "
    54      tmp="$(mktemp)"
    55      if [[ -z "${cmd}" ]]; then
    56          cmd="${name}"
    57      fi
    58      if ! ${cmd} &> "$tmp"; then
    59          echo FAIL:
    60          cat "$tmp"
    61          rm -f "$tmp"
    62          failing+=("${name}")
    63          return 0
    64      fi
    65      rm -f "$tmp"
    66      echo PASS
    67      return 0
    68  }
    69  
    70  step "//hack:verify-all" bazel test //hack:verify-all
    71  step "bazel test" bazel test --build_tests_only "${tests[@]}"
    72  
    73  if [[ "${#failing[@]}" != 0 ]]; then
    74      echo "FAILURE: ${#failing[@]} steps failed: ${failing[@]}"
    75      exit 1
    76  fi
    77  echo "SUCCESS"