tests: display binary, name and options of current engine
[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 export NIT_TESTING=true
23
24 unset NIT_DIR
25
26 usage()
27 {
28 e=`basename "$0"`
29 cat<<END
30 Usage: $e [options] modulenames
31 -o option Pass option to the engine
32 -v Verbose (show tests steps)
33 -h This help
34 --tap Produce TAP output
35 --engine Use a specific engine (default=nitc)
36 --noskip Do not skip a test even if the .skip file matches
37 --[no]soso Force enable (or disable) SOSO
38 END
39 }
40
41 # $1 is the pattern of the test
42 # $2 is the file to compare to
43 # the result is:
44 # 0: if the file to compare to do not exists
45 # 1: if the file match
46 # 2: if the file match with soso
47 # 3: if the file do not match
48 function compare_to_result()
49 {
50 local pattern="$1"
51 local sav="$2"
52 if [ ! -r "$sav" ]; then return 0; fi
53 diff -u "$sav" "out/$pattern.res" > "out/$pattern.diff.sav.log"
54 if [ "$?" == 0 ]; then
55 return 1
56 fi
57 [ -z "$soso" ] && return 3
58 sed '/[Ww]arning/d;/[Ee]rror/d' "out/$pattern.res" > "out/$pattern.res2"
59 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "out/$pattern.sav2"
60 grep '[Ee]rror' "out/$pattern.res" >/dev/null && echo "Error" >> "out/$pattern.res2"
61 grep '[Ee]rror' "$sav" >/dev/null && echo "Error" >> "out/$pattern.sav2"
62 diff -u "out/$pattern.sav2" "out/$pattern.res2" > "out/$pattern.diff.sav.log"
63 if [ "$?" == 0 ]; then
64 return 2
65 else
66 return 3
67 fi
68 }
69
70 # As argument: the pattern used for the file
71 function process_result()
72 {
73 ((tapcount=tapcount+1))
74 # Result
75 pattern=$1
76 description=$2
77 pack=$3
78 SAV=""
79 NSAV=""
80 FIXME=""
81 NFIXME=""
82 SOSO=""
83 NSOSO=""
84 SOSOF=""
85 NSOSOF=""
86 echo >>$xml "<testcase classname='$pack' name='$description'>"
87 for sav in "sav/$engine/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
88 compare_to_result "$pattern" "$sav"
89 case "$?" in
90 0)
91 ;; # no file
92 1)
93 SAV="$sav" ;;
94 2)
95 SOSO="$sav" ;;
96 3)
97 NSAV="$sav";;
98 esac
99 done
100 for sav in "sav/$engine/fixme/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.fail"; do
101 compare_to_result "$pattern" "$sav"
102 case "$?" in
103 0)
104 ;; # no file
105 1)
106 FIXME="$sav" ;;
107 2)
108 SOSOF="$sav" ;;
109 3)
110 NFIXME="$sav";;
111 esac
112 done
113 grep 'NOT YET IMPLEMENTED' "out/$pattern.res" >/dev/null
114 NYI="$?"
115 if [ -n "$SAV" ]; then
116 if [ -n "$tap" ]; then
117 echo "ok - $description"
118 elif [ -z "$FIXME" ]; then
119 echo "[ok] out/$pattern.res $SAV"
120 else
121 echo "[ok] out/$pattern.res $SAV - but $FIXME remains!"
122 fi
123 ok="$ok $pattern"
124 elif [ -n "$FIXME" ]; then
125 if [ -n "$tap" ]; then
126 echo "not ok - $description # TODO expected failure"
127 else
128 echo "[fixme] out/$pattern.res $FIXME"
129 fi
130 todos="$todos $pattern"
131 elif [ -n "$SOSO" ]; then
132 if [ -n "$tap" ]; then
133 echo "ok - $description # SOSO"
134 else
135 echo "[soso] out/$pattern.res $SOSO"
136 fi
137 ok="$ok $pattern"
138 elif [ "x$NYI" = "x0" ]; then
139 if [ -n "$tap" ]; then
140 echo "not ok - $description # TODO not yet implemented"
141 else
142 echo "[todo] out/$pattern.res -> not yet implemented"
143 fi
144 todos="$todos $pattern"
145 elif [ -n "$SOSOF" ]; then
146 if [ -n "$tap" ]; then
147 echo "not ok - $description # TODO SOSO expected failure"
148 else
149 echo "[fixme soso] out/$pattern.res $SOSOF"
150 fi
151 todos="$todos $pattern"
152 elif [ -n "$NSAV" ]; then
153 if [ -n "$tap" ]; then
154 echo "not ok - $description"
155 else
156 echo "[======= fail out/$pattern.res $NSAV =======]"
157 fi
158 echo >>$xml "<error message='fail out/$pattern.res $NSAV'/>"
159 echo >>$xml "<system-out><![CDATA["
160 head >>$xml -n 50 out/$pattern.diff.sav.log
161 echo >>$xml "]]></system-out>"
162 nok="$nok $pattern"
163 echo "$ii" >> "$ERRLIST"
164 elif [ -n "$NFIXME" ]; then
165 if [ -n "$tap" ]; then
166 echo "not ok - $description"
167 else
168 echo "[======= changed out/$pattern.res $NFIXME ======]"
169 fi
170 echo >>$xml "<error message='changed out/$pattern.res $NFIXME'/>"
171 echo >>$xml "<system-out><![CDATA["
172 head >>$xml -n 50 out/$pattern.diff.sav.log
173 echo >>$xml "]]></system-out>"
174 nok="$nok $pattern"
175 echo "$ii" >> "$ERRLIST"
176 else
177 if [ -n "$tap" ]; then
178 echo "ok - $description # skip no sav"
179 else
180 echo "[=== no sav ===] out/$pattern.res"
181 fi
182 echo >>$xml "<skipped/>"
183 echo >>$xml "<system-out><![CDATA["
184 cat >>$xml out/$pattern.res
185 echo >>$xml "]]></system-out>"
186 nos="$nos $pattern"
187 fi
188 if test -s out/$pattern.cmp.err; then
189 echo >>$xml "<system-err><![CDATA["
190 cat >>$xml out/$pattern.cmp.err
191 echo >>$xml "]]></system-err>"
192 fi
193 echo >>$xml "</testcase>"
194 }
195
196 need_skip()
197 {
198 test "$noskip" = true && return 1
199 if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
200 ((tapcount=tapcount+1))
201 if [ -n "$tap" ]; then
202 echo "ok - $2 # skip"
203 else
204 echo "=> $2: [skip]"
205 fi
206 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
207 return 0
208 fi
209 return 1
210 }
211
212 find_nitc()
213 {
214 ((tapcount=tapcount+1))
215 name="$enginebinname"
216 recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
217 if [[ "x$recent" == "x" ]]; then
218 if [ -n "$tap" ]; then
219 echo "not ok - find binary for $engine"
220 echo "Bail out! Could not find binary for engine $engine, aborting"
221 else
222 echo "Could not find binary for engine $engine, aborting"
223 fi
224 exit 1
225 fi
226 if [ -n "$tap" ]; then
227 echo "ok - find binary for $engine: $recent $OPT"
228 else
229 echo "Find binary for engine $engine: $recent $OPT"
230 fi
231 NITC=$recent
232 }
233
234 verbose=false
235 stop=false
236 tapcount=0
237 engine=nitc
238 noskip=
239 while [ $stop = false ]; do
240 case $1 in
241 -o) OPT="$OPT $2"; shift; shift;;
242 -v) verbose=true; shift;;
243 -h) usage; exit;;
244 --tap) tap=true; shift;;
245 --engine) engine="$2"; shift; shift;;
246 --noskip) noskip=true; shift;;
247 --soso) soso=true; shift;;
248 --nososo) nososo=true; shift;;
249 *) stop=true
250 esac
251 done
252 enginebinname=$engine
253 case $engine in
254 nitc) ;;
255 nitg) engine=nitg-s; enginebinname=nitg; OPT="--separate $OPT";;
256 nitg-s) enginebinname=nitg; OPT="--separate $OPT";;
257 nitg-e) enginebinname=nitg; OPT="--erasure $OPT";;
258 nitg-g) enginebinname=nitg; OPT="--global $OPT";;
259 nit) engine=niti ;;
260 niti) enginebinname=nit ;;
261 esac
262
263 # The default nitc compiler
264 [ -z "$NITC" ] && find_nitc
265
266 # Set NIT_DIR if needed
267 [ -z "$NIT_DIR" ] && export NIT_DIR=..
268
269 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
270 TIMEOUT="timelimit -t 600"
271 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
272 TIMEOUT="timeout 600s"
273 else
274 echo "No timelimit or timeout command detected. Tests may hang :("
275 fi
276
277 # Mark to distinguish files among tests
278 # MARK=
279
280 # File where error tests are outputed
281 # Old ERRLIST is backuped
282 ERRLIST=${ERRLIST:-errlist}
283 ERRLIST_TARGET=$ERRLIST
284
285 if [ $# = 0 ]; then
286 usage;
287 exit
288 fi
289
290 # Initiate new ERRLIST
291 if [ "x$ERRLIST" = "x" ]; then
292 ERRLIST=/dev=null
293 else
294 ERRLIST=$ERRLIST.tmp
295 > "$ERRLIST"
296 fi
297
298 ok=""
299 nok=""
300 todos=""
301 xml="tests-$engine.xml"
302 echo >$xml "<testsuites><testsuite>"
303
304 # CLEAN the out directory
305 rm -rf out/ 2>/dev/null
306 mkdir out 2>/dev/null
307
308 for ii in "$@"; do
309 if [ ! -f $ii ]; then
310 echo "File '$ii' does not exist."
311 continue
312 fi
313 f=`basename "$ii" .nit`
314
315 pack=`echo $ii | perl -p -e 's|^../([^/]*)/([a-zA-Z_]*).*|\1.\2| || s|^([a-zA-Z]*)[^_]*_([a-zA-Z]*).*|\1.\2| || s|\W*([a-zA-Z_]*).*|\1|'`
316
317 # Sould we skip the file for this engine?
318 need_skip $f $f $pack && continue
319
320 tmp=${ii/../AA}
321 if [ "x$tmp" = "x$ii" ]; then
322 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
323 else
324 includes="-I alt"
325 fi
326
327 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
328 bf=`basename $i .nit`
329 ff="out/$bf"
330
331 # Sould we skip the alternative for this engine?
332 need_skip $bf $bf $pack && continue
333
334 test -z "$tap" && echo -n "=> $bf: "
335
336 if [ -f "$f.inputs" ]; then
337 inputs="$f.inputs"
338 else
339 inputs=/dev/null
340 fi
341
342 if [ "$engine" = "niti" ]; then
343 cat > "./$ff.bin" <<END
344 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
345 END
346 chmod +x "./$ff.bin"
347 > "$ff.cmp.err"
348 > "$ff.compile.log"
349 ERR=0
350 else
351 # Compile
352 if [ "x$verbose" = "xtrue" ]; then
353 echo ""
354 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
355 fi
356 NIT_NO_STACK=1 $TIMEOUT $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
357 ERR=$?
358 if [ "x$verbose" = "xtrue" ]; then
359 cat "$ff.compile.log"
360 cat >&2 "$ff.cmp.err"
361 fi
362 fi
363 if [ "$ERR" != 0 ]; then
364 test -z "$tap" && echo -n "! "
365 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
366 process_result $bf $bf $pack
367 elif [ -x "./$ff.bin" ]; then
368 test -z "$tap" && echo -n ". "
369 # Execute
370 args=""
371 if [ "x$verbose" = "xtrue" ]; then
372 echo ""
373 echo "NIT_NO_STACK=1 ./$ff.bin" $args
374 fi
375 NIT_NO_STACK=1 $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
376 if [ "x$verbose" = "xtrue" ]; then
377 cat "$ff.res"
378 cat >&2 "$ff.err"
379 fi
380 if [ -f "$ff.write" ]; then
381 cat "$ff.write" >> "$ff.res"
382 elif [ -d "$ff.write" ]; then
383 LANG=C /bin/ls -F $ff.write >> "$ff.res"
384 fi
385 cp "$ff.res" "$ff.res2"
386 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
387 process_result $bf $bf $pack
388
389 if [ -f "$f.args" ]; then
390 fargs=$f.args
391 cptr=0
392 while read line; do
393 ((cptr=cptr+1))
394 args="$line"
395 bff=$bf"_args"$cptr
396 fff=$ff"_args"$cptr
397 name="$bf args $cptr"
398
399 # Sould we skip the input for this engine?
400 need_skip $bff " $name" $pack && continue
401
402 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
403 if [ "x$verbose" = "xtrue" ]; then
404 echo ""
405 echo "NIT_NO_STACK=1 ./$ff.bin" $args
406 fi
407 test -z "$tap" && echo -n "==> $name "
408 echo "./$ff.bin $args" > "./$fff.bin"
409 chmod +x "./$fff.bin"
410 sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $inputs > $fff.res 2>$fff.err"
411 if [ "x$verbose" = "xtrue" ]; then
412 cat "$fff.res"
413 cat >&2 "$fff.err"
414 fi
415 if [ -f "$fff.write" ]; then
416 cat "$fff.write" >> "$fff.res"
417 elif [ -d "$fff.write" ]; then
418 LANG=C /bin/ls -F $fff.write >> "$fff.res"
419 fi
420 if [ -s "$fff.err" ]; then
421 cp "$fff.res" "$fff.res2"
422 cat "$fff.err" "$fff.res2" > "$fff.res"
423 fi
424 process_result $bff " $name" $pack
425 done < $fargs
426 fi
427 else
428 test -z "$tap" && echo -n "! "
429 cat "$ff.cmp.err" > "$ff.res"
430 echo "Compilation error" > "$ff.res"
431 process_result $bf "$bf" $pack
432 fi
433 done
434 done
435
436 if [ -n "$tap" ]; then
437 echo "1..$tapcount"
438 echo "# ok:" `echo $ok | wc -w`
439 echo "# not ok:" `echo $nok | wc -w`
440 echo "# no sav:" `echo $nos | wc -w`
441 echo "# todo/fixme:" `echo $todos | wc -w`
442 exit
443 fi
444
445 echo "engine: $engine ($enginebinname $OPT)"
446 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
447
448 if [ -n "$nok" ]; then
449 echo "fail: $nok"
450 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
451 fi
452 if [ -n "$nos" ]; then
453 echo "no sav: $nos"
454 fi
455 if [ -n "$todos" ]; then
456 echo "todo/fixme: $todos"
457 fi
458
459 # write $ERRLIST
460 if [ "x$ERRLIST" != "x" ]; then
461 if [ -x "$ERRLIST_TARGET" ]; then
462 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
463 fi
464 mv $ERRLIST $ERRLIST_TARGET
465 fi
466
467 echo >>$xml "</testsuite></testsuites>"
468
469 if [ -n "$nok" ]; then
470 exit 1
471 else
472 exit 0
473 fi