...

Text file src/github.com/transparency-dev/merkle/scripts/presubmit.sh

Documentation: github.com/transparency-dev/merkle/scripts

     1#!/bin/bash
     2#
     3# Presubmit checks for this repository.
     4#
     5# Checks for lint errors, spelling, licensing, correct builds / tests and so on.
     6# Flags may be specified to allow suppressing of checks or automatic fixes, try
     7# `scripts/presubmit.sh --help` for details.
     8#
     9# Globals:
    10#   GO_TEST_TIMEOUT: timeout for 'go test'. Optional (defaults to 5m).
    11set -eu
    12
    13check_pkg() {
    14  local cmd="$1"
    15  local pkg="$2"
    16  check_cmd "$cmd" "try running 'go get -u $pkg'"
    17}
    18
    19check_cmd() {
    20  local cmd="$1"
    21  local msg="$2"
    22  if ! type -p "${cmd}" > /dev/null; then
    23    echo "${cmd} not found, ${msg}"
    24    return 1
    25  fi
    26}
    27
    28usage() {
    29  echo "$0 [--coverage] [--fix] [--no-mod-tidy] [--no-build] [--no-linters]"
    30}
    31
    32main() {
    33  local coverage=0
    34  local fix=0
    35  local run_mod_tidy=1
    36  local run_build=1
    37  local run_lint=1
    38  while [[ $# -gt 0 ]]; do
    39    case "$1" in
    40      --coverage)
    41        coverage=1
    42        ;;
    43      --fix)
    44        fix=1
    45        ;;
    46      --no-mod-tidy)
    47        run_mod_tidy=0
    48        ;;
    49      --help)
    50        usage
    51        exit 0
    52        ;;
    53      --no-build)
    54        run_build=0
    55        ;;
    56      --no-linters)
    57        run_lint=0
    58        ;;
    59      *)
    60        usage
    61        exit 1
    62        ;;
    63    esac
    64    shift 1
    65  done
    66
    67  cd "$(dirname "$0")"  # at scripts/
    68  cd ..  # at top level
    69
    70  go_srcs="$(find . -name '*.go' | \
    71    grep -v mock_ | \
    72    grep -v .pb.go | \
    73    grep -v _string.go | \
    74    grep -v .shims.go | \
    75    tr '\n' ' ')"
    76
    77  if [[ "$fix" -eq 1 ]]; then
    78    check_pkg goimports golang.org/x/tools/cmd/goimports || exit 1
    79
    80    echo 'running gofmt'
    81    gofmt -s -w ${go_srcs}
    82    echo 'running goimports'
    83    goimports -w ${go_srcs}
    84    if [[ "$run_mod_tidy" -eq 1 ]]; then
    85      echo 'running go mod tidy'
    86      go mod tidy
    87    fi
    88  fi
    89
    90  if [[ "${run_build}" -eq 1 ]]; then
    91    echo 'running go build'
    92    go build ./...
    93
    94    export TEST_FLAGS="-timeout=${GO_TEST_TIMEOUT:-5m}"
    95
    96    if [[ ${coverage} -eq 1 ]]; then
    97      TEST_FLAGS+=" -covermode=atomic -coverprofile=coverage.txt"
    98    fi
    99
   100    echo "running go test ${TEST_FLAGS} ./..."
   101    go test ${TEST_FLAGS} ./...
   102  fi
   103
   104  if [[ "${run_lint}" -eq 1 ]]; then
   105    check_cmd golangci-lint \
   106      'have you installed github.com/golangci/golangci-lint?' || exit 1
   107
   108    echo 'running golangci-lint'
   109    golangci-lint run --deadline=8m
   110    echo 'checking license headers'
   111    ./scripts/check_license.sh ${go_srcs}
   112  fi
   113}
   114
   115main "$@"

View as plain text