github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/script/test-unit (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  RED=$'\033[31m'
     5  GREEN=$'\033[32m'
     6  TEXTRESET=$'\033[0m' # reset the foreground colour
     7  
     8  # This helper function walks the current directory looking for directories
     9  # holding certain files ($1 parameter), and prints their paths on standard
    10  # output, one per line.
    11  find_dirs() {
    12      find . -not \( \
    13           \( \
    14           -path './integration/*' \
    15           -o -path './.git/*' \
    16           -o -path './vendor/*' \
    17           -o -path './bundles/*' \
    18           \) \
    19           -prune \
    20           \) -name "$1" -print0 | xargs -0n1 dirname | sort -u
    21  }
    22  
    23  TESTFLAGS="-cover -coverprofile=cover.out ${TESTFLAGS}"
    24  
    25  if [ -z "$TESTDIRS" ]; then
    26      TESTDIRS=$(find_dirs '*_test.go')
    27  fi
    28  
    29  TESTS_FAILED=()
    30  
    31  set +e
    32  for dir in $TESTDIRS; do
    33      echo '+ go test -race' $TESTFLAGS "${LIBCOMPOSE_PKG}/${dir#./}"
    34      go test -race ${TESTFLAGS} "${LIBCOMPOSE_PKG}/${dir#./}"
    35      if [ $? != 0 ]; then
    36          TESTS_FAILED+=("$dir")
    37          echo
    38          echo "${RED}Tests failed: ${LIBCOMPOSE_PKG}${TEXTRESET}"
    39          sleep 1 # give it a second, so observers watching can take note
    40      fi
    41  done
    42  
    43  echo
    44  echo "Run non-race test (if any)"
    45  for dir in $TESTDIRS; do
    46      if [ -n "$(grep --include '*_test.go' -l -ri '!race' ${dir})" ]; then
    47          echo '+ go test ' $TESTFLAGS "${LIBCOMPOSE_PKG}/${dir#./}"
    48          go test ${TESTFLAGS} "${LIBCOMPOSE_PKG}/${dir#./}"
    49          if [ $? != 0 ]; then
    50              TESTS_FAILED+=("$dir")
    51              echo
    52              echo "${RED}Tests failed: ${LIBCOMPOSE_PKG}${TEXTRESET}"
    53              sleep 1 # give it a second, so observers watching can take note
    54          fi
    55      fi
    56  done
    57  set -e
    58  
    59  echo
    60  
    61  # if some tests fail, we want the bundlescript to fail, but we want to
    62  # try running ALL the tests first, hence TESTS_FAILED
    63  if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
    64      echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
    65      echo
    66      false
    67  else
    68      echo "${GREEN}Test success${TEXTRESET}"
    69      echo
    70      true
    71  fi