#!/bin/sh

set -e

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

# With PKGOS_RUN_TESTS_FIXTURE set, test_failing fails: the runner
# must exit non-zero, report the failure, and print the output of the
# failing test, so that failures are correctly reported even though
# tests run as background jobs.
PKGOS_RUN_TESTS_FIXTURE=yes
export PKGOS_RUN_TESTS_FIXTURE

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

if [ "${RC}" = "0" ] ; then
	echo "${ME}: runner must exit non-zero when a test fails"
	exit 1
fi
case "${OUT}" in
*"test_failing ... FAILED"*) ;;
*)
	echo "${ME}: the failing test is not reported as FAILED:"
	echo "${OUT}"
	exit 1
;;
esac
case "${OUT}" in
*"This failure is expected: PKGOS_RUN_TESTS_FIXTURE is set."*) ;;
*)
	echo "${ME}: the output of the failing test is not printed:"
	echo "${OUT}"
	exit 1
;;
esac
case "${OUT}" in
*"Failure while running unit tests :("*) ;;
*)
	echo "${ME}: missing failure summary:"
	echo "${OUT}"
	exit 1
;;
esac
case "${OUT}" in
*"Successfully ran"*)
	echo "${ME}: the success summary must not be printed on failure"
	exit 1
;;
esac

exit 0
