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