#!/bin/sh

set -e

ME=$(basename $0)
RUNNER=$(readlink -f ../../../build-tools/pkgos-run-tests)
FIXTURES=$(readlink -f ./fixtures)

# Proves that progress is printed while the suite runs, and not only
# once it is finished: the runner's output goes to a log file, which
# suite-c/test_progress reads while it runs, waiting for its own
# Running line to show up in it. With PKGOS_RUN_TESTS_LOG unset, that
# fixture just passes, so that it is harmless when found by the rest
# of the suite.
LOG=$(mktemp /tmp/pkgos-run-tests-progress.XXXXXX)
trap 'rm -f ${LOG}' EXIT
PKGOS_RUN_TESTS_LOG=${LOG}
export PKGOS_RUN_TESTS_LOG

set +e
"${RUNNER}" "${FIXTURES}" >"${LOG}" 2>&1
RC=$?
set -e

if [ "${RC}" != "0" ] ; then
	echo "${ME}: runner must exit 0 when every test passes, got ${RC}:"
	cat "${LOG}"
	exit 1
fi
case "$(cat ${LOG})" in
*"Running 4 shell script unit tests from 3 folders"*" (up to"*) ;;
*)
	echo "${ME}: the progress header is missing:"
	cat "${LOG}"
	exit 1
;;
esac
case "$(cat ${LOG})" in
*"suite-c/test_progress ... OK"*) ;;
*)
	echo "${ME}: test_progress did not see its Running line while it was running:"
	cat "${LOG}"
	exit 1
;;
esac
case "$(cat ${LOG})" in
*"Successfully ran 4 tests :)"*) ;;
*)
	echo "${ME}: expected a successful run of 4 tests, got rc=${RC}:"
	cat "${LOG}"
	exit 1
;;
esac

exit 0
