|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Careful! `set -e` doesn't do everything you'd think it does. In |
| 4 | +# fact, we don't get its benefit in any of the `run_foo` functions. |
| 5 | +# |
| 6 | +# This is because it has an effect only when it can exit the whole shell. |
| 7 | +# (Its full name is `set -o errexit`, and it means "exit" literally.) See: |
| 8 | +# https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin |
| 9 | +# |
| 10 | +# When one test suite fails, we want to go on to run the other suites, so |
| 11 | +# we use `||` to prevent the whole script from exiting there, and that |
| 12 | +# defeats `set -e`. |
| 13 | +# |
| 14 | +# For now our workaround is to put `|| return` in the `run_foo` just |
| 15 | +# after each nontrivial command that isn't the final command in the |
| 16 | +# function. |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | + |
| 20 | +## CLI PARSING |
| 21 | + |
| 22 | +default_suites=(analyze test) |
| 23 | +extra_suites=( |
| 24 | +) |
| 25 | + |
| 26 | +usage() { |
| 27 | + cat >&2 <<EOF |
| 28 | +usage: tools/check [SUITE]... |
| 29 | +
|
| 30 | +Run our tests. |
| 31 | +
|
| 32 | +By default, run ${#default_suites[@]} suite(s): |
| 33 | + ${default_suites[*]} |
| 34 | +and skip ${#extra_suites[@]} suite(s): |
| 35 | + ${extra_suites[*]} |
| 36 | +EOF |
| 37 | + exit 2 |
| 38 | +} |
| 39 | + |
| 40 | +opt_suites=() |
| 41 | +while (( $# )); do |
| 42 | + case "$1" in |
| 43 | + analyze|test) |
| 44 | + opt_suites+=("$1"); shift;; |
| 45 | + *) usage;; |
| 46 | + esac |
| 47 | +done |
| 48 | + |
| 49 | +if (( ! "${#opt_suites[@]}" )); then |
| 50 | + opt_suites=( "${default_suites[@]}" ) |
| 51 | +fi |
| 52 | + |
| 53 | + |
| 54 | +## EXECUTION |
| 55 | + |
| 56 | +rootdir=$(git rev-parse --show-toplevel) |
| 57 | +cd "$rootdir" |
| 58 | + |
| 59 | +run_analyze() { |
| 60 | + flutter analyze |
| 61 | +} |
| 62 | + |
| 63 | +run_test() { |
| 64 | + flutter test |
| 65 | +} |
| 66 | + |
| 67 | +failed=() |
| 68 | +for suite in "${opt_suites[@]}"; do |
| 69 | + echo "Running $suite..." |
| 70 | + case "$suite" in |
| 71 | + analyze) run_analyze ;; |
| 72 | + test) run_test ;; |
| 73 | + *) echo >&2 "Internal error: unknown suite $suite" ;; |
| 74 | + esac || failed+=( "$suite" ) |
| 75 | +done |
| 76 | + |
| 77 | +if (( ${#failed[@]} )); then |
| 78 | + cat >&2 <<EOF |
| 79 | +
|
| 80 | +FAILED: ${failed[*]} |
| 81 | +
|
| 82 | +To rerun the suites that failed, run: |
| 83 | + $ tools/check ${failed[*]} |
| 84 | +EOF |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +echo "Passed!" |
0 commit comments