xref: /titanic_44/usr/src/lib/libshell/common/tests/shtests (revision 8cdd6a74847b5ae6ed26727528bdf6b139cf7552)
1# This program runs ksh regression tests
2# shtests [ name=value ... ] [ --all | --compile ] [ --time ] [ a.sh b.sh ... ]
3
4timesensitive='*@(options|sigchld|subshell).sh'
5
6unset DISPLAY ENV FIGNORE HISTFILE
7LANG=C
8LC_ALL=C
9compile=1
10script=1
11time=1
12vmdebug=a
13while	:
14do	case $1 in
15	-a|--a*)compile=2
16		script=2
17		;;
18	-c|--c*)compile=2
19		script=
20		;;
21	-s|--s*)compile=
22		script=2
23		;;
24	-t|--not*)time=
25		;;
26	-v|--nov*)vmdebug=
27		;;
28	-*)	echo $0: $1: invalid option >&2
29		exit 2
30		;;
31	*=*)	n=${1%%=*}
32		v=${1#*=}
33		eval $n=\'$v\'
34		export $n
35		;;
36	*)	break
37		;;
38	esac
39	shift
40done
41if	[[ ! $vmdebug ]]
42then	unset VMDEBUG
43elif	[[ $VMDEBUG ]]
44then	vmdebug=$VMDEBUG
45else	export VMDEBUG=$vmdebug
46fi
47[[ $VMDEBUG ]] || timesensitive=.
48export LANG LC_ALL PATH PWD SHELL
49PWD=`pwd`
50SHELL=${SHELL-ksh}
51case $0 in
52/*)	d=`dirname $0`;;
53*/*)	d=$PWD/`dirname $0`;;
54*)	d=$PWD;;
55esac
56case $SHELL in
57/*)	;;
58*/*)	SHELL=$d/$SHELL;;
59*)	SHELL=$(whence $SHELL);;
60esac
61PATH=/bin:/usr/bin
62if	[[ -d /usr/ucb ]]
63then	PATH=$PATH:/usr/ucb
64fi
65PATH=$PATH:$d
66if	[[ $INSTALLROOT && -r $INSTALLROOT/bin/.paths ]]
67then	PATH=$INSTALLROOT/bin:$PATH
68fi
69if	[[ $compile ]]
70then	SHCOMP=${SHCOMP:-shcomp}
71	if	whence $SHCOMP > /dev/null
72	then	tmp=$(mktemp -dt) || { echo mktemp -dt failed >&2; exit 1; }
73		trap "cd /; rm -rf $tmp" EXIT
74	elif	[[ $compile != 1 ]]
75	then	echo $0: --compile: $SHCOMP not found >&2
76		exit 1
77	else	compile=
78	fi
79fi
80typeset -A tests
81for i in ${*-*.sh}
82do	t=$(grep -c err_exit $i)
83	if	(( $t > 2 ))
84	then	(( t = $t - 2 ))
85	fi
86	tests[$i]=$t
87	T=test
88	if	(( $t != 1 ))
89	then	T=${T}s
90	fi
91	E=error
92	if	[[ $script ]]
93	then	echo test $i begins ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"}
94		if	[[ $i == $timesensitive ]]
95		then	unset VMDEBUG
96		fi
97		if	$SHELL $i
98		then	echo test $i passed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} "[ $t $T 0 ${E}s ]"
99		else	e=$?
100			if	(( e > 256 ))
101			then	e=1
102				E=signal
103			fi
104			if	(( $e != 1 ))
105			then	E=${E}s
106			fi
107			echo test $i failed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T $e $E ]"
108		fi
109		if	[[ $i == $timesensitive ]]
110		then	export VMDEBUG=$vmdebug
111		fi
112	fi
113done
114if	[[ $compile ]]
115then	for i in ${*-*.sh}
116	do	t=${tests[$i]}
117		T=test
118		if	(( $t != 1 ))
119		then	T=${T}s
120		fi
121		o=${i##*/}
122		o=shcomp-${o%.sh}.ksh
123		echo test $o begins ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"}
124		E=error
125		if	$SHCOMP $i > $tmp/$o
126		then	if	[[ $i == $timesensitive ]]
127			then	unset VMDEBUG
128			fi
129			if	$SHELL $tmp/$o
130			then	echo test $o passed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} "[ $t $T 0 ${E}s ]"
131			else	e=$?
132				if	(( e > 256 ))
133				then	e=1
134					E=signal
135				fi
136				if	(( $e != 1 ))
137				then	E=${E}s
138				fi
139				echo test $o failed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T $e $E ]"
140			fi
141			if	[[ $i == $timesensitive ]]
142			then	export VMDEBUG=$vmdebug
143			fi
144		else	e=$?
145			t=1
146			T=test
147			echo test $o failed to compile ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T 1 $E ]"
148		fi
149	done
150fi
151