Path: blob/master/tester/run-tests.sh
909 views
#! /usr/bin/env bash12GREEN='\033[0;32m'3RED='\033[0;31m'4NONE='\033[0m'56# run_test testdir testnumber7run_test () {8local testdir=$19local testnum=$210local verbose=$31112# pre: execute this after before the test is done, to set up13local prefile=$testdir/$testnum.pre14if [[ -f $prefile ]]; then15eval $(cat $prefile)16if (( $verbose == 1 )); then17echo -n "pre-test: "18cat $prefile19fi20fi21local testfile=$testdir/$testnum.run22if (( $verbose == 1 )); then23echo -n "test: "24cat $testfile25fi26eval $(cat $testfile) > tests-out/$testnum.out 2> tests-out/$testnum.err27echo $? > tests-out/$testnum.rc2829# post: execute this after the test is done, to clean up30local postfile=$testdir/$testnum.post31if [[ -f $postfile ]]; then32eval $(cat $postfile)33if (( $verbose == 1 )); then34echo -n "post-test: "35cat $postfile36fi37fi38return39}4041print_error_message () {42local testnum=$143local contrunning=$244local filetype=$345builtin echo -e "test $testnum: ${RED}$testnum.$filetype incorrect${NONE}"46echo " what results should be found in file: $testdir/$testnum.$filetype"47echo " what results produced by your program: tests-out/$testnum.$filetype"48echo " compare the two using diff, cmp, or related tools to debug, e.g.:"49echo " prompt> diff $testdir/$testnum.$filetype tests-out/$testnum.$filetype"50echo " See tests/$testnum.run for what is being run"51if (( $contrunning == 0 )); then52exit 153fi54}5556# check_test testdir testnumber contrunning out/err57check_test () {58local testdir=$159local testnum=$260local contrunning=$361local filetype=$46263# option to use cmp instead?64returnval=$(diff $testdir/$testnum.$filetype tests-out/$testnum.$filetype)65if (( $? == 0 )); then66echo 067else68echo 169fi70}7172# run_and_check testdir testnumber contrunning verbose printerror73# testnumber: the test to run and check74# printerrer: if 1, print an error if test does not exist75run_and_check () {76local testdir=$177local testnum=$278local contrunning=$379local verbose=$480local failmode=$58182if [[ ! -f $testdir/$testnum.run ]]; then83if (( $failmode == 1 )); then84echo "test $testnum does not exist" >&2; exit 185fi86exit 087fi88if (( $verbose == 1 )); then89echo -n -e "running test $testnum: "90cat $testdir/$testnum.desc91fi92run_test $testdir $testnum $verbose93rccheck=$(check_test $testdir $testnum $contrunning rc)94outcheck=$(check_test $testdir $testnum $contrunning out)95errcheck=$(check_test $testdir $testnum $contrunning err)96othercheck=097if [[ -f $testdir/$testnum.other ]]; then98othercheck=$(check_test $testdir $testnum $contrunning other)99fi100# echo "results: outcheck:$outcheck errcheck:$errcheck"101if (( $rccheck == 0 )) && (( $outcheck == 0 )) && (( $errcheck == 0 )) && (( $othercheck == 0 )); then102echo -e "test $testnum: ${GREEN}passed${NONE}"103if (( $verbose == 1 )); then104echo ""105fi106else107if (( $rccheck == 1 )); then108print_error_message $testnum $contrunning rc109fi110if (( $outcheck == 1 )); then111print_error_message $testnum $contrunning out112fi113if (( $errcheck == 1 )); then114print_error_message $testnum $contrunning err115fi116if (( $othercheck == 1 )); then117print_error_message $testnum $contrunning other118fi119fi120}121122# usage: call when args not parsed, or when help needed123usage () {124echo "usage: run-tests.sh [-h] [-v] [-t test] [-c] [-s] [-d testdir]"125echo " -h help message"126echo " -v verbose"127echo " -t n run only test n"128echo " -c continue even after failure"129echo " -s skip pre-test initialization"130echo " -d testdir run tests from testdir"131return 0132}133134#135# main program136#137verbose=0138testdir="tests"139contrunning=0140skippre=0141specific=""142143args=`getopt hvsct:d: $*`144if [[ $? != 0 ]]; then145usage; exit 1146fi147148set -- $args149for i; do150case "$i" in151-h)152usage153exit 0154shift;;155-v)156verbose=1157shift;;158-c)159contrunning=1160shift;;161-s)162skippre=1163shift;;164-t)165specific=$2166shift167number='^[0-9]+$'168if ! [[ $specific =~ $number ]]; then169usage170echo "-t must be followed by a number" >&2; exit 1171fi172shift;;173-d)174testdir=$2175shift176shift;;177--)178shift; break;;179esac180done181182# need a test directory; must be named "tests-out"183if [[ ! -d tests-out ]]; then184mkdir tests-out185fi186187# do a one-time setup step188if (( $skippre == 0 )); then189if [[ -f tests/pre ]]; then190echo -e "doing one-time pre-test (use -s to suppress)"191source tests/pre192if (( $? != 0 )); then193echo "pre-test: failed"194exit 1195fi196echo ""197fi198fi199200# run just one test201if [[ $specific != "" ]]; then202run_and_check $testdir $specific $contrunning $verbose 1203exit 0204fi205206# run all tests207(( testnum = 1 ))208while true; do209run_and_check $testdir $testnum $contrunning $verbose 0210(( testnum = $testnum + 1 ))211done212213exit 0214215216