tests.sh: run junit2html to render xml files
[nit.git] / tests / tests.sh
index 0e15c3e..2aff98a 100755 (executable)
 # This shell script compile, run and verify Nit program files
 
 # Set lang do default to avoid failed tests because of locale
-export LANG=C
-export LC_ALL=C
+export LANG=C.UTF-8
+
+# Explore candidates for LC_ALL
+#
+# If C.UTF-8, the most agnostic UTF-8 locale is supported, we
+# use it, otherwise we default to the UTF-8 system locale, and
+# if unavailable, we default on C.
+#
+# Note that C however is not guaranteed to be UTF-8, and
+# some tests may fail when relying on Unicode semantics.
+locale_candidate=C.UTF-8
+if ! locale -a 2>/dev/null | grep -q C.UTF-8; then
+       locale_candidate="$(locale -a | grep -i utf8 | head -n1)"
+       if [ -z "$locale_candidate" ]; then
+               locale_candidate=C
+       fi
+fi
+export LC_ALL="$locale_candidate"
+
+if uname | grep Darwin 1>/dev/null 2>&1; then
+       export LANG=en_US.UTF-8
+       export LC_ALL=en_US.UTF-8
+fi
+
 export NIT_TESTING=true
+# Use the pid as a collision prevention
+export NIT_TESTING_ID=$$
 export NIT_SRAND=0
 
+# Identify this as a tests.sh test to differentiate from a nitunit test
+export NIT_TESTING_TESTS_SH=true
+
 unset NIT_DIR
 
 # Get the first Java lib available
-shopt -s nullglob
-JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
+if which_java=$(which javac 2>/dev/null); then
 
-paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
-paths=($paths)
-JNI_LIB_PATH=`dirname ${paths[0]}`
-shopt -u nullglob
+       if sh -c "readlink -f ." 1>/dev/null 2>&1; then
+               READLINK="readlink -f"
+       else
+               # Darwin?
+               READLINK="readlink"
+       fi
+       JAVA_HOME=$(dirname $(dirname $($READLINK "$which_java")))
+
+       shopt -s nullglob
+       paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
+       if [ -n "$paths" ]; then
+               paths=($paths)
+               JNI_LIB_PATH=`dirname ${paths[0]}`
+       fi
+       shopt -u nullglob
+fi
 
 outdir="out"
 compdir="nit_compile"
 
+# User CPU time limit (in seconds)
+# Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
+usertimelimit=600 # 1 CPU minute
+
 # Real-time limit (in seconds)
 # Is used to avoid waiting or sleeping tests.
 # Require timeout or timelimit, or else is not used.
 realtimelimit=300 # 5 min
 
+# User limit for write files (in kilo-bytes)
+# Is used to avoid execution that loop and fill the hard drive. See ulimit -f
+# Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
+filelimit=100000 # ~100MB
+
+# Limit (in bytes) for generated .res file.
+# Larger ones are truncated and will fail tests
+# Is used to avoid processing huge crappy res file (diff, xml, etc)
+reslimit=100000 # ~100KB
+
 usage()
 {
        e=`basename "$0"`
@@ -75,6 +127,9 @@ saferun()
                        *) stop=true
                esac
        done
+       (
+       ulimit -f "$filelimit" 2> /dev/null
+       ulimit -t "$usertimelimit" 2> /dev/null
        if test -d "$1"; then
                find $1 | sort
        elif test -n "$TIME"; then
@@ -83,6 +138,7 @@ saferun()
                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
@@ -108,10 +164,12 @@ else
 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"
+if command time --quiet -f%e true 2>/dev/null; then
+       TIME="command time --quiet -f%e"
+elif command time -f%e true 2>/dev/null; then
+       TIME="command time -f%e"
+elif command gtime -f%e true 2>/dev/null; then
+       TIME="command gtime -f%e"
 else
        TIME=
 fi
@@ -123,6 +181,15 @@ else
        TIMESTAMP=
 fi
 
+# Detect a working hostname command
+if hostname --version 2>&1 | grep coreutils >/dev/null 2>&1; then
+       HOSTNAME="hostname"
+else
+       HOSTNAME="hostname -s"
+fi
+
+UNAME=`uname | sed s/-.*//`
+
 # $1 is the pattern of the test
 # $2 is the file to compare to
 # the result is:
@@ -136,7 +203,7 @@ function compare_to_result()
        local sav="$2"
        if [ ! -r "$sav" ]; then return 0; fi
        test "`cat -- "$sav"`" = "UNDEFINED" && return 1
-       diff -u -- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
+       diff -u --strip-trailing-cr -- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
        if [ "$?" == 0 ]; then
                return 1
        fi
@@ -144,7 +211,7 @@ function compare_to_result()
        sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
        grep '[Ee]rror' "$outdir/$pattern.res" >/dev/null && echo "Error" >> "$outdir/$pattern.res2"
        grep '[Ee]rror' "$sav" >/dev/null && echo "Error" >> "$outdir/$pattern.sav2"
-       diff -u "$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
+       diff -u --strip-trailing-cr "$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
        if [ "$?" == 0 ]; then
                return 2
        else
@@ -163,24 +230,33 @@ EOF
 function process_result()
 {
        # Result
-       pattern=$1
-       description=$2
-       pack=$3
-       SAV=""
-       NSAV=""
-       FIXME=""
-       NFIXME=""
-       SOSO=""
-       NSOSO=""
-       SOSOF=""
-       NSOSOF=""
-       OLD=""
-       LIST=""
-       FIRST=""
+       local pattern=$1
+       local description=$2
+       local pack=$3
+       local SAV=""
+       local NSAV=""
+       local FIXME=""
+       local NFIXME=""
+       local SOSO=""
+       local NSOSO=""
+       local SOSOF=""
+       local NSOSOF=""
+       local OLD=""
+       local LIST=""
+       local FIRST=""
+
+       # Truncate too big res file
+       local size=$(wc -c < "$outdir/$pattern.res")
+       if test -n "$reslimit" -a "$size" -gt "$reslimit"; then
+               # The most portable way to truncate a file is with Perl
+               perl -e "truncate \"$outdir/$pattern.res\", $reslimit;"
+               echo "***TRUNCATED***" >> "$outdir/$pattern.res"
+       fi
+
        echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$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
+               local sav=$savdir/fixme/$pattern.res
                compare_to_result "$pattern" "$sav"
                case "$?" in
                        0)
@@ -233,7 +309,7 @@ function process_result()
                esac
        done
        OLD=`echo "$OLD" | sed -e 's/   */ /g' -e 's/^ //' -e 's/ $//'`
-       grep 'NOT YET IMPLEMENTED' "$outdir/$pattern.res" >/dev/null
+       istodo  "$outdir/$pattern.res"
        NYI="$?"
        if [ -n "$SAV" ]; then
                if [ -n "$OLD" ]; then
@@ -331,40 +407,75 @@ need_skip()
                echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
                return 0
        fi
+       if test -n "GITLAB_CI" && echo "$1" | grep -f "gitlab_ci.skip" >/dev/null 2>&1; then
+               echo "=> $2: [skip gitlab ci]"
+               echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
+               return 0
+       fi
 
        # Skip by OS
-       os_skip_file=`uname`.skip
+       local os_skip_file=$UNAME.skip
        if test -e $os_skip_file && echo "$1" | grep -f "$os_skip_file" >/dev/null 2>&1; then
                echo "=> $2: [skip os]"
                echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
                return 0
        fi
+
+       # Skip by hostname
+       local host_skip_file=`$HOSTNAME`.skip
+       if test -e $host_skip_file && echo "$1" | grep -f "$host_skip_file" >/dev/null 2>&1; then
+               echo "=> $2: [skip hostname]"
+               echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
+               return 0
+       fi
        return 1
 }
 
 skip_exec()
 {
        test "$noskip" = true && return 1
-       if echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
-               echo -n "_ "
-               return 0
-       fi
+       for savdir in $savdirs .; do
+               local f="$savdir/exec.skip"
+               test -f "$f" || continue
+               if echo "$1" | grep -f "$f" >/dev/null 2>&1; then
+                       echo -n "_ no exec by $f; "
+                       return 0
+               fi
+       done
        return 1
 }
 
 skip_cc()
 {
        test "$noskip" = true && return 1
-       if echo "$1" | grep -f "cc.skip" >/dev/null 2>&1; then
-               return 0
-       fi
+       for savdir in $savdirs .; do
+               local f="$savdir/cc.skip"
+               test -f "$f" || continue
+               if echo "$1" | grep -f "$f" >/dev/null 2>&1; then
+                       return 0
+               fi
+       done
+       return 1
+}
+
+# Check that the resfile ($1) matches some magic strings in `todo` files.
+istodo()
+{
+       test "$no" = true && return 1
+       for savdir in $savdirs .; do
+               local f="$savdir/todo"
+               test -f "$f" || continue
+               if grep -f "$f" "$1" >/dev/null 2>&1; then
+                       return 0
+               fi
+       done
        return 1
 }
 
 find_nitc()
 {
-       name="$enginebinname"
-       recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
+       local name="$enginebinname"
+       local recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../contrib/nitin/bin/$name ../c_src/$name 2>/dev/null | head -1`
        if [[ "x$recent" == "x" ]]; then
                echo "Could not find binary for engine $engine, aborting"
                exit 1
@@ -398,32 +509,37 @@ while [ $stop = false ]; do
 done
 enginebinname=$engine
 isinterpret=
+isinteractive=
 case $engine in
        nitc|nitg)
-               engine=nitg-s;
+               engine=nitcs;
                enginebinname=nitc;
                OPT="--separate $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-common/"
+               savdirs="sav/nitc-common/"
                ;;
        nitcs|nitg-s)
+               engine=nitcs;
                enginebinname=nitc;
                OPT="--separate $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-common/"
+               savdirs="sav/nitc-common/"
                ;;
        nitce|nitg-e)
+               engine=nitce;
                enginebinname=nitc;
                OPT="--erasure $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-common/"
+               savdirs="sav/nitc-common/"
                ;;
        nitcsg|nitg-sg)
+               engine=nitcsg;
                enginebinname=nitc;
                OPT="--semi-global $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-common/"
+               savdirs="sav/nitc-common/"
                ;;
        nitcg|nitg-g)
+               engine=nitcg;
                enginebinname=nitc;
                OPT="--global $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-common/"
+               savdirs="sav/nitc-common/"
                ;;
        nit)
                engine=niti
@@ -439,14 +555,20 @@ case $engine in
                OPT="--vm $OPT"
                savdirs="sav/niti/"
                ;;
+       nitin)
+               enginebinname=nitin
+               isinteractive=true
+               ;;
+       nitj)
+               engine=nitj;
+               OPT="--compile-dir $compdir --ant"
+               enginebinname=nitj;
+               savdirs="sav/nitc-common/"
+               ;;
        emscripten)
                enginebinname=nitc
                OPT="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
-               savdirs="sav/nitg-sg/"
-               ;;
-       nitc)
-               echo "disabled engine $engine"
-               exit 0
+               savdirs="sav/nitcsg/"
                ;;
        *)
                echo "unknown engine $engine"
@@ -454,7 +576,8 @@ case $engine in
                ;;
 esac
 
-savdirs="sav/$engine $savdirs sav/"
+savdirs="sav/`$HOSTNAME` sav/$UNAME sav/$engine $savdirs sav/"
+test -n "$GITLAB_CI" && savdirs="sav/gitlab_ci $savdirs"
 
 # The default nitc compiler
 [ -z "$NITC" ] && find_nitc
@@ -513,9 +636,9 @@ for ii in "$@"; do
        # Sould we skip the file for this engine?
        need_skip "$f" "$f" "$pack" && continue
 
-       tmp=${ii/../AA}
-       if [ "x$tmp" = "x$ii" ]; then
-               includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
+       local_tmp=${ii/../AA}
+       if [ "x$local_tmp" = "x$ii" ]; then
+               includes="-I . -I ../lib/core -I ../lib/core/collection -I alt"
        else
                includes="-I alt"
        fi
@@ -551,6 +674,15 @@ END
                        > "$ff.compile.log"
                        ERR=0
                        echo 0.0 > "$ff.time.out"
+               elif [ -n "$isinteractive" ]; then
+                       cat > "$ff.bin" <<END
+exec $NITC --no-color --no-prompt --source-name $(printf '%q' "$i") $OPT $includes < $(printf '%q' "$i") "\$@"
+END
+                       chmod +x "$ff.bin"
+                       > "$ff.cmp.err"
+                       > "$ff.compile.log"
+                       ERR=0
+                       echo 0.0 > "$ff.time.out"
                else
                        if skip_cc "$bf"; then
                                nocc="--no-cc"
@@ -604,7 +736,7 @@ END
                                echo ""
                                echo "NIT_NO_STACK=1 $ff.bin" $args
                        fi
-                       NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
+                       NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH WRITE="$ff.write" \
                                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"
@@ -616,7 +748,7 @@ END
                        if [ -f "$ff.write" ]; then
                                cat -- "$ff.write" >> "$ff.res"
                        elif [ -d "$ff.write" ]; then
-                               LANG=C /bin/ls -F "$ff.write" >> "$ff.res"
+                               /bin/ls -F "$ff.write" >> "$ff.res"
                        fi
                        cp -- "$ff.res"  "$ff.res2"
                        cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
@@ -658,7 +790,7 @@ END
                                        if [ -f "$fff.write" ]; then
                                                cat -- "$fff.write" >> "$fff.res"
                                        elif [ -d "$fff.write" ]; then
-                                               LANG=C /bin/ls -F -- "$fff.write" >> "$fff.res"
+                                               /bin/ls -F -- "$fff.write" >> "$fff.res"
                                        fi
                                        if [ -s "$fff.err" ]; then
                                                cp -- "$fff.res"  "$fff.res2"
@@ -710,6 +842,10 @@ fi
 
 echo >>$xml "</testsuite></testsuites>"
 
+if type junit2html >/dev/null; then
+       junit2html "$xml"
+fi
+
 if [ -n "$nok" ]; then
        exit 1
 else