#!/bin/sh

set -e

# Test that with the legacy testrepository (.testr.conf), which
# cannot read the selection files by itself, pkgos-dh_auto_test
# builds one combined regex out of the command line regexes and the
# debian/tests/{include,exclude}-list.txt entries:
# - include and exclusion lists: (INCLUSIONS)(?!.*(?:EXCLUSIONS))
# - exclusion list only, without any inclusion: ^(?!.*(?:EXCLUSIONS))
# - inclusion list only: the command line regexes plus one extra
#   regex holding the include-list entries (testrepository ORs the
#   positional regexes).

ME=$(basename $0)
WORK=$(mktemp -d)
trap 'rm -rf "${WORK}"' EXIT

# Mock py3versions with a python4 version, so that the check for
# /usr/bin/testr-python4 fails, and the script falls back to the
# testr from the PATH, which is mocked below.
mkdir -p "${WORK}/bin"
printf '#!/bin/sh\necho 4.2\n' >"${WORK}/bin/py3versions"
printf '#!/bin/sh\ncat >/dev/null\n' >"${WORK}/bin/subunit2pyunit"
printf '#!/bin/sh\ncat >/dev/null\n' >"${WORK}/bin/subunit-filter"
printf '#!/bin/sh\ncat >/dev/null\n' >"${WORK}/bin/subunit-stats"
printf '#!/bin/sh\necho "$@" >>"${TESTR_MOCK_LOG}"\n' >"${WORK}/bin/testr"
chmod +x "${WORK}/bin/py3versions" "${WORK}/bin/subunit2pyunit" "${WORK}/bin/subunit-filter" "${WORK}/bin/subunit-stats" "${WORK}/bin/testr"

PATH="${WORK}/bin:${PATH}"
export PATH

SCRIPT=$(cd "$(dirname "$0")/../../../build-tools" && pwd)/pkgos-dh_auto_test

# Run the script in a safe temp dir holding a .testr.conf, so that it
# takes the testrepository branch.
cd "${WORK}"
printf '[DEFAULT]\ntest_command = foo\n' >.testr.conf
mkdir -p debian/tests

check_run_line () {
	RUN_LINE=$(grep '^run' "${TESTR_MOCK_LOG}" || true)
	if [ -z "${RUN_LINE}" ] ; then
		echo "${ME}: the testr mock never saw a run command"
		exit 1
	fi
	case "${RUN_LINE}" in
	*"${1}"*) ;;
	*)
		echo "${ME}: testr wasn't called with '${1}': ${RUN_LINE}"
		exit 1
		;;
	esac
}

# 1: command line regex + include-list + exclude-list.
printf 'incl_regex\n' >debian/tests/include-list.txt
printf 'excl_one\\.regex\nexcl_two\\.regex\n' >debian/tests/exclude-list.txt
TESTR_MOCK_LOG="${WORK}/testr1.log"
export TESTR_MOCK_LOG
"${SCRIPT}" 'nova_prefix' >out.log 2>&1 || {
	cat out.log
	echo "${ME}: pkgos-dh_auto_test failed"
	exit 1
}
check_run_line '(nova_prefix|incl_regex)(?!.*(?:excl_one\.regex|excl_two\.regex))'

# 2: exclude-list only, without any inclusion.
rm debian/tests/include-list.txt
printf 'excl_regex\n' >debian/tests/exclude-list.txt
TESTR_MOCK_LOG="${WORK}/testr2.log"
"${SCRIPT}" >out2.log 2>&1 || {
	cat out2.log
	echo "${ME}: pkgos-dh_auto_test failed with an exclusion list only"
	exit 1
}
check_run_line '^(?!.*(?:excl_regex))'

# 3: include-list only.
rm debian/tests/exclude-list.txt
printf 'incl_regex\n' >debian/tests/include-list.txt
TESTR_MOCK_LOG="${WORK}/testr3.log"
"${SCRIPT}" 'nova_prefix' >out3.log 2>&1 || {
	cat out3.log
	echo "${ME}: pkgos-dh_auto_test failed with an inclusion list only"
	exit 1
}
check_run_line '--subunit nova_prefix (incl_regex)'

exit 0
