#!/usr/bin/env bash

TESTSDIR=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
failed=0
for test in $(find ${TESTSDIR} -maxdepth 1 -executable -type f | grep -v test.sh)
do
    returncode=$(grep $(basename ${test}) tests/returncodes.txt 2>/dev/null | cut -d : -f 2)
    if [[ "$returncode" == "" ]]; then
	returncode=0
    fi
    echo -n "$(basename ${test}) .... "
    (${test} >tmpfile 2>&1)
    if [[ $? -ne $returncode ]]; then
	echo "[!!]"
	echo
	cat tmpfile | sed s/'^'/'        '/g
	echo
	failed=$((failed + 1))
    else
	echo "[OK]"
    fi
done
echo "${failed} FAILED"
exit ${failed}
