#!/bin/sh
# Run the upstream test suite against the given git-assembler binary.
#
# The upstream tests expect to find git-assembler next to the t/
# directory, so assemble that layout in a temporary directory with the
# binary under test symlinked in. Each test is run directly rather than
# through t/run.sh, which only considers scripts with mode 755.
set -e

GAS="$1"
if [ -z "$GAS" ]; then
	echo "usage: $0 /path/to/git-assembler" >&2
	exit 2
fi

SRCDIR="$(realpath "$(dirname "$0")/../..")"
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT

cp -r "$SRCDIR/t" "$WORKDIR/t"
ln -s "$(realpath "$GAS")" "$WORKDIR/git-assembler"

# Isolate the tests from any user or system git configuration and
# provide an identity for the commits they create.
mkdir "$WORKDIR/home"
export HOME="$WORKDIR/home"
export GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME="Debian Test" GIT_AUTHOR_EMAIL="test@example.org"
export GIT_COMMITTER_NAME="Debian Test" GIT_COMMITTER_EMAIL="test@example.org"
unset VISUAL EDITOR GIT_DIR GIT_WORK_TREE

failed=0
for t in "$WORKDIR"/t/t*-*.sh; do
	name="$(basename "$t" .sh)"
	rc=0
	sh "$t" >/dev/null 2>&1 || rc=$?
	case "$rc" in
	0)  echo "$name: ok" ;;
	96) echo "$name: skip" ;;
	*)  echo "$name: FAIL ($rc)"
	    sh "$t" -v || true
	    failed=1 ;;
	esac
done

exit "$failed"
