github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/hack/verify.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -o errexit
     4  set -o nounset
     5  set -o pipefail
     6  
     7  # cd to the repo root
     8  REPO_ROOT=$(git rev-parse --show-toplevel)
     9  cd "${REPO_ROOT}"
    10  
    11  # Some useful colors.
    12  if [[ -z "${color_start-}" ]]; then
    13    declare -r color_start="\033["
    14    declare -r color_red="${color_start}0;31m"
    15    declare -r color_green="${color_start}0;32m"
    16    declare -r color_norm="${color_start}0m"
    17  fi
    18  
    19  # Excluded check patterns are always skipped.
    20  EXCLUDED_PATTERNS=(
    21    "verify.sh"                # this script calls the make rule and would cause a loop
    22    "verify-*-dockerized.sh"   # Don't run any scripts that intended to be run dockerized
    23  
    24    # TODO(verify): Enable these checks once their errors have been resolved
    25    # "verify-golangci-lint.sh"
    26    "verify-shellcheck.sh"
    27    "verify-build.sh"
    28  )
    29  
    30  EXCLUDED_CHECKS=$(ls ${EXCLUDED_PATTERNS[@]/#/${REPO_ROOT}\/hack\/} 2>/dev/null || true)
    31  
    32  function is-excluded {
    33    for e in ${EXCLUDED_CHECKS[@]}; do
    34      if [[ $1 -ef "$e" ]]; then
    35        return
    36      fi
    37    done
    38    return 1
    39  }
    40  
    41  function run-cmd {
    42    if ${SILENT}; then
    43      "$@" &> /dev/null
    44    else
    45      "$@"
    46    fi
    47  }
    48  
    49  # Collect failed tests in this array; initialize it to nil
    50  FAILED_TESTS=()
    51  
    52  function print-failed-tests {
    53    echo -e "========================"
    54    echo -e "${color_red}FAILED TESTS${color_norm}"
    55    echo -e "========================"
    56    for t in "${FAILED_TESTS[@]}"; do
    57        echo -e "${color_red}${t}${color_norm}"
    58    done
    59  }
    60  
    61  function run-checks {
    62    local -r pattern=$1
    63    local -r runner=$2
    64  
    65    local t
    66    local check_name
    67    local start
    68  
    69    for t in $(ls ${pattern})
    70    do
    71      check_name="$(basename "${t}")"
    72      if is-excluded "${t}" ; then
    73        echo "Skipping ${check_name}"
    74        continue
    75      fi
    76  
    77      echo -e "Verifying ${check_name}"
    78  
    79      start=$(date +%s)
    80      run-cmd "${runner}" "${t}" && tr=$? || tr=$?
    81      local elapsed=$(($(date +%s) - start))
    82  
    83      if [[ ${tr} -eq 0 ]]; then
    84        echo -e "${color_green}SUCCESS${color_norm}  ${check_name}\t${elapsed}s"
    85      else
    86        echo -e "${color_red}FAILED${color_norm}   ${check_name}\t${elapsed}s"
    87        ret=1
    88  
    89        FAILED_TESTS+=("${t}")
    90      fi
    91    done
    92  }
    93  
    94  SILENT=false
    95  
    96  while getopts ":s" opt; do
    97    case ${opt} in
    98      s)
    99        SILENT=true
   100        ;;
   101      \?)
   102        echo "Invalid flag: -${OPTARG}" >&2
   103        exit 1
   104        ;;
   105    esac
   106  done
   107  
   108  if ${SILENT} ; then
   109    echo "Running in silent mode, run without -s if you want to see script logs."
   110  fi
   111  
   112  ret=0
   113  run-checks "${REPO_ROOT}/hack/verify-*.sh" bash
   114  
   115  if [[ ${ret} -eq 1 ]]; then
   116    print-failed-tests
   117  fi
   118  
   119  exit ${ret}
   120  
   121  # ex: ts=2 sw=2 et filetype=sh