#!/bin/sh

#  $NiH: runtest,v 1.8 2002/04/23 12:05:28 dillo Exp $
#
#  runtest -- run regression tests
#  Copyright (C) 2002 Dieter Baron and Thomas Klausner
#
#  This file is part of cg, a program to assemble and decode binary Usenet
#  postings.  The authors can be contacted at <nih@giga.or.at>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# runtest TESTNAME
#
# files: 
#   TESTNAME.test: test scenario
#   TESTNAME.in*: input files
#   TESTNAME.log: expected testdec output
#
# test scenario:
#   the first line must contain `return RET' where RET is the expected \
#	exit code of testdec.
#   all other line must contain `output EXPECTED_FILE CREATED_FILE' where
#	CREATED_FILE names a file created by testdec, which will be compared
#	against EXPECTED_FILE.  CREATED_FILE may contain white space.
# 
# environment variables:
#   VERBOSE: if set, be more verbose (e. g., output diffs)
#   NOCLEANUP: if set, don't delete directory test is run in

die() {
	echo "$0: $*" >&2;
	cleanup;
	exit 2;
}

fail() {
	echo "${TEST} -- FAILED: $*";
	cleanup;
	exit 1;
}

succeed() {
	if [ ! -z "${VERBOSE}" ]
	then
		echo "${TEST} -- passed";
	fi
	cleanup;
	exit 0;
}

cleanup() {
	cd ..;
	if [ -z "${NOCLEANUP}" ]
	then
		rm -r ${DIR};
	fi
}

checkfile() {
	if [ ! -f "$2" ]
	then
		fail "missing output file: '$2'"
	else
		diff "$1" "$2" > /dev/null
		if [ $? -ne 0 ]
		then
			if [ ! -z "${VERBOSE}" ]
			then
				diff -u "$1" "$2"
			fi
			fail "$3"
		fi
		echo "$2" >> .files.need
	fi
}

check_in() {
	if [ ! "$1" = "$2" ]
	then
		die "unexpected directive in test file: got '$1', expeced '$2'"
	fi
}


TEST=`echo $1 | sed 's/\.test$//'`
shift

DIR=${TEST}.d$$
if [ -z "${srcdir}" ]
then
    srcdir=..
else
	# XXX: fix for absolute srcdir?
	srcdir=../${srcdir}
fi

if [ -z "${TESTDEC}" ]
then
    TESTDEC=../../testdec
fi

# XXX: set up trap to cleanup

mkdir ${DIR} || ( die "cannot create test directory ${DIR}" )
cd ${DIR} || ( die "cannot cd to test directory ${DIR}" )

{

read c RET
check_in $c return

if [ ! -z "${VERBOSE}" ]
then
	echo "running: ${TESTDEC} ${srcdir}/${TEST}.in*"
fi
${TESTDEC} ${srcdir}/${TEST}.in* > testdec.out 2>&1
ret=$?

if [ $ret -ne ${RET} ]
then
	fail "unexpected exit status: got $ret, expected ${RET}"
fi

ls > .files.got

checkfile ${srcdir}/${TEST}.log testdec.out "unexpected output"

while read c EXP OUT
do
    check_in $c output

    checkfile ${srcdir}/${EXP} "${OUT}" "unexpected file content: '${OUT}'"
done

sort .files.need | diff - .files.got | grep '>' > .files.extra

if [ `wc -l .files.extra | awk '{print $1}'` -ne 0 ]
then
	fail "unexpected files: `sed 's/> \(.*\)/\1,/'`"
fi

succeed

} < ${srcdir}/${TEST}.test
