#!/bin/sh

set -e

# This is a basic test runner for shell script tests
# it just searches for all test_ scripts under the tests
# folder, and run them.

GREEN="\033[1;32m"
RED="\033[1;31m"
NO_COL="\033[0m"

echo "Running shell script unit tests..."

CURDIR=$(pwd)
SUCCESS=yes
CNT=0
for i in $(find tests -type f -iname 'test_*') ; do
	DIRNAME=$(dirname $i)
	BASENAME=$(basename $i)
	cd ${DIRNAME}
	echo -n $i" ... "
	if ./${BASENAME} ; then
		echo "${GREEN}OK${NO_COL}"
	else
		SUCCESS=no
		echo "${RED}FAILED${NO_COL}"
	fi
	cd ${CURDIR}
	CNT=$(( ${CNT} + 1 ))
done

if [ "${SUCCESS}" = "yes" ] ; then
	echo "${GREEN}Successfully ran ${CNT} tests :)${NO_COL}"
	exit 0
else
	echo "${RED}Failure while running unit tests :(${NO_COL}"
	exit 1
fi
