/
run_wml_tests
executable file
·221 lines (206 loc) · 5.41 KB
/
run_wml_tests
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
#!/bin/bash
#This script runs a sequence of unit tests, found in the file wml_test_schedule
#Format: <expected return code> <unit test scenario name>
#
#The script will ignore comments, beginning with #
#
#The script has options for "verbose" mode -v, and "debug" mode -d.
#
#The -u option uses "unix timeouts", through the timeout command, instead of
#wesnoth's built-in timeout mechanism. The built-in one is somewhat unreliable
#at time of writing, so if you are on unix then you may prefer that.
get_code_string()
{
case ${1} in
0)
CodeString="PASS"
;;
1)
CodeString="FAIL"
;;
2)
CodeString="FAIL (TIMEOUT)"
;;
3)
CodeString="FAIL (INVALID REPLAY)"
;;
4)
CodeString="FAIL (ERRORED REPLAY)"
;;
*)
CodeString="? ? ?"
;;
esac
}
check_errs()
{
# Argument 1 is the name of the test.
# Argument 2 is the wesnoth error code for the test.
# Argument 3 is the expected error code.
if [ "${2}" -eq 134 -a "${3}" -eq 2 -a $UnixTimeout -eq 0 ]; then
if [ $Verbose -ge 2 ]; then
echo "Caught \'terminate called without an active exception\' return code 134"
echo "This means wesnoth tried to kill the thread but SDL threw an error..."
echo "(This happens occasionally when running the empty_test with a timeout.)"
echo "Since we expected timeout, the test passes."
fi
return 0
elif [ "${2}" -eq 124 -a "${3}" -eq 2 -a $UnixTimeout -eq 1 ]; then
if [ $Verbose -ge 2 ]; then
echo "Caught return code 124 from timeout"
echo "This signal means that the unix timeout utility killed wesnoth with TERM."
echo "Since we expected timeout, the test passes."
fi
return 0
elif [ "${2}" -eq 137 -a "${3}" -eq 2 -a $UnixTimeout -eq 1 ]; then
if [ $Verbose -ge 2 ]; then
echo "Caught return code 137 from timeout"
echo "This signal means that the unix timeout utility killed wesnoth with KILL."
echo "Since we expected timeout, the test passes."
fi
return 0
elif [ "${2}" -ne "${3}" ]; then
get_code_string ${2}
echo -e ${1} ": \t" ${2} "-" $CodeString
get_code_string ${3}
echo -e "Expected result :\t" ${3} "-" $CodeString
if [ -f "error.log" ]; then
echo ""
echo "Found error.log:"
cat error.log
fi
return 1
fi
return 0
}
run_test()
{
# Argument 1 is the expected error code
# Argument 2 is the name of the test scenario
if [ "$FirstTest" -eq 1 ]; then
opts="--timeout 20000"
else
opts="--validcache --timeout 10000"
fi
if [ "$Verbose" -ge 1 ]; then
echo "wesnoth -u" $2 $opts "2> error.log"
fi
wesnoth -u $2 $opts 2> error.log
if check_errs $2 $? $1; then
FirstTest=0 #Don't start using validcache flag until at least one test has passed without error
return 0
else
return 1
fi
}
#This doesn't use the wesnoth timeout mechanism, and uses the unix core "timeout" function instead
run_test_unix_timeout()
{
# Argument 1 is the expected error code
# Argument 2 is the name of the test scenario
if [ "$FirstTest" -eq 1 ]; then
opts=""
timer=20
FirstTest=0
else
opts="--validcache"
timer=10
fi
timer1=$((timer+1))
if [ "$Verbose" -ge 1 ]; then
echo "timeout" "--kill-after=$timer1" $timer "wesnoth -u" $2 $opts "2> error.log"
fi
timeout "--kill-after=$timer1" $timer wesnoth -u $2 $opts 2> error.log
if check_errs $2 $? $1; then
return 0
else
return 1
fi
}
### Main Script Starts Here ###
Verbose=0
UnixTimeout=0
while getopts ":vduh" Option
do
case $Option in
v )
if [ $Verbose -lt 1 ]; then
Verbose=1
fi
;;
d )
if [ $Verbose -lt 2 ]; then
Verbose=2
fi
;;
u )
UnixTimeout=1
;;
h )
echo $0 "[OPTIONS]"
echo
echo -e "\t-h\tShows this help."
echo -e "\t-v\tVerbose mode."
echo -e "\t-d\tDebug mode."
echo -e "\t-u\tUse unix timeout instead of wesnoth --timeout argument."
echo -e "\t \tUnix timeout will send a TERM signal, followed by KILL."
echo
echo "The script executes tests found in the file 'wml_test_schedule'."
echo "Each line should be formatted:"
echo -e "\n\t<expected return code> <name of unit test scenario>.\n"
echo "Lines beginning # are ignored."
echo
exit 0;
esac
done
shift $(($OPTIND - 1))
old_IFS=$IFS
IFS=$'\n'
schedule=($(cat wml_test_schedule)) # array
IFS=$old_IFS
TESTS=0
COMMENTS=0
for line in "${schedule[@]}"
do
if [[ "$line" =~ \#.* ]]; then
COMMENTS=$((COMMENTS+1))
else
TESTS=$((TESTS+1))
fi
done
echo "Running" $TESTS "test scenarios."
AllPassed=1
FirstTest=1
for line in "${schedule[@]}"
do
if [[ "$line" =~ \#.* ]]; then
if [ $Verbose -ge 2 ]; then
echo "comment:" $line
fi
else
if [ $UnixTimeout -eq 1 ]; then
if run_test_unix_timeout $line; then
if [ $Verbose -ge 2 ]; then
echo "good"
fi
else
AllPassed=0
fi
else
if run_test $line; then #note: don't put run_test inside a pipe implicitly by using ! or something, this will cause the FirstTest variable not to work properly
if [ $Verbose -ge 2 ]; then
echo "good"
fi
else
AllPassed=0
fi
fi
fi
done
if [ $AllPassed -eq 0 ]; then
echo "Not all tests gave the correct result."
exit 1
else
echo "All tests gave the correct result."
exit 0
fi