#! /bin/bash

if [ ! -d test ]
then
    echo 'Needs to called in root of package (outside "test" directory).'
    exit 11
fi

errorsfound=0

# Function to do the test for us
_do_test () {

    packagetest="$1"                      # Setup the test to be done

    $packagetest                          # Do the test
    errorcode=$?                          # Get the error code

    if [ $errorcode -ne 0 ]               # If failed tell user, and increase error count
    then

        tput setaf 1 2> /dev/null
        echo "$packagetest exited with error code $errorcode!"
        errorsfound=$((errorsfound + 1))

    else                                  # If passed, tell for fun too

        tput setaf 2 2> /dev/null
        echo "$packagetest passed!"

    fi

    tput sgr0 2> /dev/null                # Reset colors

}

# Check if we should test build Sphinx documentation
if [ -f docs/conf.py ] && [ "$nodoc" != "true" ]
then

    test -f docs/requirements.txt && pip install -r docs/requirements.txt
    _do_test "sphinx-build -W -b html -E docs test/html"

fi


# Now do the tests inside the test directory
cd test

for packagetest in test_*
do

    _do_test "./$packagetest"

done

if [ $errorsfound -eq 1 ]                 # Setting plural correctly
then                                      #   looks impressive to some
    errstr="error"
else
    errstr="errors"
fi

if [ $errorsfound -eq 0 ]                 # Set terminal text color depending on result
then
    tput setaf 2 2> /dev/null             # Green: Passed test
else
    tput setaf 1 2> /dev/null             # Red: Did not pass test
fi

echo "$errorsfound $errstr found"

tput sgr0 2> /dev/null                    # Reset color

exit $errorsfound
