#!/bin/sh

#  $NiH: runtest,v 1.3 2002/04/20 00:12:24 dillo Exp $
#
#  runtest -- run regression tests
#  Copyright (C) 2002 Dieter Baron
#
#  This file is part of malint, an MPEG Audio stream validator.
#  The author can be contacted at <dillo@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 EXIT-CODE OUTPUT-FILENAMES
#
# files: 
#   TESTNAME.test: test scenario
#   TESTNAME.log: expected malint output
#
# test scenario files contain the following lines:
#   title TEST DESCRIPTION
#	description of test case. [required]
#   return RET
#	where RET is the expected return code from malint. [required]
#   cat FILES
#	files to feed malint on stdin. [optional]
#   flags FLAGS
#	flags to invoke malint with. [optional]
#   input FILES
#       input files to run malint on. [optional]
# 
# environment variables:
#   VERBOSE: if set, be more verbose (e. g., output diffs)
#   NOCLEANUP: if set, don't delete output created by test run

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() {
	if [ -z "${NOCLEANUP}" ]
	then
		rm -f ${LOG};
	fi
}

checkfile() {
    if [ ! -f "$2" ]
    then
	fail "missing output file: '$2'"
    else
	sed -e '/^stdin:/n' -e 's-^\([^ ]*\):-'"${srcdir}"'/\1:-' "$1" \
		| diff - "$2" > /dev/null
	if [ $? -ne 0 ]
	then
	    if [ ! -z "${VERBOSE}" ]
	    then
		sed -e '/^stdin:/n' -e 's-^\([^ ]*\):-'"${srcdir}"'/\1:-' "$1"\
		    | diff -u - "$2"
	    fi
	    fail "$3"
	fi
    fi
}

if [ -z "${srcdir}" ]
then
    srcdir=.
fi

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

if [ -z "${MALINT}" ]
then
    MALINT=../malint
fi

# XXX: set up trap to cleanup

{

while read c a
do
    case "$c"
    in
	title) TITLE="$a";;
	return) RET="$a";;
	cat) CATFILES="$a";;
	flags) FLAGS="$a";;
	input) INPUT="$a";;
	*) die "unknown directive in test file: $c"
    esac
done

# XXX: check for missing title/return

ARGS="${FLAGS}"
for f in ${INPUT}
do
    ARGS="${ARGS} ${srcdir}/$f"
done

if [ ! -z "${CATFILES}" ]
then
    PRECMD="cat"
    for f in ${CATFILES}
    do
	PRECMD="${PRECMD} ${srcdir}/$f"
    done
fi

if [ ! -z "${VERBOSE}" ]
then
    	echo "test ${TEST}: ${TITLE}"
	echo "running: ${PRECMD}${PRECMD:+ | }${MALINT} ${ARGS}"
fi
if [ ! -z "${PRECMD}" ]
then
    ${PRECMD} | ${MALINT} ${ARGS} > ${LOG} 2>&1
else
    ${MALINT} ${ARGS} > ${LOG} 2>&1
fi
ret=$?

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

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

succeed

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