#!/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..."

# The test suite must never open a reportbug window (there is no test
# for the window itself, which would require a display), so remove any
# display from the environment and force the pkgos-reportbug fallback.
unset DISPLAY
unset WAYLAND_DISPLAY
PKGOS_DEBPIPY_NO_REPORTBUG=1
export PKGOS_DEBPIPY_NO_REPORTBUG

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
