|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# This script is modeled in part on Cirq's check/all script. |
| 18 | + |
| 19 | +declare -r usage="\ |
| 20 | +Usage: ${0##*/} [--help] [--only-changed-files] [--apply-format-changes] [BASE_REV] |
| 21 | +
|
| 22 | +If the first argument given on the command line is the option --help or -h, |
| 23 | +this program prints usage information and then exits. |
| 24 | +
|
| 25 | +With no arguments, this runs multiple checks on the code base. These tests |
| 26 | +are based on the programs in the checks/ subdirectory. |
| 27 | +
|
| 28 | +If --apply-format-changes is specified, the flag --apply will be passed to |
| 29 | +check/format-incremental to apply the format changes suggested by the |
| 30 | +formatter. |
| 31 | +
|
| 32 | +You can specify a base git revision to compare against (i.e., to use when |
| 33 | +determining whether or not a file is considered to have changed). If given, |
| 34 | +the argument BASE_REV is passed on to tests that can use it, such as |
| 35 | +check/pytest-and-incremental-coverage." |
| 36 | + |
| 37 | +set -eo pipefail -o errtrace |
| 38 | +shopt -s inherit_errexit |
| 39 | + |
| 40 | +# Get the working directory to the repo root. |
| 41 | +thisdir=$(dirname "${BASH_SOURCE[0]:?}") |
| 42 | +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) |
| 43 | +cd "${repo_dir}" |
| 44 | + |
| 45 | +function error() { |
| 46 | + echo >&2 "ERROR: ${*}" |
| 47 | +} |
| 48 | + |
| 49 | +# ~~~~ Parse the CLI arguments and gather some data ~~~~ |
| 50 | + |
| 51 | +declare -a rev=() |
| 52 | +declare -a apply_arg=() |
| 53 | +declare only_changed="" |
| 54 | +for arg in "$@"; do |
| 55 | + case "${arg}" in |
| 56 | + -h | --help) |
| 57 | + echo "${usage}" |
| 58 | + exit 0 |
| 59 | + ;; |
| 60 | + --apply-format-changes) |
| 61 | + apply_arg=( "--apply" ) |
| 62 | + shift |
| 63 | + ;; |
| 64 | + --only-changed-files) |
| 65 | + only_changed="true" |
| 66 | + shift |
| 67 | + ;; |
| 68 | + -*) |
| 69 | + error "Invalid option '${arg}'" |
| 70 | + error "See '$0 --help' for the list of supported options." |
| 71 | + exit 1 |
| 72 | + ;; |
| 73 | + *) |
| 74 | + if ! rev=( "$(git rev-parse --verify --end-of-options "${arg}^{commit}")" ); then |
| 75 | + error "No revision '${arg}'" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + ;; |
| 79 | + esac |
| 80 | +done |
| 81 | + |
| 82 | +# ~~~~ Run the tests ~~~~ |
| 83 | + |
| 84 | +declare -a errors=() |
| 85 | + |
| 86 | +function run() { |
| 87 | + echo "Running $* ..." |
| 88 | + "$@" || errors+=( "$* failed" ) |
| 89 | + echo |
| 90 | +} |
| 91 | + |
| 92 | +if [[ -n "${only_changed}" ]]; then |
| 93 | + run check/format-incremental "${rev[@]}" "${apply_arg[@]}" |
| 94 | + run check/pylint-changed-files "${rev[@]}" |
| 95 | +else |
| 96 | + run check/format-incremental "${rev[@]}" "${apply_arg[@]}" --all |
| 97 | + run check/pylint "${rev[@]}" |
| 98 | +fi |
| 99 | +run check/mypy |
| 100 | +run check/pytest-and-incremental-coverage "${rev[@]}" |
| 101 | +run check/shellcheck |
| 102 | + |
| 103 | +# ~~~~ Summarize the results and exit with a status code ~~~~ |
| 104 | + |
| 105 | +declare exit_code=0 |
| 106 | +echo |
| 107 | +echo "Done." |
| 108 | +if [[ "${#errors[@]}" == 0 ]]; then |
| 109 | + echo "All checks passed." |
| 110 | +else |
| 111 | + error "Some checks failed." |
| 112 | + printf " %s\n" "${errors[@]}" |
| 113 | + exit_code=1 |
| 114 | +fi |
| 115 | + |
| 116 | +exit "${exit_code}" |
0 commit comments