#!/bin/sh

exec 2>&1
set -e
#set -x

tests="$@"

cleanup() {
  rm -rf "$ADTTMP"
}
if [ -z "$ADTTMP" ]; then
  ADTTMP=$(mktemp -d)
  trap cleanup INT TERM EXIT
fi

cp -r 'test/' $ADTTMP
cd $ADTTMP

if [ -z "$tests" ]; then
  # FIXME for now, we are excluding the tests for C extensions; couldn't figure
  # out how to properly build them without building everything else
  tests=$(find 'test/' -name 'test_*.rb' -and -not -path '*-ext-*' | sort)
fi

pass=0
fail=0

for t in $tests; do
  if ruby2.2 test/runner.rb $t >log 2>&1; then
    echo "PASS $t"
    pass=$(($pass + 1))
  else
    fail=$(($fail + 1))
    echo "FAIL $t"
    echo "FAIL $t" | sed -e 's/./-/g'
    echo
    cat log
  fi
done
rm -f log

echo
echo "Finished"
echo '--------'
echo " Ran: $(($pass+$fail))"
echo "PASS: $pass"
echo "FAIL: $fail"
echo

if [ $fail -gt 0 ]; then
  exit 1
fi
