#!/bin/sh

set -e

# Test that --dynamic is refused with a hard error when the package
# uses the legacy testrepository (.testr.conf), and that testr is
# never called in that case.

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\necho "$@" >>"${TESTR_MOCK_LOG}"\n' >"${WORK}/bin/testr"
chmod +x "${WORK}/bin/py3versions" "${WORK}/bin/subunit2pyunit" "${WORK}/bin/testr"

PATH="${WORK}/bin:${PATH}"
export PATH
TESTR_MOCK_LOG="${WORK}/testr.log"
export TESTR_MOCK_LOG

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

if "${SCRIPT}" --dynamic >out.log 2>&1 ; then
	echo "${ME}: pkgos-dh_auto_test should exit 1 with --dynamic on a .testr.conf"
	exit 1
fi
if [ -s "${TESTR_MOCK_LOG}" ] ; then
	echo "${ME}: testr was called even though --dynamic isn't supported"
	exit 1
fi
if ! grep -q "testrepository doesn't support --dynamic" out.log ; then
	echo "${ME}: the error message wasn't displayed"
	exit 1
fi

exit 0
