/ scripts / runtests
runtests
  1  #!/bin/bash
  2  
  3  # When running with "-v", the test itself runs in a pipeline with tee, and
  4  # without pipefail we get the exit value from tee instead of from the test.
  5  set -o pipefail
  6  
  7  # The linuxcnc starter script sometimes tries to display X windows if
  8  # DISPLAY is set.  We never want that while running tests, so unset it.
  9  unset DISPLAY
 10  
 11  # Some of our tests emit locale-sensitive strings, so reset the locale
 12  # to a sane default.
 13  export LC_ALL=C
 14  
 15  
 16  case "$0" in
 17  	*/*) MYDIR="${0%/*}" ;;
 18  	*) MYDIR="`type -path $0`"; MYDIR="${MYDIR%/*}"
 19  esac
 20  MYDIR=$(cd $MYDIR; pwd);
 21  TOPDIR=$(cd $MYDIR/..; pwd)
 22  
 23  . $TOPDIR/scripts/rip-environment >/dev/null
 24  
 25  NUM=0
 26  FAIL=0; FAIL_NAMES=""
 27  XFAIL=0
 28  VERBOSE=0
 29  
 30  clean () {
 31      find $* \( -name "stderr" -or -name "result" \
 32  	-or -name "*.var" -or -name "*.var.bak" \) \
 33  	-print0 | xargs -0 rm -f
 34  }
 35  
 36  run_shell_script () {
 37      testname=$(basename $1)
 38      testdir=$(dirname $1)
 39  
 40      pushd $testdir > /dev/null
 41      if [ $VERBOSE -eq 1 ]; then
 42          (bash -x $testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
 43      else
 44          bash -x $testname > result 2> stderr
 45      fi
 46      exitcode=$?
 47      popd > /dev/null
 48      return $exitcode
 49  }
 50  
 51  run_executable () {
 52      testname=$(basename $1)
 53      testdir=$(dirname $1)
 54  
 55      pushd $testdir > /dev/null
 56      if [ $VERBOSE -eq 1 ]; then
 57          (./$testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
 58      else
 59          ./$testname > result 2> stderr
 60      fi
 61      exitcode=$?
 62      popd > /dev/null
 63      return $exitcode
 64  }
 65  
 66  run_without_overruns () {
 67      testname=$(basename $1)
 68      testdir=$(dirname $1)
 69      for i in $(seq 10); do
 70          if [ $i != 1 ]; then echo "--- $testdir: overrun detected in sampler, re-running test" 1>&2 ; fi
 71  
 72          pushd $testdir > /dev/null
 73          if [ $VERBOSE -eq 1 ]; then
 74              (halrun -f $testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
 75          else
 76              halrun -f $testname > result 2> stderr
 77          fi
 78          exitcode=$?
 79          popd > /dev/null
 80  
 81          if ! grep -q '^overrun$' $testdir/result; then return $exitcode; fi
 82      done
 83      echo "--- $testdir: $i overruns detected, giving up" 1>&2
 84      return 1
 85  }
 86  
 87  TMPDIR=`mktemp -d /tmp/runtest.XXXXXX`
 88  trap "rm -rf $TMPDIR" 0 1 2 3 9 15
 89  
 90  
 91  run_tests () {
 92      find $* -name test.hal -or -name test.sh -or -name test \
 93  	| sort > $TMPDIR/alltests
 94  
 95      while read testname; do
 96  	testdir=$(dirname $testname)
 97  	if [ -e $testdir/skip ]; then
 98  	    if ! [ -x $testdir/skip ] || ! $testdir/skip; then
 99  		echo "Skipping test: $testdir" 1>&2
100  		continue
101  	    fi
102  	fi
103  	NUM=$(($NUM+1))
104  	echo "Running test: $testdir" 1>&2
105          case $testname in
106          *.hal) run_without_overruns $testname ;;
107          *.sh) run_shell_script $testname ;;
108          *) run_executable $testname ;;
109          esac
110  	exitcode=$?
111  	if [ $exitcode -ne 0 ]; then
112  	    reason="test run exited with $exitcode"
113  	else
114  	    if [ -e $testdir/checkresult ]; then
115  		$testdir/checkresult $testdir/result
116  		exitcode=$?
117  		reason="checkresult exited with $exitcode"
118  	    elif [ -f $testdir/expected ]; then
119  		cmp -s $testdir/expected $testdir/result
120  		exitcode=$?
121  		reason="result differed from expected"
122  		if [ $exitcode -ne 0 ]; then
123  		    diff -u $testdir/expected $testdir/result > $TMPDIR/diff
124  		    SIZE=$(wc -l < $TMPDIR/diff)
125  		    if [ $SIZE -lt 40 ]; then
126  			cat $TMPDIR/diff
127  		    else
128  			OMIT=$((SIZE-40))
129  			head -40 $TMPDIR/diff
130  			echo "($OMIT more lines omitted)"
131  		    fi
132  		fi
133  	    else
134  		exitcode=1
135  		reason="Neither expected nor checkresult existed"
136  	    fi
137  	fi
138  	if [ $exitcode -ne 0 ]; then
139  	    if [ -f $testdir/xfail ]; then
140  		XFAIL=$(($XFAIL+1))
141  		echo "*** $testdir: XFAIL: $reason"
142  		if [ $NOCLEAN -eq 0 ]; then
143  		    rm -f $testdir/stderr $testdir/result \
144  			$testdir/*.var $testdir/*.var.bak
145  		fi
146  	    else
147  		FAIL=$(($FAIL+1))
148  		FAIL_NAMES="$FAIL_NAMES
149  	$testdir"
150  		echo "*** $testdir: FAIL: $reason"
151  	    fi
152  	else
153  	    if [ -f $testdir/xfail ]; then
154  		echo "*** $testdir: XPASS: Passed, but was expected to fail"
155  	    else
156  		if [ $NOCLEAN -eq 0 ]; then
157  		    rm -f $testdir/stderr $testdir/result \
158  			$testdir/*.var $testdir/*.var.bak
159  		fi
160  	    fi
161  	fi
162      done < $TMPDIR/alltests
163  
164      SUCC=$((NUM-FAIL-XFAIL))
165      echo "Runtest: $NUM tests run, $SUCC successful, $FAIL failed + $XFAIL expected"
166      if [ $FAIL -ne 0 ]; then
167  	echo "Failed: $FAIL_NAMES"
168  	exit 1;
169      else
170  	exit 0;
171      fi
172  }
173  
174  usage () {
175      P=${0##*/}
176      cat <<EOF
177  $P: Run HAL test suite items
178  
179  Usage:
180      $P [-n] tests
181  	Run tests.  With '-n', do not remove temporary files for successful
182  	tests.
183  
184      $P -c tests
185  	Remove temporary files from an earlier test run.
186  
187      $P -v
188          Show stdout and stderr (normally it's hidden).
189  EOF
190  }
191  
192  CLEAN_ONLY=0
193  NOCLEAN=0
194  while getopts cnvh opt; do
195      case "$opt" in
196      c) CLEAN_ONLY=1 ;;
197      n) NOCLEAN=1 ;;
198      v) VERBOSE=1 ;;
199      h|?) usage; exit 0 ;;
200      *) usage; exit 1 ;;
201      esac
202  done
203  shift $((OPTIND-1))
204  
205  if [ $# -eq 0 ]; then
206      if [ -f test.hal -o -f test.sh ]; then
207          set -- .
208      else
209          set -- $TOPDIR/tests
210      fi
211  fi
212  
213  if [ $CLEAN_ONLY -eq 1 ]; then
214      clean "$@"
215  else
216      run_tests "$@"
217  fi