summaryrefslogtreecommitdiff
path: root/usr/src/lib/libshell/common/tests/shtests
blob: 645d290574a43ec25a64f14abbf03ee7f191f914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# This program runs ksh regression tests
# shtests [ name=value ... ] [ --all | --compile ] [ --time ] [ a.sh b.sh ... ]

timesensitive='*@(options|sigchld|subshell).sh'

unset DISPLAY ENV FIGNORE HISTFILE
LANG=C
LC_ALL=C
compile=1
script=1
time=1
vmalloc_options=abort
while	:
do	case $1 in
	-a|--a*)compile=2
		script=2
		;;
	-c|--c*)compile=2
		script=
		;;
	-s|--s*)compile=
		script=2
		;;
	-t|--not*)time=
		;;
	-v|--nov*)vmalloc_options=
		;;
	-*)	echo $0: $1: invalid option >&2
		exit 2
		;;
	*=*)	n=${1%%=*}
		v=${1#*=}
		eval $n=\'$v\'
		export $n
		;;
	*)	break
		;;
	esac
	shift
done
if	[[ $VMALLOC_OPTIONS ]]
then	vmalloc_options=$VMALLOC_OPTIONS
else	VMALLOC_OPTIONS=$vmalloc_options
fi
[[ $VMALLOC_OPTIONS ]] || timesensitive=.
export LANG LC_ALL PATH PWD SHELL VMALLOC_OPTIONS
PWD=`pwd`
SHELL=${SHELL-ksh}
case $0 in
/*)	d=`dirname $0`;;
*/*)	d=$PWD/`dirname $0`;;
*)	d=$PWD;;
esac
case $SHELL in
/*)	;;
*/*)	SHELL=$d/$SHELL;;
*)	SHELL=$(whence $SHELL);;
esac
PATH=/bin:/usr/bin
if	[[ -d /usr/ucb ]]
then	PATH=$PATH:/usr/ucb
fi
PATH=$PATH:$d
if	[[ $INSTALLROOT && -r $INSTALLROOT/bin/.paths ]]
then	PATH=$INSTALLROOT/bin:$PATH
fi
if	[[ $compile ]]
then	SHCOMP=${SHCOMP:-shcomp}
	if	whence $SHCOMP > /dev/null
	then	tmp=$(mktemp -dt) || { echo mktemp -dt failed >&2; exit 1; }
		trap "cd /; rm -rf $tmp" EXIT
	elif	[[ $compile != 1 ]]
	then	echo $0: --compile: $SHCOMP not found >&2
		exit 1
	else	compile=
	fi
fi
typeset -A tests
for i in ${*-*.sh}
do	if	[[ ! -r $i ]]
	then	echo $0: $i: not found >&2
		continue
	fi
	t=$(grep -c err_exit $i)
	if	(( $t > 2 ))
	then	(( t = $t - 2 ))
	fi
	tests[$i]=$t
	T=test
	if	(( $t != 1 ))
	then	T=${T}s
	fi
	E=error
	if	[[ $script ]]
	then	echo test $i begins ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"}
		if	[[ $i == $timesensitive ]]
		then	VMALLOC_OPTIONS=
		fi
		if	$SHELL $i
		then	echo test $i passed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} "[ $t $T 0 ${E}s ]"
		else	e=$?
			if	(( e > 256 ))
			then	e=1
				E=signal
			fi
			if	(( $e != 1 ))
			then	E=${E}s
			fi
			echo test $i failed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T $e $E ]"
		fi
		if	[[ $i == $timesensitive ]]
		then	VMALLOC_OPTIONS=$vmalloc_options
		fi
	fi
done
if	[[ $compile ]]
then	for i in ${*-*.sh}
	do	if	[[ ! -r $i ]]
		then	continue
		fi
		t=${tests[$i]}
		T=test
		if	(( $t != 1 ))
		then	T=${T}s
		fi
		o=${i##*/}
		o=shcomp-${o%.sh}.ksh
		echo test $o begins ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"}
		E=error
		if	$SHCOMP $i > $tmp/$o
		then	if	[[ $i == $timesensitive ]]
			then	VMALLOC_OPTIONS=
			fi
			if	$SHELL $tmp/$o
			then	echo test $o passed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} "[ $t $T 0 ${E}s ]"
			else	e=$?
				if	(( e > 256 ))
				then	e=1
					E=signal
				fi
				if	(( $e != 1 ))
				then	E=${E}s
				fi
				echo test $o failed ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T $e $E ]"
			fi
			if	[[ $i == $timesensitive ]]
			then	VMALLOC_OPTIONS=$vmalloc_options
			fi
		else	e=$?
			t=1
			T=test
			echo test $o failed to compile ${time:+"at $(date +%Y-%m-%d+%H:%M:%S)"} with exit code $e "[ $t $T 1 $E ]"
		fi
	done
fi