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