tests: do not overwrite 'errlist' on hard exit
[nit.git] / tests / tests.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # This shell script compile, run and verify Nit program files
19
20 # Set lang do default to avoid failed tests because of locale
21 export LANG=C
22
23 # The default nitc compiler
24 [ -z "$NITC" ] && NITC=../bin/nitc
25
26 usage()
27 {
28 e=`basename "$0"`
29 cat<<END
30 Usage: $e [options] modulenames
31 -o option Pass option to nitc
32 -v Verbose (show tests steps)
33 -h This help
34 END
35 }
36
37 # As argument: the pattern used for the file
38 function process_result()
39 {
40 # Result
41 pattern=$1
42 SAV=""
43 FAIL=""
44 if [ -r "sav/$pattern.sav" ]; then
45 diff -u "$pattern.res" "sav/$pattern.sav" > "$pattern.diff.sav.log"
46 if [ "$?" == 0 ]; then
47 SAV=OK
48 else
49 SAV=NOK
50 fi
51 fi
52 if [ -r "sav/$pattern.fail" ]; then
53 diff -u "$pattern.res" "sav/$pattern.fail" > "$pattern.diff.fail.log"
54 if [ "$?" == 0 ]; then
55 FAIL=OK
56 else
57 FAIL=NOK
58 fi
59 fi
60 if [ "x$SAV" = "xOK" ]; then
61 if [ "x$FAIL" = "x" ]; then
62 echo "[ok] $pattern.res"
63 else
64 echo "[ok] $pattern.res - but sav/$pattern.fail remains!"
65 fi
66 ok="$ok $pattern"
67 elif [ "x$FAIL" = "xOK" ]; then
68 echo "[fail] $pattern.res"
69 ok="$ok $pattern"
70 elif [ "x$SAV" = "xNOK" ]; then
71 echo "[======= fail $pattern.res sav/$pattern.sav =======]"
72 nok="$nok $ff"
73 echo "$ii" >> "$ERRLIST"
74 elif [ "x$FAIL" = "xNOK" ]; then
75 echo "[======= changed $pattern.res sav/$pattern.fail ======]"
76 nok="$nok $ff"
77 echo "$ii" >> "$ERRLIST"
78 else
79 echo "[=== no sav ===] $pattern.res"
80 nos="$nos $pattern"
81 fi
82 }
83
84
85 verbose=false
86 stop=false
87 while [ $stop = false ]; do
88 case $1 in
89 -o) OPT="$OPT $2"; shift; shift;;
90 -v) verbose=true; shift;;
91 -h) usage; exit;;
92 *) stop=true
93 esac
94 done
95
96 # Mark to distinguish files among tests
97 # MARK=
98
99 # File where error tests are outputed
100 # Old ERRLIST is backuped
101 ERRLIST=${ERRLIST:-errlist}
102 ERRLIST_TARGET=$ERRLIST
103
104 if [ $# = 0 ]; then
105 usage;
106 exit
107 fi
108
109 # Initiate new ERRLIST
110 if [ "x$ERRLIST" = "x" ]; then
111 ERRLIST=/dev=null
112 else
113 ERRLIST=$ERRLIST.tmp
114 > "$ERRLIST"
115 fi
116
117 ok=""
118 nok=""
119
120 for ii in "$@"; do
121 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
122 f=`basename "$ii" .nit`
123 d=`dirname "$ii"`
124 ff="$f"
125 i="$ii"
126 if [ "x$alt" != "x" ]; then
127 test -d alt || mkdir -p alt
128 i="alt/${f}_$alt.nit"
129 ff="${ff}_$alt"
130 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
131 fi
132 ff="$ff$MARK"
133
134 echo -n "=> $i: "
135
136 rm "$ff.res" "$ff.err" "$ff.write" "$ff.bin" 2> /dev/null
137
138 # Compile
139 if [ "x$verbose" = "xtrue" ]; then
140 echo ""
141 echo $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard
142 fi
143 $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard 2> "$ff.cmp.err" > "$ff.compile.log"
144 ERR=$?
145 if [ "x$verbose" = "xtrue" ]; then
146 cat "$ff.compile.log"
147 cat >&2 "$ff.cmp.err"
148 fi
149 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
150 if [ "$ERR" != 0 ]; then
151 echo -n "! "
152 cp "$ff.cmp.err" "$ff.res"
153 process_result $ff
154 elif [ -x "./$ff.bin" ]; then
155 echo -n ". "
156 # Execute
157 args=""
158 if [ "x$verbose" = "xtrue" ]; then
159 echo ""
160 echo "./$ff.bin" $args
161 fi
162 if [ -f "$f.inputs" ]; then
163 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
164 else
165 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
166 fi
167 if [ "x$verbose" = "xtrue" ]; then
168 cat "$ff.res"
169 cat >&2 "$ff.err"
170 fi
171 if [ -f "$ff.write" ]; then
172 cat "$ff.write" >> "$ff.res"
173 fi
174 if [ -s "$ff.err" ]; then
175 cat "$ff.err" >> "$ff.res"
176 fi
177 process_result $ff
178
179 if [ -f "$f.args" ]; then
180 fargs=$f.args
181 cptr=0
182 cat $fargs |
183 while read line; do
184 ((cptr=cptr+1))
185 args=$line
186 fff=$ff"_args"$cptr
187 if [ "x$verbose" = "xtrue" ]; then
188 echo ""
189 echo "./$ff.bin" $args
190 fi
191 echo -n "==> args #"$cptr " "
192 if [ -f "$f.inputs" ]; then
193 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
194 else
195 sh -c "./$ff.bin ''$args > $fff.res 2>$fff.err"
196 fi
197 if [ "x$verbose" = "xtrue" ]; then
198 cat "$fff.res"
199 cat >&2 "$fff.err"
200 fi
201 if [ -f "$fff.write" ]; then
202 cat "$fff.write" >> "$fff.res"
203 fi
204 if [ -s "$fff.err" ]; then
205 cat "$fff.err" >> "$fff.res"
206 fi
207 process_result $fff
208 done
209 fi
210 else
211 echo -n "! "
212 echo "Compilation error" > "$ff.res"
213 process_result $ff
214 fi
215 done
216 done
217
218 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
219
220 if [ -n "$nok" ]; then
221 echo "fail: $nok"
222 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
223 fi
224 if [ -n "$nos" ]; then
225 echo "no sav: $nos"
226 fi
227
228 # write $ERRLIST
229 if [ "x$ERRLIST" != "x" ]; then
230 if [ -x "$ERRLIST_TARGET" ]; then
231 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
232 fi
233 mv $ERRLIST $ERRLIST_TARGET
234 fi
235
236 if [ -n "$nok" ]; then
237 exit 1
238 else
239 exit 0
240 fi