Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
remzi-arpacidusseau
GitHub Repository: remzi-arpacidusseau/ostep-projects
Path: blob/master/tester/run-tests.sh
909 views
1
#! /usr/bin/env bash
2
3
GREEN='\033[0;32m'
4
RED='\033[0;31m'
5
NONE='\033[0m'
6
7
# run_test testdir testnumber
8
run_test () {
9
local testdir=$1
10
local testnum=$2
11
local verbose=$3
12
13
# pre: execute this after before the test is done, to set up
14
local prefile=$testdir/$testnum.pre
15
if [[ -f $prefile ]]; then
16
eval $(cat $prefile)
17
if (( $verbose == 1 )); then
18
echo -n "pre-test: "
19
cat $prefile
20
fi
21
fi
22
local testfile=$testdir/$testnum.run
23
if (( $verbose == 1 )); then
24
echo -n "test: "
25
cat $testfile
26
fi
27
eval $(cat $testfile) > tests-out/$testnum.out 2> tests-out/$testnum.err
28
echo $? > tests-out/$testnum.rc
29
30
# post: execute this after the test is done, to clean up
31
local postfile=$testdir/$testnum.post
32
if [[ -f $postfile ]]; then
33
eval $(cat $postfile)
34
if (( $verbose == 1 )); then
35
echo -n "post-test: "
36
cat $postfile
37
fi
38
fi
39
return
40
}
41
42
print_error_message () {
43
local testnum=$1
44
local contrunning=$2
45
local filetype=$3
46
builtin echo -e "test $testnum: ${RED}$testnum.$filetype incorrect${NONE}"
47
echo " what results should be found in file: $testdir/$testnum.$filetype"
48
echo " what results produced by your program: tests-out/$testnum.$filetype"
49
echo " compare the two using diff, cmp, or related tools to debug, e.g.:"
50
echo " prompt> diff $testdir/$testnum.$filetype tests-out/$testnum.$filetype"
51
echo " See tests/$testnum.run for what is being run"
52
if (( $contrunning == 0 )); then
53
exit 1
54
fi
55
}
56
57
# check_test testdir testnumber contrunning out/err
58
check_test () {
59
local testdir=$1
60
local testnum=$2
61
local contrunning=$3
62
local filetype=$4
63
64
# option to use cmp instead?
65
returnval=$(diff $testdir/$testnum.$filetype tests-out/$testnum.$filetype)
66
if (( $? == 0 )); then
67
echo 0
68
else
69
echo 1
70
fi
71
}
72
73
# run_and_check testdir testnumber contrunning verbose printerror
74
# testnumber: the test to run and check
75
# printerrer: if 1, print an error if test does not exist
76
run_and_check () {
77
local testdir=$1
78
local testnum=$2
79
local contrunning=$3
80
local verbose=$4
81
local failmode=$5
82
83
if [[ ! -f $testdir/$testnum.run ]]; then
84
if (( $failmode == 1 )); then
85
echo "test $testnum does not exist" >&2; exit 1
86
fi
87
exit 0
88
fi
89
if (( $verbose == 1 )); then
90
echo -n -e "running test $testnum: "
91
cat $testdir/$testnum.desc
92
fi
93
run_test $testdir $testnum $verbose
94
rccheck=$(check_test $testdir $testnum $contrunning rc)
95
outcheck=$(check_test $testdir $testnum $contrunning out)
96
errcheck=$(check_test $testdir $testnum $contrunning err)
97
othercheck=0
98
if [[ -f $testdir/$testnum.other ]]; then
99
othercheck=$(check_test $testdir $testnum $contrunning other)
100
fi
101
# echo "results: outcheck:$outcheck errcheck:$errcheck"
102
if (( $rccheck == 0 )) && (( $outcheck == 0 )) && (( $errcheck == 0 )) && (( $othercheck == 0 )); then
103
echo -e "test $testnum: ${GREEN}passed${NONE}"
104
if (( $verbose == 1 )); then
105
echo ""
106
fi
107
else
108
if (( $rccheck == 1 )); then
109
print_error_message $testnum $contrunning rc
110
fi
111
if (( $outcheck == 1 )); then
112
print_error_message $testnum $contrunning out
113
fi
114
if (( $errcheck == 1 )); then
115
print_error_message $testnum $contrunning err
116
fi
117
if (( $othercheck == 1 )); then
118
print_error_message $testnum $contrunning other
119
fi
120
fi
121
}
122
123
# usage: call when args not parsed, or when help needed
124
usage () {
125
echo "usage: run-tests.sh [-h] [-v] [-t test] [-c] [-s] [-d testdir]"
126
echo " -h help message"
127
echo " -v verbose"
128
echo " -t n run only test n"
129
echo " -c continue even after failure"
130
echo " -s skip pre-test initialization"
131
echo " -d testdir run tests from testdir"
132
return 0
133
}
134
135
#
136
# main program
137
#
138
verbose=0
139
testdir="tests"
140
contrunning=0
141
skippre=0
142
specific=""
143
144
args=`getopt hvsct:d: $*`
145
if [[ $? != 0 ]]; then
146
usage; exit 1
147
fi
148
149
set -- $args
150
for i; do
151
case "$i" in
152
-h)
153
usage
154
exit 0
155
shift;;
156
-v)
157
verbose=1
158
shift;;
159
-c)
160
contrunning=1
161
shift;;
162
-s)
163
skippre=1
164
shift;;
165
-t)
166
specific=$2
167
shift
168
number='^[0-9]+$'
169
if ! [[ $specific =~ $number ]]; then
170
usage
171
echo "-t must be followed by a number" >&2; exit 1
172
fi
173
shift;;
174
-d)
175
testdir=$2
176
shift
177
shift;;
178
--)
179
shift; break;;
180
esac
181
done
182
183
# need a test directory; must be named "tests-out"
184
if [[ ! -d tests-out ]]; then
185
mkdir tests-out
186
fi
187
188
# do a one-time setup step
189
if (( $skippre == 0 )); then
190
if [[ -f tests/pre ]]; then
191
echo -e "doing one-time pre-test (use -s to suppress)"
192
source tests/pre
193
if (( $? != 0 )); then
194
echo "pre-test: failed"
195
exit 1
196
fi
197
echo ""
198
fi
199
fi
200
201
# run just one test
202
if [[ $specific != "" ]]; then
203
run_and_check $testdir $specific $contrunning $verbose 1
204
exit 0
205
fi
206
207
# run all tests
208
(( testnum = 1 ))
209
while true; do
210
run_and_check $testdir $testnum $contrunning $verbose 0
211
(( testnum = $testnum + 1 ))
212
done
213
214
exit 0
215
216