2 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # This shell script compile, run and verify Nit program files
20 # Set lang do default to avoid failed tests because of locale
23 if uname |
grep Darwin
1>/dev
/null
2>&1; then
24 export LANG
=en_US.UTF
-8
25 export LC_ALL
=en_US.UTF
-8
28 export NIT_TESTING
=true
29 # Use the pid as a collision prevention
30 export NIT_TESTING_ID
=$$
35 # Get the first Java lib available
36 if which_java
=$
(which javac
2>/dev
/null
); then
37 JAVA_HOME
=$
(dirname $
(dirname $
(readlink
-f
"$which_java")))
40 paths
=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
42 JNI_LIB_PATH
=`dirname ${paths[0]}`
49 # User CPU time limit (in seconds)
50 # Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
51 usertimelimit
=600 # 1 CPU minute
53 # Real-time limit (in seconds)
54 # Is used to avoid waiting or sleeping tests.
55 # Require timeout or timelimit, or else is not used.
56 realtimelimit
=300 # 5 min
58 # User limit for write files (in kilo-bytes)
59 # Is used to avoid execution that loop and fill the hard drive. See ulimit -f
60 # Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
61 filelimit
=100000 # ~100MB
63 # Limit (in bytes) for generated .res file.
64 # Larger ones are truncated and will fail tests
65 # Is used to avoid processing huge crappy res file (diff, xml, etc)
66 reslimit
=100000 # ~100KB
72 Usage
: $e [options
] modulenames
73 -o option Pass option to the engine
74 -v Verbose
(show tests steps
)
76 --engine Use a specific engine
(default
=nitc
)
77 --noskip Do not skip a
test even
if the .skip
file matches
78 --outdir Use a specific output folder
(default
=out
/)
79 --compdir Use a specific temporary compilation folder
(default
=$compdir)
80 --node Run as a node
in parallel
, will not output context information
81 --autosav Copy the .res files directly
in the sav folder overriding existing .res files
85 # Run a command with a timeout and a time count.
87 # -o file write the user time into file (REQUIRED). see `-o` in `man time`
88 # -a append the time to the file (instead of overwriting it). see `-a` in `man time`
94 while [ $stop = false
]; do
96 -o
) o
="$2"; shift; shift;;
102 ulimit -f
"$filelimit" 2> /dev
/null
103 ulimit -t
"$usertimelimit" 2> /dev
/null
104 if test -d
"$1"; then
106 elif test -n
"$TIME"; then
107 $TIME -o
"$o" $a $TIMEOUT "$@"
109 if test -n
"$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
115 # Output a timestamp attribute for XML, or an empty line
118 if test -n
"$TIMESTAMP"; then
119 echo "timestamp='`$TIMESTAMP`'"
126 # Get platform specific commands ##########################
128 # Detect a working timeout
129 if sh
-c
"timelimit echo" 1>/dev
/null
2>&1; then
130 TIMEOUT
="timelimit -t $realtimelimit"
131 elif sh
-c
"timeout 1 echo" 1>/dev
/null
2>&1; then
132 TIMEOUT
="timeout ${realtimelimit}s"
134 echo "No timelimit or timeout command detected. Tests may hang :("
137 # Detect a working time command
138 if command time --quiet
-f
%e true
2>/dev
/null
; then
139 TIME
="command time --quiet -f%e"
140 elif command time -f
%e true
2>/dev
/null
; then
141 TIME
="command time -f%e"
142 elif command gtime
-f
%e true
2>/dev
/null
; then
143 TIME
="command gtime -f%e"
148 # Detect a working date command
149 if date -Iseconds
>/dev
/null
2>&1; then
150 TIMESTAMP
="date -Iseconds"
155 # Detect a working hostname command
156 if hostname
--version
2>&1 |
grep coreutils
>/dev
/null
2>&1; then
159 HOSTNAME
="hostname -s"
162 UNAME
=`uname | sed s/-.*//`
164 # $1 is the pattern of the test
165 # $2 is the file to compare to
167 # 0: if the file to compare to do not exists
168 # 1: if the file match
169 # 2: if the file match with soso
170 # 3: if the file do not match
171 function compare_to_result
()
175 if [ ! -r
"$sav" ]; then return 0; fi
176 test "`cat -- "$sav"`" = "UNDEFINED" && return 1
177 diff -u
--strip
-trailing
-cr
-- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
178 if [ "$?" == 0 ]; then
181 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
182 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
183 grep '[Ee]rror' "$outdir/$pattern.res" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.res2"
184 grep '[Ee]rror' "$sav" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.sav2"
185 diff -u
--strip
-trailing
-cr
"$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
186 if [ "$?" == 0 ]; then
195 sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'<<EOF
200 # As argument: the pattern used for the file
201 function process_result
()
219 # Truncate too big res file
220 local size
=$
(wc -c
< "$outdir/$pattern.res")
221 if test -n
"$reslimit" -a
"$size" -gt
"$reslimit"; then
222 # The most portable way to truncate a file is with Perl
223 perl
-e
"truncate \"$outdir/$pattern.res\", $reslimit;"
224 echo "***TRUNCATED***" >> "$outdir/$pattern.res"
227 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.
time.out
"`' `timestamp`>"
228 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
229 for savdir
in $savdirs; do
230 local sav
=$savdir/fixme
/$pattern.res
231 compare_to_result
"$pattern" "$sav"
241 if [ -z
"$FIRST" ]; then
248 if [ -z
"$FIRST" ]; then
256 sav
=$savdir/$pattern.res
257 compare_to_result
"$pattern" "$sav"
267 if [ -z
"$FIRST" ]; then
274 if [ -z
"$FIRST" ]; then
282 OLD
=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
283 istodo
"$outdir/$pattern.res"
285 if [ -n
"$SAV" ]; then
286 if [ -n
"$OLD" ]; then
287 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
288 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
289 remains
="$remains $OLD"
290 test "$autosav" = "true" && rm "$OLD"
292 echo "[ok] $outdir/$pattern.res $SAV"
295 elif [ -n
"$FIXME" ]; then
296 if [ -n
"$OLD" ]; then
297 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
298 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
299 remains
="$remains $OLD"
300 test "$autosav" = "true" && rm "$OLD"
302 echo "[fixme] $outdir/$pattern.res $FIXME"
303 echo >>$xml "<skipped/>"
305 todos
="$todos $pattern"
306 elif [ "x$NYI" = "x0" ]; then
307 echo "[todo] $outdir/$pattern.res -> not yet implemented"
308 echo >>$xml "<skipped/>"
309 todos
="$todos $pattern"
310 elif [ -n
"$SOSO" ]; then
311 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
312 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
313 echo >>$xml "<system-out><![CDATA["
314 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
315 echo >>$xml "]]></system-out>"
317 echo "$ii" >> "$ERRLIST"
318 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$SOSO"
319 elif [ -n
"$SOSOF" ]; then
320 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
321 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
322 echo >>$xml "<system-out><![CDATA["
323 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
324 echo >>$xml "]]></system-out>"
326 echo "$ii" >> "$ERRLIST"
327 test "$autosav" = "true" && cp "$outdir/$pattern.res" && "$SOSO"
328 elif [ -n
"$NSAV" ]; then
329 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
330 echo >>$xml "<error message='`xmlesc "fail
$outdir/$pattern.res
$NSAV"`'/>"
331 echo >>$xml "<system-out><![CDATA["
332 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
333 echo >>$xml "]]></system-out>"
335 echo "$ii" >> "$ERRLIST"
336 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NSAV"
337 elif [ -n
"$NFIXME" ]; then
338 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
339 echo >>$xml "<error message='`xmlesc "changed
$outdir/$pattern.res
$NFIXME"`'/>"
340 echo >>$xml "<system-out><![CDATA["
341 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
342 echo >>$xml "]]></system-out>"
344 echo "$ii" >> "$ERRLIST"
345 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NFIXME"
346 elif [ -s
"$outdir/$pattern.res" ]; then
347 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
348 echo >>$xml "<error message='no sav and not empty'/>"
349 echo >>$xml "<system-out><![CDATA["
350 cat -v
>>$xml -- "$outdir/$pattern.res"
351 echo >>$xml "]]></system-out>"
353 echo "$ii" >> "$ERRLIST"
354 test "$autosav" = "true" && cp "$outdir/$pattern.res" "sav/"
356 # no sav but empty res
357 echo "[0k] $outdir/$pattern.res is empty"
360 if test -s
"$outdir/$pattern.cmp.err"; then
361 echo >>$xml "<system-err><![CDATA["
362 cat -v
>>$xml -- "$outdir/$pattern.cmp.err"
363 echo >>$xml "]]></system-err>"
365 echo >>$xml "</testcase>"
370 test "$noskip" = true
&& return 1
371 if echo "$1" |
grep -f
"$engine.skip" >/dev
/null
2>&1; then
373 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
376 if test -n
"$isinterpret" && echo "$1" |
grep -f
"exec.skip" >/dev
/null
2>&1; then
377 echo "=> $2: [skip exec]"
378 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
383 local os_skip_file
=$UNAME.skip
384 if test -e
$os_skip_file && echo "$1" |
grep -f
"$os_skip_file" >/dev
/null
2>&1; then
385 echo "=> $2: [skip os]"
386 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
391 local host_skip_file
=`$HOSTNAME`.skip
392 if test -e
$host_skip_file && echo "$1" |
grep -f
"$host_skip_file" >/dev
/null
2>&1; then
393 echo "=> $2: [skip hostname]"
394 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
402 test "$noskip" = true
&& return 1
403 for savdir
in $savdirs .
; do
404 local f
="$savdir/exec.skip"
405 test -f
"$f" ||
continue
406 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
407 echo -n
"_ no exec by $f; "
416 test "$noskip" = true
&& return 1
417 for savdir
in $savdirs .
; do
418 local f
="$savdir/cc.skip"
419 test -f
"$f" ||
continue
420 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
427 # Check that the resfile ($1) matches some magic strings in `todo` files.
430 test "$no" = true
&& return 1
431 for savdir
in $savdirs .
; do
432 local f
="$savdir/todo"
433 test -f
"$f" ||
continue
434 if grep -f
"$f" "$1" >/dev
/null
2>&1; then
443 local name
="$enginebinname"
444 local recent
=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../contrib/nitin/bin/$name ../c_src/$name 2>/dev/null | head -1`
445 if [[ "x$recent" == "x" ]]; then
446 echo "Could not find binary for engine $engine, aborting"
449 if [ "x$isnode" = "xfalse" ]; then
450 echo "Found binary for engine $engine: $recent $OPT"
462 while [ $stop = false
]; do
464 -o
) OPT
="$OPT $2"; shift; shift;;
465 -v
) verbose
=true
; shift;;
467 --engine
) engine
="$2"; shift; shift;;
468 --noskip
) noskip
=true
; shift;;
469 --outdir
) outdir
="$2"; shift; shift;;
470 --compdir
) compdir
="$2"; shift; shift;;
471 --node
) isnode
=true
; shift;;
472 --autosav
) autosav
=true
; shift;;
476 enginebinname
=$engine
483 OPT
="--separate $OPT --compile-dir $compdir"
484 savdirs
="sav/nitc-common/"
489 OPT
="--separate $OPT --compile-dir $compdir"
490 savdirs
="sav/nitc-common/"
495 OPT
="--erasure $OPT --compile-dir $compdir"
496 savdirs
="sav/nitc-common/"
501 OPT
="--semi-global $OPT --compile-dir $compdir"
502 savdirs
="sav/nitc-common/"
507 OPT
="--global $OPT --compile-dir $compdir"
508 savdirs
="sav/nitc-common/"
530 OPT
="--compile-dir $compdir --ant"
532 savdirs
="sav/nitc-common/"
536 OPT
="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
537 savdirs
="sav/nitcsg/"
540 echo "unknown engine $engine"
545 savdirs
="sav/`$HOSTNAME` sav/$UNAME sav/$engine $savdirs sav/"
547 # The default nitc compiler
548 [ -z
"$NITC" ] && find_nitc
550 # Set NIT_DIR if needed
551 [ -z
"$NIT_DIR" ] && export NIT_DIR
=..
553 # Mark to distinguish files among tests
561 # CLEAN the out directory
562 rm -rf
"$outdir/" 2>/dev
/null
563 mkdir
"$outdir" 2>/dev
/null
565 # File where error tests are outputed
566 # Old ERRLIST is backuped
567 ERRLIST
=${ERRLIST:-errlist}
568 ERRLIST_TARGET
=$ERRLIST
570 # Initiate new ERRLIST
571 if [ "x$ERRLIST" = "x" ]; then
582 if [ "x$XMLDIR" = "x" ]; then
583 xml
="tests-$engine.xml"
585 sum=`echo $@ | md5sum | cut -f1 -d " "`
586 xml
="$XMLDIR/tests-$engine-$sum.xml"
590 echo >$xml "<testsuites><testsuite>"
593 if [ ! -f
"$ii" ]; then
594 echo "File '$ii' does not exist."
597 f
=`basename -- "$ii" .nit`
599 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|'`
601 # Sould we skip the file for this engine?
602 need_skip
"$f" "$f" "$pack" && continue
604 local_tmp
=${ii/../AA}
605 if [ "x$local_tmp" = "x$ii" ]; then
606 includes
="-I . -I ../lib/core -I ../lib/core/collection -I alt"
611 for i
in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
612 bf
=`basename -- "$i" .nit`
615 # Sould we skip the alternative for this engine?
616 need_skip
"$bf" "$bf" "$pack" && continue
620 if [ -f
"$f.inputs" ]; then
622 export MNIT_READ_INPUT
="$f.inputs"
625 export MNIT_READ_INPUT
=/dev
/null
629 if [ "$engine" = "emscripten" ]; then
633 if [ -n
"$isinterpret" ]; then
634 cat > "$ff.bin" <<END
635 exec $NITC --no
-color
$OPT $includes -- $
(printf '%q' "$i") "\$@"
641 echo 0.0 > "$ff.time.out"
642 elif [ -n
"$isinteractive" ]; then
643 cat > "$ff.bin" <<END
644 exec $NITC --no
-color
--no
-prompt
--source-name $
(printf '%q' "$i") $OPT $includes < $
(printf '%q' "$i") "\$@"
650 echo 0.0 > "$ff.time.out"
652 if skip_cc
"$bf"; then
658 if [ "x$verbose" = "xtrue" ]; then
660 echo $NITC --no
-color
$OPT -o
"$ffout" "$includes" $nocc "$i"
662 NIT_NO_STACK
=1 JNI_LIB_PATH
=$JNI_LIB_PATH JAVA_HOME
=$JAVA_HOME \
663 saferun
-o
"$ff.time.out" $NITC --no
-color
$OPT -o
"$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
665 if [ "x$verbose" = "xtrue" ]; then
666 cat -- "$ff.compile.log"
667 cat >&2 -- "$ff.cmp.err"
670 rm -r
"$compdir" 2>/dev
/null
672 if [ "$engine" = "emscripten" ]; then
673 echo > "$ff.bin" "nodejs $ffout \"\$@\""
675 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev
/null
; then
676 echo " [skip] do no not imports kernel"
677 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
681 if [ "$ERR" != 0 ]; then
683 cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
684 process_result
"$bf" "$bf" "$pack"
685 elif [ -n
"$nocc" ]; then
689 process_result
"$bf" "$bf" "$pack"
690 elif [ -x
"$ff.bin" ]; then
691 if skip_exec
"$bf"; then
694 process_result
"$bf" "$bf" "$pack"
700 if [ "x$verbose" = "xtrue" ]; then
702 echo "NIT_NO_STACK=1 $ff.bin" $args
704 NIT_NO_STACK
=1 LD_LIBRARY_PATH
=$JNI_LIB_PATH WRITE
="$ff.write" \
705 saferun
-a
-o
"$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
706 mv "$ff.time.out" "$ff.times.out"
707 awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
709 if [ "x$verbose" = "xtrue" ]; then
713 if [ -f
"$ff.write" ]; then
714 cat -- "$ff.write" >> "$ff.res"
715 elif [ -d
"$ff.write" ]; then
716 /bin
/ls -F
"$ff.write" >> "$ff.res"
718 cp -- "$ff.res" "$ff.res2"
719 cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
720 process_result
"$bf" "$bf" "$pack"
722 if [ -f
"$f.args" ]; then
730 name
="$bf args $cptr"
732 # Sould we skip the input for this engine?
733 need_skip
"$bff" " $name" "$pack" && continue
735 # use a specific inputs file, if required
736 if [ -f
"$bff.inputs" ]; then
737 ffinputs
="$bff.inputs"
742 rm -rf
"$fff.res" "$fff.err" "$fff.write" 2> /dev
/null
743 if [ "x$verbose" = "xtrue" ]; then
745 echo "NIT_NO_STACK=1 $ff.bin" $args
748 echo "$ff.bin $args" > "$fff.bin"
750 WRITE
="$fff.write" saferun
-o
"$fff.time.out" sh
-c
"NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
751 if [ "x$verbose" = "xtrue" ]; then
753 cat >&2 -- "$fff.err"
755 if [ -f
"$fff.write" ]; then
756 cat -- "$fff.write" >> "$fff.res"
757 elif [ -d
"$fff.write" ]; then
758 /bin
/ls -F
-- "$fff.write" >> "$fff.res"
760 if [ -s
"$fff.err" ]; then
761 cp -- "$fff.res" "$fff.res2"
762 cat -- "$fff.err" "$fff.res2" > "$fff.res"
764 process_result
"$bff" " $name" "$pack"
767 elif [ -f
"$ff.bin" ]; then
768 #Not executable (platform?)"
770 process_result
"$bf" "$bf" "$pack"
773 cat -- "$ff.cmp.err" > "$ff.res"
774 echo "Compilation error" > "$ff.res"
775 process_result
"$bf" "$bf" "$pack"
780 if [ "x$isnode" = "xfalse" ]; then
781 echo "engine: $engine ($enginebinname $OPT)"
782 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
784 if [ -n
"$nok" ]; then
786 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
788 if [ -n
"$nos" ]; then
791 if [ -n
"$todos" ]; then
792 echo "todo/fixme: $todos"
794 if [ -n
"$remains" ]; then
795 echo "sav that remains: $remains"
800 if [ "x$ERRLIST" != "x" ]; then
801 if [ -f
"$ERRLIST_TARGET" ]; then
802 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
804 uniq $ERRLIST > $ERRLIST_TARGET
808 echo >>$xml "</testsuite></testsuites>"
810 if [ -n
"$nok" ]; then