1#! /bin/sh
2
3# Copyright (C) 2000-2002, 2004, 2005, 2011, 2012, 2016, 2020  Niels M��ller
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19failed=0
20all=0
21
22debug='no'
23testflags=''
24
25if [ -z "$srcdir" ] ; then
26  srcdir=`pwd`
27fi
28
29export srcdir
30
31if [ -n "$TEST_SHLIB_DIR" ] ; then
32  # Prepend to LD_LIBRARY_PATH, if it is alredy set.
33  LD_LIBRARY_PATH="${TEST_SHLIB_DIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
34  # For MACOS
35  DYLD_LIBRARY_PATH="$TEST_SHLIB_DIR"
36  # For Windows
37  PATH="${TEST_SHLIB_DIR}:${PATH}"
38  # For Wine
39  WINEPATH="${TEST_SHLIB_DIR}${WINEPATH:+;$WINEPATH}"
40
41  export LD_LIBRARY_PATH
42  export DYLD_LIBRARY_PATH
43  export PATH
44  export WINEPATH
45fi
46
47# When used in make rules, we sometimes get the filenames VPATH
48# expanded, but usually not.
49find_program () {
50    case "$1" in
51	*/*)
52	  echo "$1"
53	  ;;
54	*)
55	  if [ -x "$1" ] ; then
56	      echo "./$1"
57	  elif [ -x "$1.exe" ] ; then
58	      echo "./$1.exe"
59	  else
60	      echo "$srcdir/$1"
61	  fi
62	  ;;
63    esac
64}
65
66env_program () {
67  if [ -x "$1" ] ; then
68    if "$1"; then : ; else
69      echo FAIL: $1
70      exit 1
71    fi
72  fi
73}
74
75test_program () {
76  testname=`basename "$1" .exe`
77  testname=`basename "$testname" -test`
78  if [ -z "$EMULATOR" ] || head -1 "$1" | grep '^#!' > /dev/null; then
79    "$1" $testflags
80  else
81    $EMULATOR "$1" $testflags
82  fi
83  case "$?" in
84      0)
85	echo PASS: $testname
86	all=`expr $all + 1`
87	;;
88      77)
89	echo SKIP: $testname
90      ;;
91      *)
92	echo FAIL: $testname
93	failed=`expr $failed + 1`
94	all=`expr $all + 1`
95	;;
96  esac
97}
98
99env_program `find_program setup-env`
100
101while test $# != 0
102do
103  case "$1" in
104  --debug)
105    debug=yes
106    ;;
107  -v)
108    testflags='-v'
109    ;;
110  -*)
111    echo >&2 'Unknown option `'"$1'"
112    exit 1
113    ;;
114  *)
115    break
116    ;;
117  esac
118  shift
119done
120
121# Comment out special handling for zero arguments to support separate
122# tests-build/tests-run.
123#if [ $# -eq 0 ] ; then
124#  for f in *-test; do test_program "./$f"; done
125#else
126  for f in "$@" ; do test_program `find_program "$f"`; done
127#fi
128
129if [ $failed -eq 0 ] ; then
130  banner="All $all tests passed"
131else
132  banner="$failed of $all tests failed"
133fi
134dashes=`echo "$banner" | sed s/./=/g`
135echo "$dashes"
136echo "$banner"
137echo "$dashes"
138
139if [ "x$debug" = xno ] ; then
140  env_program `find_program teardown-env`
141fi
142
143[ "$failed" -eq 0 ]
144