mbland/custom-links

View on GitHub
scripts/test

Summary

Maintainability
Test Coverage
#! /bin/bash
# 
# Run all automated tests
#
# Usage:
#   {{go}} {{cmd}} [--coverage]
#
# Options:
#   --coverage    Collect test coverage data using nyc/istanbul
#
# The '--coverage' flag will place its output in a directory called 'coverage'.
# An HTML report will be available at 'coverage/lcov-report/index.html'.

# Disable Karma for Windows Subsystem for Linux runs
if [[ -z "$DISABLE_KARMA" ]] &&
  node -e "process.exit(require('is-wsl') ? 0 : 1)"; then
  export DISABLE_KARMA='true'
fi

_test_tab_completion() {
  local word_index="$1"
  shift
  local args=("$@")
  local word="${args[$word_index]}"

  if [[ "$word_index" -eq '0' ]]; then
    @go.compgen -W '--coverage' -- "$1"
  fi
}

_test() {
  local coverage_run
  local flags=()
  local result='0'

  case "$1" in
  --complete)
    # Tab completions
    shift
    _test_tab_completion "$@"
    return
    ;;
  --coverage)
    coverage_run='true'
    flags=('--coverage')
    . "$_GO_USE_MODULES" 'coverage'
    shift
    ;;
  esac

  if [[ "$#" -ne '0' ]]; then
    @go.printf 'Unknown argument%s: %s\n' "${2:+s}" "$*" >&2
    return 1
  fi

  export __TEST_ALL='true'
  export MOCHA_COLORS='true'
  export FORCE_COLOR='true'

  @go.log START 'Running all automated tests...'

  if [[ -n "$coverage_run" ]] && ! cl.init_code_climate; then
    result='1'
  fi
  if ! @go.log_command @go test server "${flags[@]}"; then
    result='1'
  fi
  if ! @go.log_command @go test browser "${flags[@]}"; then
    result='1'
  fi
  if [[ "$DISABLE_KARMA" != 'true' ]] &&
    ! @go.log_command karma start --single-run; then
    result='1'
  fi
  if ! @go.log_command @go test end-to-end; then
    result='1'
  fi
  if ! @go.log_command @go test scripts; then
    result='1'
  fi
  if [[ -n "$coverage_run" ]] && ! cl.generate_coverage_report "$result"; then
    result='1'
  fi
  if [[ "$result" -eq '0' ]]; then
    @go.log FINISH 'All automated tests finished successfully.'
  else
    @go.log ERROR 'One or more of the tests failed; see results above.'
  fi
  return "$result"
}

_test "$@"