#!/bin/bash
#
# runAll.sh <path/to/svn>
#
# Run all tests in the tests directory, summarizing the results at the
# end.
die() {
    echo "$1" >>/dev/tty
    exit 1
}

SVN="$1"

[[ $# = 1 && -x $SVN ]] ||
    die "usage: $0 <path/to/svn>"

if [[ ! $SVN =~ ^/ ]]; then
    # Make $SVN path absolute.
    SVN="$PWD/$SVN"
fi

# Create arrays to accumulate results.
declare -a PASSED
declare -a FAILED

testtopleveldir=$(dirname $0)
numTestsRun=0
for testScript in $testtopleveldir/tests/*
do
    testdir="testing/$testScript"
    testName=$(basename $testScript)
    echo "--------------------------------------------------"
    echo "             $testName"
    echo "--------------------------------------------------"
    numTestsRun=$((numTestsRun + 1))

    rm -rf "$testdir"
    mkdir -p "$testdir"
    if (cd "$testdir" && "$testScript" "$SVN"); then
        PASSED[$numTestsRun]="$testName"
        result=PASSED
    else
        FAILED[$numTestsRun]="$testName"
        result=FAILED
    fi

    echo "--------------------------------------------------"
    echo "             ${testName}:$result"
    echo "--------------------------------------------------"
done

# Print results.
EXITCODE=0  # So far
echo
echo "Results"
echo "-------"
for testName in "${PASSED[@]}"
do
    echo "PASSED: $testName"
done
for testName in "${FAILED[@]}"
do
    echo "FAILED: $testName"
    EXITCODE=1
done

exit $EXITCODE
