#!/usr/bin/env bash
# Usage: script/test [--coverage [<MIN>]] [<FEATURES>...]
#
# Run Go and Cucumber test suites for hub.

set -e

while [ $# -gt 0 ]; do
  case "$1" in
  --coverage )
    export HUB_COVERAGE="$PWD/tmp/cover.out"
    if [ "${2%.*}" -gt 0 ] 2>/dev/null; then
      min_coverage="$2"
      shift 2
    else
      min_coverage=1
      shift 1
    fi
    ;;
  -h | --help )
    sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
    exit
    ;;
  * )
    break
    ;;
  esac
done

STATUS=0

trap "exit 1" INT

[ -z "$HUB_COVERAGE" ] || script/coverage prepare
script/build
script/build test || STATUS="$?"
script/ruby-test "$@" || STATUS="$?"

if [ -n "$HUB_COVERAGE" ]; then
  total_coverage="$(script/coverage generate)"
  echo "Code coverage: $total_coverage"
  result="$(bc <<<"${total_coverage%\%} < $min_coverage")"
  if [ "$result" -eq 1 ]; then
    echo "Error: coverage dropped below the minimum treshold of ${min_coverage}%!"
    if [ -n "$CI" ]; then
      html_result="${HUB_COVERAGE%.out}.html"
      html_result="${html_result#$PWD/}"
      printf 'Please run `script/test --coverage` locally and open `%s` to analyze the results.\n' "$html_result"
    fi
    STATUS=1
  fi
fi

if [ -n "$CI" ]; then
  make fmt >/dev/null
  if ! git diff -U1 --exit-code; then
    STATUS=1
    echo
    echo "Some go code was not formatted properly." >&2
    echo "Run \`make fmt' locally to fix these errors." >&2
  fi
fi

exit "$STATUS"
