#!/usr/bin/env bash
set -e

# Usage:
#   script/test <test_file>
#   script/test [ruby|jruby]
#   script/test

if [ -d test/dest ]
  then rm -r test/dest
fi

# -----------------------------------------------------------------------------
# If you send us a ruby then we use that, if you do not then we test with
# whatever we can detect, this way you can run both suites when you test out
# your source, we expect full coverage now, not just MRI.
# -----------------------------------------------------------------------------

if [[ "$1" == "ci" ]]
then
  rubies=(
    ruby
  )

  shift
elif [[ "$1" == "ruby" ]] || [[ "$1" == "jruby" ]]
then
  rubies=(
    $1
  )

  shift
else
  rubies=($(script/rubies))
fi

# Tests only pass when timezone offset is zero.
TZ=UTC

for ruby in $rubies; do
  if [[ "$ruby" == "jruby" ]]
  then
    testopts=""
  else
    testopts="--profile"
  fi

  if [[ $# -lt 1 ]]
  then
    set -x
    time $ruby -S bundle exec \
      rake TESTOPTS=$testopts test
   else
    set -x
    time $ruby -S bundle exec ruby -I test \
      "$@" $testops
  fi
done
