tests.sh: add env var to move xml file
[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
403 if [ "x$XMLDIR" = "x" ]; then
404 xml="tests-$engine.xml"
405 else
406 xml="$XMLDIR/tests-$engine.xml"
407 fi
408
409 echo >$xml "<testsuites><testsuite>"
410
411 for ii in "$@"; do
412 if [ ! -f $ii ]; then
413 echo "File '$ii' does not exist."
414 continue
415 fi
416 f=`basename "$ii" .nit`
417
418 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|'`
419
420 # Sould we skip the file for this engine?
421 need_skip $f $f $pack && continue
422
423 tmp=${ii/../AA}
424 if [ "x$tmp" = "x$ii" ]; then
425 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
426 else
427 includes="-I alt"
428 fi
429
430 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
431 bf=`basename $i .nit`
432 ff="$outdir/$bf"
433
434 # Sould we skip the alternative for this engine?
435 need_skip $bf $bf $pack && continue
436
437 echo -n "=> $bf: "
438
439 if [ -f "$f.inputs" ]; then
440 inputs="$f.inputs"
441 export MNIT_READ_INPUT="$f.inputs"
442 else
443 inputs=/dev/null
444 export MNIT_READ_INPUT=/dev/null
445 fi
446
447 ffout="$ff.bin"
448 if [ "$engine" = "emscripten" ]; then
449 ffout="$ff.bin.js"
450 fi
451
452 if [ -n "$isinterpret" ]; then
453 cat > "$ff.bin" <<END
454 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
455 END
456 chmod +x "$ff.bin"
457 > "$ff.cmp.err"
458 > "$ff.compile.log"
459 ERR=0
460 echo 0.0 > "$ff.time.out"
461 else
462 if skip_cc "$bf"; then
463 nocc="--no-cc"
464 else
465 nocc=
466 fi
467 # Compile
468 if [ "x$verbose" = "xtrue" ]; then
469 echo ""
470 echo $NITC --no-color $OPT -o "$ffout" "$i" "$includes" $nocc
471 fi
472 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
473 /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"
474 ERR=$?
475 if [ "x$verbose" = "xtrue" ]; then
476 cat "$ff.compile.log"
477 cat >&2 "$ff.cmp.err"
478 fi
479 fi
480 if [ "$engine" = "emscripten" ]; then
481 echo > "$ff.bin" "nodejs $ffout \"\$@\""
482 chmod +x "$ff.bin"
483 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev/null; then
484 echo " [skip] do no not imports kernel"
485 echo >>$xml "<testcase classname='$pack' name='$bf' timestamp='`date -Iseconds`'><skipped/></testcase>"
486 continue
487 fi
488 fi
489 if [ "$ERR" != 0 ]; then
490 echo -n "! "
491 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
492 process_result $bf $bf $pack
493 elif [ -n "$nocc" ]; then
494 # not compiled
495 echo -n "nocc "
496 > "$ff.res"
497 process_result $bf $bf $pack
498 elif [ -x "$ff.bin" ]; then
499 if skip_exec "$bf"; then
500 # No exec
501 > "$ff.res"
502 process_result $bf $bf $pack
503 break
504 fi
505 echo -n ". "
506 # Execute
507 args=""
508 if [ "x$verbose" = "xtrue" ]; then
509 echo ""
510 echo "NIT_NO_STACK=1 $ff.bin" $args
511 fi
512 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
513 /usr/bin/time -f%U -a -o "$ff.time.out" $TIMEOUT "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
514 mv $ff.time.out $ff.times.out
515 awk '{ SUM += $1} END { print SUM }' $ff.times.out > $ff.time.out
516
517 if [ "x$verbose" = "xtrue" ]; then
518 cat "$ff.res"
519 cat >&2 "$ff.err"
520 fi
521 if [ -f "$ff.write" ]; then
522 cat "$ff.write" >> "$ff.res"
523 elif [ -d "$ff.write" ]; then
524 LANG=C /bin/ls -F $ff.write >> "$ff.res"
525 fi
526 cp "$ff.res" "$ff.res2"
527 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
528 process_result $bf $bf $pack
529
530 if [ -f "$f.args" ]; then
531 fargs=$f.args
532 cptr=0
533 while read line; do
534 ((cptr=cptr+1))
535 args="$line"
536 bff=$bf"_args"$cptr
537 fff=$ff"_args"$cptr
538 name="$bf args $cptr"
539
540 # Sould we skip the input for this engine?
541 need_skip $bff " $name" $pack && continue
542
543 # use a specific inputs file, if required
544 if [ -f "$bff.inputs" ]; then
545 ffinputs="$bff.inputs"
546 else
547 ffinputs=$inputs
548 fi
549
550 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
551 if [ "x$verbose" = "xtrue" ]; then
552 echo ""
553 echo "NIT_NO_STACK=1 $ff.bin" $args
554 fi
555 echo -n "==> $name "
556 echo "$ff.bin $args" > "$fff.bin"
557 chmod +x "$fff.bin"
558 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"
559 if [ "x$verbose" = "xtrue" ]; then
560 cat "$fff.res"
561 cat >&2 "$fff.err"
562 fi
563 if [ -f "$fff.write" ]; then
564 cat "$fff.write" >> "$fff.res"
565 elif [ -d "$fff.write" ]; then
566 LANG=C /bin/ls -F $fff.write >> "$fff.res"
567 fi
568 if [ -s "$fff.err" ]; then
569 cp "$fff.res" "$fff.res2"
570 cat "$fff.err" "$fff.res2" > "$fff.res"
571 fi
572 process_result $bff " $name" $pack
573 done < $fargs
574 fi
575 elif [ -f "$ff.bin" ]; then
576 #Not executable (platform?)"
577 > "$ff.res"
578 process_result $bf "$bf" $pack
579 else
580 echo -n "! "
581 cat "$ff.cmp.err" > "$ff.res"
582 echo "Compilation error" > "$ff.res"
583 process_result $bf "$bf" $pack
584 fi
585 done
586 done
587
588 if [ "x$isnode" = "xfalse" ]; then
589 echo "engine: $engine ($enginebinname $OPT)"
590 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
591
592 if [ -n "$nok" ]; then
593 echo "fail: $nok"
594 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
595 fi
596 if [ -n "$nos" ]; then
597 echo "no sav: $nos"
598 fi
599 if [ -n "$todos" ]; then
600 echo "todo/fixme: $todos"
601 fi
602 if [ -n "$remains" ]; then
603 echo "sav that remains: $remains"
604 fi
605 fi
606
607 # write $ERRLIST
608 if [ "x$ERRLIST" != "x" ]; then
609 if [ -f "$ERRLIST_TARGET" ]; then
610 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
611 fi
612 uniq $ERRLIST > $ERRLIST_TARGET
613 rm $ERRLIST
614 fi
615
616 echo >>$xml "</testsuite></testsuites>"
617
618 if [ -n "$nok" ]; then
619 exit 1
620 else
621 exit 0
622 fi