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