-
Notifications
You must be signed in to change notification settings - Fork 0
/
stef.sh
executable file
·250 lines (217 loc) · 6.47 KB
/
stef.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash
#
# STEF - a Simple TEst Framework.
#
# (c) Jan Pechanec <[email protected]>
#
typeset -i fail=0
typeset -i untested=0
typeset -i ret
typeset diffout=stef-diff.out
typeset testnames
typeset testfiles
typeset output=stef-output-file.data
typeset tests
typeset LS=/bin/ls
typeset STEF_CONFIG=stef-config
typeset -i STEF_UNSUPPORTED=100
typeset -i STEF_UNTESTED=101
# So that test scripts may use those.
export STEF_UNSUPPORTED
export STEF_UNTESTED
function catoutput
{
[[ -s $output ]] || return
echo "--- 8< BEGIN output ---"
cat $output
echo "--- 8< END output ---"
}
# If the first argument is a directory, go in there.
if (( $# > 0 )); then
if [[ -d $1 ]]; then
cd $1
shift
fi
fi
# If you want to use variables defined in here in the tests, export them in
# there.
[[ -f $STEF_CONFIG ]] && source ./$STEF_CONFIG
[[ -z $STEF_TESTSUITE_NAME ]] && STEF_TESTSUITE_NAME="STEF Test Run"
printf "=== [ $STEF_TESTSUITE_NAME ] ===\n"
# If this variable is set, the test suite clone specific settings may put in
# that file. For example, stuff that each user will need to set differently,
# like mysh, myed, or mytar binaries.
if [[ -n $STEF_CONFIG_LOCAL && -f $STEF_CONFIG_LOCAL ]]; then
echo "Sourcing test suite specific configuration: $STEF_CONFIG_LOCAL"
source ./$STEF_CONFIG_LOCAL
fi
echo "Checking configuration sanity."
if [[ -n $STEF_UNCONFIGURE ]]; then
if [[ -n $STEF_UNCONFIGURE_NEVER && -n $STEF_UNCONFIGURE_ALWAYS ]]; then
printf "\n"
printf "STEF_UNCONFIGURE_(ALWAYS|NEVER) are mutually exclusive."
echo "\nPlease fix it before trying to re-run. Exiting."
exit 1
fi
fi
if [[ -n $STEF_EXECUTABLE_LOCAL_VARS ]]; then
echo "Checking existence of executables provided by the" \
"following variables: $STEF_EXECUTABLE_LOCAL_VARS"
for var in $STEF_EXECUTABLE_LOCAL_VARS; do
varval=$(eval echo \$$var)
[[ -x $varval && $varval == /* ]] && continue
if [[ $varval != /* ]]; then
printf "\n%s%s\n%s\n" \
"Variable '$var' set as '$varval' as part of " \
"STEF_EXECUTABLE_LOCAL_VARS" \
"defined in $STEF_CONFIG must be an absolute path."
printf "\nPlease fix it before trying to re-run. "
printf "Exiting.\n"
exit 1
fi
printf "\n%s%s\n%s\n" \
"Variable '$var' set as '$varval' as part of " \
"STEF_EXECUTABLE_LOCAL_VARS" \
"defined in '$STEF_CONFIG' does not point to an executable."
printf "\nPlease fix it before trying to re-run. Exiting.\n"
exit 1
done
fi
if [[ -n $STEF_REGFILE_LOCAL_VARS ]]; then
echo "Checking existence of regular files provided by the" \
"following variables: $STEF_REGFILE_LOCAL_VARS"
for var in $STEF_REGFILE_LOCAL_VARS; do
varval=$(eval echo \$$var)
[[ -f $varval && $varval == /* ]] && continue
if [[ $varval != /* ]]; then
printf "\n%s%s\n%s\n" \
"Variable '$var' set as '$varval' as part of " \
"STEF_REGFILE_LOCAL_VARS" \
"defined in $STEF_CONFIG must be an absolute path."
printf "\nPlease fix it before trying to re-run. "
printf "Exiting.\n"
exit 1
fi
printf "\n%s%s\n%s\n" \
"Variable '$var' set as '$varval' as part of " \
"STEF_REGFILE_LOCAL_VARS" \
"defined in $STEF_CONFIG does not point to a regular file."
printf "\nPlease fix it before trying to re-run. Exiting.\n"
exit 1
done
fi
typeset varname
for varname in STEF_CONFIGURE STEF_UNCONFIGURE; do
typeset varvalue=$(eval echo \$$varname)
[[ -z $varvalue ]] && continue
if [[ ! -x $varvalue ]]; then
echo "Error: $varname set to '$varvalue' but not executable."
echo "Exiting."
exit 1
fi
done
if [[ -n $STEF_CONFIGURE ]]; then
printf -- "\n--- [ Configuration Start ] ---\n"
$STEF_CONFIGURE
if (($? != 0)); then
echo "Configuration failed, fix it and rerun. Exiting."
exit 1
fi
printf -- "--- [ Configuration End ] ---\n"
fi
# Test must match a pattern "test-*.sh". All other scripts are ignored.
# E.g. test-001.sh, test-002.sh, test-cmd-003, etc.
if (( $# > 0 )); then
testnames=$*
# Make sure all test names represent valid test scripts.
for i in $names; do
[[ -x test-$i.sh ]] || \
{ echo "$i not a valid test. Exiting." && exit 1; }
done
else
testfiles=$( $LS test-*.sh )
if (( $? != 0 )); then
echo "No valid tests present. Exiting."
exit 1
fi
testnames=$( echo "$testfiles" | cut -f2- -d- | cut -f1 -d. )
fi
printf -- "\n--- [ Running tests ] ---\n"
for i in $testnames; do
# Print the test number.
printf " $i\t"
./test-$i.sh >$output 2>&1
ret=$?
# Go through some selected exit codes that has special meaning to STEF.
if ((ret == STEF_UNSUPPORTED)); then
echo "UNSUPPORTED"
catoutput
rm -f $output
continue;
elif ((ret == STEF_UNTESTED)); then
echo "UNTESTED"
# An untested test is a red flag as we failed even before
# testing what we were supposed to.
((++untested))
catoutput
rm -f $output
continue;
fi
# Anything else aside from 0 is a test fail.
if ((ret != 0)); then
echo "FAIL (return code $ret)"
((++fail))
if [[ -s $output ]]; then
echo "--- 8< BEGIN output ---"
cat $output
echo "--- 8< END output ---"
fi
rm $output
continue
fi
# If the expected output file does not exist, we consider the test
# successful and are done.
if [[ ! -f test-output-$i.txt ]]; then
echo "PASS"
rm -f $output
continue
fi
# As both stdout and stderr output goes to the same file, the unit test
# output file must contains both expected stdout and stderr.
diff -u test-output-$i.txt $output > $diffout
if (($? != 0)); then
echo "FAIL"
((++fail))
echo "--- 8< BEGIN diff output ---"
cat $diffout
echo "--- 8< END diff output ---"
else
echo "PASS"
fi
rm -f $output $diffout
done
printf -- "--- [ Tests finished ] ---\n"
typeset -i stefret=0
((fail > 0)) && stefret=1
((untested > 0)) && stefret=1
if [[ -n $STEF_UNCONFIGURE ]]; then
printf -- "\n--- [ Unconfiguration Start ] ---\n"
if [[ -n $STEF_UNCONFIGURE_NEVER ]]; then
echo "Skipping unconfiguration (STEF_UNCONFIGURE_NEVER)."
elif [[ $stefret -ne 0 && -z $STEF_UNCONFIGURE_ALWAYS ]]; then
echo "Skipping unconfiguration due to some test failures."
else
((stefret != 0)) &&
echo "Forcing unconfiguration (STEF_UNCONFIGURE_ALWAYS)."
$STEF_UNCONFIGURE
if (($? != 0)); then
echo "WARNING: Unconfiguration failed."
fi
fi
printf -- "--- [ Unconfiguration End ] ---\n"
fi
printf "\n=== [ $STEF_TESTSUITE_NAME Results ] ===\n"
((fail > 0)) && echo "WARNING: $fail test(s) FAILED !!!"
((untested > 0)) && echo "WARNING: $untested test(s) UNTESTED !!!"
((fail == 0 && untested == 0)) && echo "All tests passed."
exit $stefret