Merge: More tweaks to support the Java FFI on turing
[nit.git] / tests / tests.sh
index 4d8ad78..faed626 100755 (executable)
@@ -27,13 +27,13 @@ unset NIT_DIR
 
 # Get the first Java lib available
 shopt -s nullglob
-paths=`echo /usr/lib/jvm/*/`
-paths=($paths) 
-JAVA_HOME=${paths[0]}
+JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
 
 paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/`
 paths=($paths) 
 JNI_LIB_PATH=${paths[0]}
+echo $JAVA_HOME
+echo $JNI_LIB_PATH
 shopt -u nullglob
 
 outdir="out"
@@ -51,9 +51,72 @@ Usage: $e [options] modulenames
 --noskip    Do not skip a test even if the .skip file matches
 --outdir    Use a specific output folder (default=out/)
 --compdir   Use a specific temporary compilation folder (default=.nit_compile)
+--node      Run as a node in parallel, will not output context information
 END
 }
 
+# Run a command with a timeout and a time count.
+# Options:
+#   -o file    write the user time into file (REQUIRED). see `-o` in `man time`
+#   -a         append the time to the file (instead of overwriting it). see `-a` in `man time`
+saferun()
+{
+       local stop=false
+       local o=
+       local a=
+       while [ $stop = false ]; do
+               case $1 in
+                       -o) o="$2"; shift; shift;;
+                       -a) a="-a"; shift;;
+                       *) stop=true
+               esac
+       done
+       if test -n "$TIME"; then
+               $TIME -o "$o" $a $TIMEOUT "$@"
+       else
+               if test -n "$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
+               $TIMEOUT "$@"
+       fi
+}
+
+# Output a timestamp attribute for XML, or an empty line
+timestamp()
+{
+       if test -n "$TIMESTAMP"; then
+               echo "timestamp='`$TIMESTAMP`'"
+       else
+               echo ""
+       fi
+
+}
+
+# Get platform specific commands ##########################
+
+# Detect a working timeout
+if sh -c "timelimit echo" 1>/dev/null 2>&1; then
+       TIMEOUT="timelimit -t 600"
+elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
+       TIMEOUT="timeout 600s"
+else
+       echo "No timelimit or timeout command detected. Tests may hang :("
+fi
+
+# Detect a working time command
+if env time --quiet -f%U true 2>/dev/null; then
+       TIME="env time --quiet -f%U"
+elif env time -f%U true 2>/dev/null; then
+       TIME="env time -f%U"
+else
+       TIME=
+fi
+
+# Detect a working date command
+if date -Iseconds >/dev/null 2>&1; then
+       TIMESTAMP="date -Iseconds"
+else
+       TIMESTAMP=
+fi
+
 # $1 is the pattern of the test
 # $2 is the file to compare to
 # the result is:
@@ -101,7 +164,7 @@ function process_result()
        OLD=""
        LIST=""
        FIRST=""
-       echo >>$xml "<testcase classname='$pack' name='$description' time='`cat $outdir/$pattern.time.out`' timestamp='`date -Iseconds`'>"
+       echo >>$xml "<testcase classname='$pack' name='$description' time='`cat $outdir/$pattern.time.out`' `timestamp`>"
        #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
        for savdir in $savdirs; do
                sav=$savdir/fixme/$pattern.res
@@ -240,12 +303,12 @@ need_skip()
        test "$noskip" = true && return 1
        if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
                echo "=> $2: [skip]"
-               echo >>$xml "<testcase classname='$3' name='$2' timestamp='`date -Iseconds`'><skipped/></testcase>"
+               echo >>$xml "<testcase classname='$3' name='$2' `timestamp`><skipped/></testcase>"
                return 0
        fi
        if test -n "$isinterpret" && echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
                echo "=> $2: [skip exec]"
-               echo >>$xml "<testcase classname='$3' name='$2' timestamp='`date -Iseconds`'><skipped/></testcase>"
+               echo >>$xml "<testcase classname='$3' name='$2' `timestamp`><skipped/></testcase>"
                return 0
        fi
        return 1
@@ -278,11 +341,14 @@ find_nitc()
                echo "Could not find binary for engine $engine, aborting"
                exit 1
        fi
-       echo "Find binary for engine $engine: $recent $OPT"
+       if [ "x$isnode" = "xfalse" ]; then
+               echo "Found binary for engine $engine: $recent $OPT"
+       fi
        NITC=$recent
 }
 
 verbose=false
+isnode=false
 stop=false
 engine=nitg
 noskip=
@@ -296,6 +362,7 @@ while [ $stop = false ]; do
                --noskip) noskip=true; shift;;
                --outdir) outdir="$2"; shift; shift;;
                --compdir) compdir="$2"; shift; shift;;
+               --node) isnode=true; shift;;
                *) stop=true
        esac
 done
@@ -306,22 +373,27 @@ case $engine in
                engine=nitg-s;
                enginebinname=nitg;
                OPT="--separate $OPT --compile-dir $compdir"
+               savdirs="sav/nitg-common/"
                ;;
        nitg-s)
                enginebinname=nitg;
                OPT="--separate $OPT --compile-dir $compdir"
+               savdirs="sav/nitg-common/"
                ;;
        nitg-e)
                enginebinname=nitg;
                OPT="--erasure $OPT --compile-dir $compdir"
+               savdirs="sav/nitg-common/"
                ;;
        nitg-sg)
                enginebinname=nitg;
                OPT="--semi-global $OPT --compile-dir $compdir"
+               savdirs="sav/nitg-common/"
                ;;
        nitg-g)
                enginebinname=nitg;
                OPT="--global $OPT --compile-dir $compdir"
+               savdirs="sav/nitg-common/"
                ;;
        nit)
                engine=niti
@@ -358,27 +430,23 @@ savdirs="sav/$engine $savdirs sav/"
 # Set NIT_DIR if needed
 [ -z "$NIT_DIR" ] && export NIT_DIR=..
 
-if sh -c "timelimit echo" 1>/dev/null 2>&1; then
-       TIMEOUT="timelimit -t 600"
-elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
-       TIMEOUT="timeout 600s"
-else
-       echo "No timelimit or timeout command detected. Tests may hang :("
-fi
-
 # Mark to distinguish files among tests
 # MARK=
 
-# File where error tests are outputed
-# Old ERRLIST is backuped
-ERRLIST=${ERRLIST:-errlist}
-ERRLIST_TARGET=$ERRLIST
-
 if [ $# = 0 ]; then
        usage;
        exit
 fi
 
+# CLEAN the out directory
+rm -rf "$outdir/" 2>/dev/null
+mkdir "$outdir" 2>/dev/null
+
+# File where error tests are outputed
+# Old ERRLIST is backuped
+ERRLIST=${ERRLIST:-errlist}
+ERRLIST_TARGET=$ERRLIST
+
 # Initiate new ERRLIST
 if [ "x$ERRLIST" = "x" ]; then
        ERRLIST=/dev/null
@@ -390,12 +458,16 @@ fi
 ok=""
 nok=""
 todos=""
-xml="tests-$engine.xml"
-echo >$xml "<testsuites><testsuite>"
 
-# CLEAN the out directory
-rm -rf "$outdir/" 2>/dev/null
-mkdir "$outdir" 2>/dev/null
+if [ "x$XMLDIR" = "x" ]; then
+       xml="tests-$engine.xml"
+else
+       sum=`echo $@ | md5sum | cut -f1 -d " "`
+       xml="$XMLDIR/tests-$engine-$sum.xml"
+       mkdir -p "$XMLDIR"
+fi
+
+echo >$xml "<testsuites><testsuite>"
 
 for ii in "$@"; do
        if [ ! -f $ii ]; then
@@ -440,7 +512,7 @@ for ii in "$@"; do
 
                if [ -n "$isinterpret" ]; then
                        cat > "$ff.bin" <<END
-exec $NITC --no-color $OPT "$i" $includes -- "\$@"
+exec $NITC --no-color $OPT $includes -- "$i" "\$@"
 END
                        chmod +x "$ff.bin"
                        > "$ff.cmp.err"
@@ -456,10 +528,10 @@ END
                        # Compile
                        if [ "x$verbose" = "xtrue" ]; then
                                echo ""
-                               echo $NITC --no-color $OPT -o "$ffout" "$i" "$includes" $nocc
+                               echo $NITC --no-color $OPT -o "$ffout" "$includes" $nocc "$i"
                        fi
                        NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
-                               /usr/bin/time --quiet -f%U -o "$ff.time.out" $TIMEOUT $NITC --no-color $OPT -o "$ffout" "$i" $includes $nocc 2> "$ff.cmp.err" > "$ff.compile.log"
+                               saferun -o "$ff.time.out" $NITC --no-color $OPT -o "$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
                        ERR=$?
                        if [ "x$verbose" = "xtrue" ]; then
                                cat "$ff.compile.log"
@@ -471,7 +543,7 @@ END
                        chmod +x "$ff.bin"
                        if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev/null; then
                                echo " [skip] do no not imports kernel"
-                               echo >>$xml "<testcase classname='$pack' name='$bf' timestamp='`date -Iseconds`'><skipped/></testcase>"
+                               echo >>$xml "<testcase classname='$pack' name='$bf' `timestamp`><skipped/></testcase>"
                                continue
                        fi
                fi
@@ -497,9 +569,9 @@ END
                        if [ "x$verbose" = "xtrue" ]; then
                                echo ""
                                echo "NIT_NO_STACK=1 $ff.bin" $args
-                       fi      
+                       fi
                        NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
-                               /usr/bin/time --quiet -f%U -a -o "$ff.time.out" $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
+                               saferun -a -o "$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
                        mv $ff.time.out $ff.times.out
                        awk '{ SUM += $1} END { print SUM }' $ff.times.out > $ff.time.out
 
@@ -544,7 +616,7 @@ END
                                        echo -n "==> $name "
                                        echo "$ff.bin $args" > "$fff.bin"
                                        chmod +x "$fff.bin"
-                                       WRITE="$fff.write" /usr/bin/time --quiet -f%U -o "$fff.time.out" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $ffinputs > $fff.res 2>$fff.err"
+                                       WRITE="$fff.write" saferun -o "$fff.time.out" sh -c "NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
                                        if [ "x$verbose" = "xtrue" ]; then
                                                cat "$fff.res"
                                                cat >&2 "$fff.err"
@@ -574,21 +646,23 @@ END
        done
 done
 
-echo "engine: $engine ($enginebinname $OPT)"
-echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
+if [ "x$isnode" = "xfalse" ]; then
+       echo "engine: $engine ($enginebinname $OPT)"
+       echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
 
-if [ -n "$nok" ]; then
-       echo "fail: $nok"
-       echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
-fi
-if [ -n "$nos" ]; then
-       echo "no sav: $nos"
-fi
-if [ -n "$todos" ]; then
-       echo "todo/fixme: $todos"
-fi
-if [ -n "$remains" ]; then
-       echo "sav that remains: $remains"
+       if [ -n "$nok" ]; then
+               echo "fail: $nok"
+               echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
+       fi
+       if [ -n "$nos" ]; then
+               echo "no sav: $nos"
+       fi
+       if [ -n "$todos" ]; then
+               echo "todo/fixme: $todos"
+       fi
+       if [ -n "$remains" ]; then
+               echo "sav that remains: $remains"
+       fi
 fi
 
 # write $ERRLIST