github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/tools/test (about)

     1  #!/bin/bash
     2  
     3  set -e
     4  
     5  DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
     6  SLOW=
     7  NO_GO_GET=true
     8  TAGS=
     9  PARALLEL=
    10  RACE="-race -covermode=atomic"
    11  TIMEOUT=1m
    12  VERBOSE=
    13  
    14  usage() {
    15      echo "$0 [-slow] [-in-container foo] [-netgo] [-(no-)go-get] [-timeout 1m]"
    16  }
    17  
    18  while [ $# -gt 0 ]; do
    19      case "$1" in
    20          "-v")
    21              VERBOSE="-v"
    22              shift 1
    23              ;;
    24          "-slow")
    25              SLOW=true
    26              shift 1
    27              ;;
    28          "-no-race")
    29              RACE=
    30              shift 1
    31              ;;
    32          "-no-go-get")
    33              NO_GO_GET=true
    34              shift 1
    35              ;;
    36          "-go-get")
    37              NO_GO_GET=
    38              shift 1
    39              ;;
    40          "-netgo")
    41              TAGS="netgo"
    42              shift 1
    43              ;;
    44          "-tags")
    45              TAGS="$2"
    46              shift 2
    47              ;;
    48          "-p")
    49              PARALLEL=true
    50              shift 1
    51              ;;
    52          "-timeout")
    53              TIMEOUT=$2
    54              shift 2
    55              ;;
    56          *)
    57              usage
    58              exit 2
    59              ;;
    60      esac
    61  done
    62  
    63  GO_TEST_ARGS=(-tags "${TAGS[@]}" -cpu 4 -timeout $TIMEOUT $VERBOSE)
    64  
    65  if [ -n "$SLOW" ] || [ -n "$CIRCLECI" ]; then
    66      SLOW=true
    67  fi
    68  
    69  if [ -n "$SLOW" ]; then
    70      GO_TEST_ARGS=("${GO_TEST_ARGS[@]}" ${RACE})
    71  
    72      # shellcheck disable=SC2153
    73      if [ -n "$COVERDIR" ]; then
    74          coverdir="$COVERDIR"
    75      else
    76          coverdir=$(mktemp -d coverage.XXXXXXXXXX)
    77      fi
    78  
    79      mkdir -p "$coverdir"
    80  fi
    81  
    82  fail=0
    83  
    84  if [ -z "$TESTDIRS" ]; then
    85      # NB: Relies on paths being prefixed with './'.
    86      TESTDIRS=($(git ls-files -- '*_test.go' | grep -vE '^(vendor|experimental)/' | xargs -n1 dirname | sort -u | sed -e 's|^|./|'))
    87  else
    88      # TESTDIRS on the right side is not really an array variable, it
    89      # is just a string with spaces, but it is written like that to
    90      # shut up the shellcheck tool.
    91      TESTDIRS=($(for d in ${TESTDIRS[*]}; do echo "$d"; done))
    92  fi
    93  
    94  # If running on circle, use the scheduler to work out what tests to run on what shard
    95  if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then
    96      PREFIX=$(go list -e ./ | sed -e 's/\//-/g')
    97      TESTDIRS=($(echo "${TESTDIRS[@]}" | "$DIR/sched" sched "$PREFIX-$CIRCLE_PROJECT_USERNAME-$CIRCLE_PROJECT_REPONAME-$CIRCLE_BUILD_NUM" "$CIRCLE_NODE_TOTAL" "$CIRCLE_NODE_INDEX"))
    98      echo "${TESTDIRS[@]}"
    99  fi
   100  
   101  PACKAGE_BASE=$(go list -e ./)
   102  
   103  # Speed up the tests by compiling and installing their dependencies first.
   104  go test -i "${GO_TEST_ARGS[@]}" "${TESTDIRS[@]}"
   105  
   106  run_test() {
   107      local dir=$1
   108      if [ -z "$NO_GO_GET" ]; then
   109          go get -t -tags "${TAGS[@]}" "$dir"
   110      fi
   111  
   112      local GO_TEST_ARGS_RUN=("${GO_TEST_ARGS[@]}")
   113      if [ -n "$SLOW" ]; then
   114          local COVERPKGS
   115          COVERPKGS=$( (
   116              go list "$dir"
   117              go list -f '{{join .Deps "\n"}}' "$dir" | grep -v "vendor" | grep "^$PACKAGE_BASE/"
   118          ) | paste -s -d, -)
   119          local output
   120          output=$(mktemp "$coverdir/unit.XXXXXXXXXX")
   121          local GO_TEST_ARGS_RUN=("${GO_TEST_ARGS[@]}" -coverprofile=$output -coverpkg=$COVERPKGS)
   122      fi
   123  
   124      local START
   125      START=$(date +%s)
   126      if ! go test "${GO_TEST_ARGS_RUN[@]}" "$dir"; then
   127          fail=1
   128      fi
   129      local END
   130      END=$(date +%s)
   131      local RUNTIME=$((END - START))
   132  
   133      # Report test runtime when running on circle, to help scheduler
   134      if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then
   135          "$DIR/sched" time "$dir" "$RUNTIME"
   136      fi
   137  }
   138  
   139  for dir in "${TESTDIRS[@]}"; do
   140      if [ -n "$PARALLEL" ]; then
   141          run_test "$dir" &
   142      else
   143          run_test "$dir"
   144      fi
   145  done
   146  
   147  if [ -n "$PARALLEL" ]; then
   148      wait
   149  fi
   150  
   151  if [ -n "$SLOW" ] && [ -z "$COVERDIR" ]; then
   152      go get github.com/weaveworks/tools/cover
   153      cover "$coverdir"/* >profile.cov
   154      rm -rf "$coverdir"
   155      go tool cover -html=profile.cov -o=coverage.html
   156      go tool cover -func=profile.cov | tail -n1
   157  fi
   158  
   159  exit $fail