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
38 if sh
-c
"readlink -f ." 1>/dev
/null
2>&1; then
39 READLINK
="readlink -f"
44 JAVA_HOME
=$
(dirname $
(dirname $
($READLINK "$which_java")))
47 paths
=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
48 if [ -n
"$paths" ]; then
50 JNI_LIB_PATH
=`dirname ${paths[0]}`
58 # User CPU time limit (in seconds)
59 # Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
60 usertimelimit
=600 # 1 CPU minute
62 # Real-time limit (in seconds)
63 # Is used to avoid waiting or sleeping tests.
64 # Require timeout or timelimit, or else is not used.
65 realtimelimit
=300 # 5 min
67 # User limit for write files (in kilo-bytes)
68 # Is used to avoid execution that loop and fill the hard drive. See ulimit -f
69 # Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
70 filelimit
=100000 # ~100MB
72 # Limit (in bytes) for generated .res file.
73 # Larger ones are truncated and will fail tests
74 # Is used to avoid processing huge crappy res file (diff, xml, etc)
75 reslimit
=100000 # ~100KB
81 Usage
: $e [options
] modulenames
82 -o option Pass option to the engine
83 -v Verbose
(show tests steps
)
85 --engine Use a specific engine
(default
=nitc
)
86 --noskip Do not skip a
test even
if the .skip
file matches
87 --outdir Use a specific output folder
(default
=out
/)
88 --compdir Use a specific temporary compilation folder
(default
=$compdir)
89 --node Run as a node
in parallel
, will not output context information
90 --autosav Copy the .res files directly
in the sav folder overriding existing .res files
94 # Run a command with a timeout and a time count.
96 # -o file write the user time into file (REQUIRED). see `-o` in `man time`
97 # -a append the time to the file (instead of overwriting it). see `-a` in `man time`
103 while [ $stop = false
]; do
105 -o
) o
="$2"; shift; shift;;
111 ulimit -f
"$filelimit" 2> /dev
/null
112 ulimit -t
"$usertimelimit" 2> /dev
/null
113 if test -d
"$1"; then
115 elif test -n
"$TIME"; then
116 $TIME -o
"$o" $a $TIMEOUT "$@"
118 if test -n
"$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
124 # Output a timestamp attribute for XML, or an empty line
127 if test -n
"$TIMESTAMP"; then
128 echo "timestamp='`$TIMESTAMP`'"
135 # Get platform specific commands ##########################
137 # Detect a working timeout
138 if sh
-c
"timelimit echo" 1>/dev
/null
2>&1; then
139 TIMEOUT
="timelimit -t $realtimelimit"
140 elif sh
-c
"timeout 1 echo" 1>/dev
/null
2>&1; then
141 TIMEOUT
="timeout ${realtimelimit}s"
143 echo "No timelimit or timeout command detected. Tests may hang :("
146 # Detect a working time command
147 if command time --quiet
-f
%e true
2>/dev
/null
; then
148 TIME
="command time --quiet -f%e"
149 elif command time -f
%e true
2>/dev
/null
; then
150 TIME
="command time -f%e"
151 elif command gtime
-f
%e true
2>/dev
/null
; then
152 TIME
="command gtime -f%e"
157 # Detect a working date command
158 if date -Iseconds
>/dev
/null
2>&1; then
159 TIMESTAMP
="date -Iseconds"
164 # Detect a working hostname command
165 if hostname
--version
2>&1 |
grep coreutils
>/dev
/null
2>&1; then
168 HOSTNAME
="hostname -s"
171 UNAME
=`uname | sed s/-.*//`
173 # $1 is the pattern of the test
174 # $2 is the file to compare to
176 # 0: if the file to compare to do not exists
177 # 1: if the file match
178 # 2: if the file match with soso
179 # 3: if the file do not match
180 function compare_to_result
()
184 if [ ! -r
"$sav" ]; then return 0; fi
185 test "`cat -- "$sav"`" = "UNDEFINED" && return 1
186 diff -u
--strip
-trailing
-cr
-- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
187 if [ "$?" == 0 ]; then
190 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
191 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
192 grep '[Ee]rror' "$outdir/$pattern.res" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.res2"
193 grep '[Ee]rror' "$sav" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.sav2"
194 diff -u
--strip
-trailing
-cr
"$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
195 if [ "$?" == 0 ]; then
204 sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'<<EOF
209 # As argument: the pattern used for the file
210 function process_result
()
228 # Truncate too big res file
229 local size
=$
(wc -c
< "$outdir/$pattern.res")
230 if test -n
"$reslimit" -a
"$size" -gt
"$reslimit"; then
231 # The most portable way to truncate a file is with Perl
232 perl
-e
"truncate \"$outdir/$pattern.res\", $reslimit;"
233 echo "***TRUNCATED***" >> "$outdir/$pattern.res"
236 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.
time.out
"`' `timestamp`>"
237 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
238 for savdir
in $savdirs; do
239 local sav
=$savdir/fixme
/$pattern.res
240 compare_to_result
"$pattern" "$sav"
250 if [ -z
"$FIRST" ]; then
257 if [ -z
"$FIRST" ]; then
265 sav
=$savdir/$pattern.res
266 compare_to_result
"$pattern" "$sav"
276 if [ -z
"$FIRST" ]; then
283 if [ -z
"$FIRST" ]; then
291 OLD
=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
292 istodo
"$outdir/$pattern.res"
294 if [ -n
"$SAV" ]; then
295 if [ -n
"$OLD" ]; then
296 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
297 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
298 remains
="$remains $OLD"
299 test "$autosav" = "true" && rm "$OLD"
301 echo "[ok] $outdir/$pattern.res $SAV"
304 elif [ -n
"$FIXME" ]; then
305 if [ -n
"$OLD" ]; then
306 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
307 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
308 remains
="$remains $OLD"
309 test "$autosav" = "true" && rm "$OLD"
311 echo "[fixme] $outdir/$pattern.res $FIXME"
312 echo >>$xml "<skipped/>"
314 todos
="$todos $pattern"
315 elif [ "x$NYI" = "x0" ]; then
316 echo "[todo] $outdir/$pattern.res -> not yet implemented"
317 echo >>$xml "<skipped/>"
318 todos
="$todos $pattern"
319 elif [ -n
"$SOSO" ]; then
320 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
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
"$SOSOF" ]; then
329 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
330 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
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" && "$SOSO"
337 elif [ -n
"$NSAV" ]; then
338 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
339 echo >>$xml "<error message='`xmlesc "fail
$outdir/$pattern.res
$NSAV"`'/>"
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" "$NSAV"
346 elif [ -n
"$NFIXME" ]; then
347 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
348 echo >>$xml "<error message='`xmlesc "changed
$outdir/$pattern.res
$NFIXME"`'/>"
349 echo >>$xml "<system-out><![CDATA["
350 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
351 echo >>$xml "]]></system-out>"
353 echo "$ii" >> "$ERRLIST"
354 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NFIXME"
355 elif [ -s
"$outdir/$pattern.res" ]; then
356 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
357 echo >>$xml "<error message='no sav and not empty'/>"
358 echo >>$xml "<system-out><![CDATA["
359 cat -v
>>$xml -- "$outdir/$pattern.res"
360 echo >>$xml "]]></system-out>"
362 echo "$ii" >> "$ERRLIST"
363 test "$autosav" = "true" && cp "$outdir/$pattern.res" "sav/"
365 # no sav but empty res
366 echo "[0k] $outdir/$pattern.res is empty"
369 if test -s
"$outdir/$pattern.cmp.err"; then
370 echo >>$xml "<system-err><![CDATA["
371 cat -v
>>$xml -- "$outdir/$pattern.cmp.err"
372 echo >>$xml "]]></system-err>"
374 echo >>$xml "</testcase>"
379 test "$noskip" = true
&& return 1
380 if echo "$1" |
grep -f
"$engine.skip" >/dev
/null
2>&1; then
382 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
385 if test -n
"$isinterpret" && echo "$1" |
grep -f
"exec.skip" >/dev
/null
2>&1; then
386 echo "=> $2: [skip exec]"
387 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
392 local os_skip_file
=$UNAME.skip
393 if test -e
$os_skip_file && echo "$1" |
grep -f
"$os_skip_file" >/dev
/null
2>&1; then
394 echo "=> $2: [skip os]"
395 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
400 local host_skip_file
=`$HOSTNAME`.skip
401 if test -e
$host_skip_file && echo "$1" |
grep -f
"$host_skip_file" >/dev
/null
2>&1; then
402 echo "=> $2: [skip hostname]"
403 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
411 test "$noskip" = true
&& return 1
412 for savdir
in $savdirs .
; do
413 local f
="$savdir/exec.skip"
414 test -f
"$f" ||
continue
415 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
416 echo -n
"_ no exec by $f; "
425 test "$noskip" = true
&& return 1
426 for savdir
in $savdirs .
; do
427 local f
="$savdir/cc.skip"
428 test -f
"$f" ||
continue
429 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
436 # Check that the resfile ($1) matches some magic strings in `todo` files.
439 test "$no" = true
&& return 1
440 for savdir
in $savdirs .
; do
441 local f
="$savdir/todo"
442 test -f
"$f" ||
continue
443 if grep -f
"$f" "$1" >/dev
/null
2>&1; then
452 local name
="$enginebinname"
453 local recent
=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../contrib/nitin/bin/$name ../c_src/$name 2>/dev/null | head -1`
454 if [[ "x$recent" == "x" ]]; then
455 echo "Could not find binary for engine $engine, aborting"
458 if [ "x$isnode" = "xfalse" ]; then
459 echo "Found binary for engine $engine: $recent $OPT"
471 while [ $stop = false
]; do
473 -o
) OPT
="$OPT $2"; shift; shift;;
474 -v
) verbose
=true
; shift;;
476 --engine
) engine
="$2"; shift; shift;;
477 --noskip
) noskip
=true
; shift;;
478 --outdir
) outdir
="$2"; shift; shift;;
479 --compdir
) compdir
="$2"; shift; shift;;
480 --node
) isnode
=true
; shift;;
481 --autosav
) autosav
=true
; shift;;
485 enginebinname
=$engine
492 OPT
="--separate $OPT --compile-dir $compdir"
493 savdirs
="sav/nitc-common/"
498 OPT
="--separate $OPT --compile-dir $compdir"
499 savdirs
="sav/nitc-common/"
504 OPT
="--erasure $OPT --compile-dir $compdir"
505 savdirs
="sav/nitc-common/"
510 OPT
="--semi-global $OPT --compile-dir $compdir"
511 savdirs
="sav/nitc-common/"
516 OPT
="--global $OPT --compile-dir $compdir"
517 savdirs
="sav/nitc-common/"
539 OPT
="--compile-dir $compdir --ant"
541 savdirs
="sav/nitc-common/"
545 OPT
="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
546 savdirs
="sav/nitcsg/"
549 echo "unknown engine $engine"
554 savdirs
="sav/`$HOSTNAME` sav/$UNAME sav/$engine $savdirs sav/"
556 # The default nitc compiler
557 [ -z
"$NITC" ] && find_nitc
559 # Set NIT_DIR if needed
560 [ -z
"$NIT_DIR" ] && export NIT_DIR
=..
562 # Mark to distinguish files among tests
570 # CLEAN the out directory
571 rm -rf
"$outdir/" 2>/dev
/null
572 mkdir
"$outdir" 2>/dev
/null
574 # File where error tests are outputed
575 # Old ERRLIST is backuped
576 ERRLIST
=${ERRLIST:-errlist}
577 ERRLIST_TARGET
=$ERRLIST
579 # Initiate new ERRLIST
580 if [ "x$ERRLIST" = "x" ]; then
591 if [ "x$XMLDIR" = "x" ]; then
592 xml
="tests-$engine.xml"
594 sum=`echo $@ | md5sum | cut -f1 -d " "`
595 xml
="$XMLDIR/tests-$engine-$sum.xml"
599 echo >$xml "<testsuites><testsuite>"
602 if [ ! -f
"$ii" ]; then
603 echo "File '$ii' does not exist."
606 f
=`basename -- "$ii" .nit`
608 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|'`
610 # Sould we skip the file for this engine?
611 need_skip
"$f" "$f" "$pack" && continue
613 local_tmp
=${ii/../AA}
614 if [ "x$local_tmp" = "x$ii" ]; then
615 includes
="-I . -I ../lib/core -I ../lib/core/collection -I alt"
620 for i
in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
621 bf
=`basename -- "$i" .nit`
624 # Sould we skip the alternative for this engine?
625 need_skip
"$bf" "$bf" "$pack" && continue
629 if [ -f
"$f.inputs" ]; then
631 export MNIT_READ_INPUT
="$f.inputs"
634 export MNIT_READ_INPUT
=/dev
/null
638 if [ "$engine" = "emscripten" ]; then
642 if [ -n
"$isinterpret" ]; then
643 cat > "$ff.bin" <<END
644 exec $NITC --no
-color
$OPT $includes -- $
(printf '%q' "$i") "\$@"
650 echo 0.0 > "$ff.time.out"
651 elif [ -n
"$isinteractive" ]; then
652 cat > "$ff.bin" <<END
653 exec $NITC --no
-color
--no
-prompt
--source-name $
(printf '%q' "$i") $OPT $includes < $
(printf '%q' "$i") "\$@"
659 echo 0.0 > "$ff.time.out"
661 if skip_cc
"$bf"; then
667 if [ "x$verbose" = "xtrue" ]; then
669 echo $NITC --no
-color
$OPT -o
"$ffout" "$includes" $nocc "$i"
671 NIT_NO_STACK
=1 JNI_LIB_PATH
=$JNI_LIB_PATH JAVA_HOME
=$JAVA_HOME \
672 saferun
-o
"$ff.time.out" $NITC --no
-color
$OPT -o
"$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
674 if [ "x$verbose" = "xtrue" ]; then
675 cat -- "$ff.compile.log"
676 cat >&2 -- "$ff.cmp.err"
679 rm -r
"$compdir" 2>/dev
/null
681 if [ "$engine" = "emscripten" ]; then
682 echo > "$ff.bin" "nodejs $ffout \"\$@\""
684 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev
/null
; then
685 echo " [skip] do no not imports kernel"
686 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
690 if [ "$ERR" != 0 ]; then
692 cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
693 process_result
"$bf" "$bf" "$pack"
694 elif [ -n
"$nocc" ]; then
698 process_result
"$bf" "$bf" "$pack"
699 elif [ -x
"$ff.bin" ]; then
700 if skip_exec
"$bf"; then
703 process_result
"$bf" "$bf" "$pack"
709 if [ "x$verbose" = "xtrue" ]; then
711 echo "NIT_NO_STACK=1 $ff.bin" $args
713 NIT_NO_STACK
=1 LD_LIBRARY_PATH
=$JNI_LIB_PATH WRITE
="$ff.write" \
714 saferun
-a
-o
"$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
715 mv "$ff.time.out" "$ff.times.out"
716 awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
718 if [ "x$verbose" = "xtrue" ]; then
722 if [ -f
"$ff.write" ]; then
723 cat -- "$ff.write" >> "$ff.res"
724 elif [ -d
"$ff.write" ]; then
725 /bin
/ls -F
"$ff.write" >> "$ff.res"
727 cp -- "$ff.res" "$ff.res2"
728 cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
729 process_result
"$bf" "$bf" "$pack"
731 if [ -f
"$f.args" ]; then
739 name
="$bf args $cptr"
741 # Sould we skip the input for this engine?
742 need_skip
"$bff" " $name" "$pack" && continue
744 # use a specific inputs file, if required
745 if [ -f
"$bff.inputs" ]; then
746 ffinputs
="$bff.inputs"
751 rm -rf
"$fff.res" "$fff.err" "$fff.write" 2> /dev
/null
752 if [ "x$verbose" = "xtrue" ]; then
754 echo "NIT_NO_STACK=1 $ff.bin" $args
757 echo "$ff.bin $args" > "$fff.bin"
759 WRITE
="$fff.write" saferun
-o
"$fff.time.out" sh
-c
"NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
760 if [ "x$verbose" = "xtrue" ]; then
762 cat >&2 -- "$fff.err"
764 if [ -f
"$fff.write" ]; then
765 cat -- "$fff.write" >> "$fff.res"
766 elif [ -d
"$fff.write" ]; then
767 /bin
/ls -F
-- "$fff.write" >> "$fff.res"
769 if [ -s
"$fff.err" ]; then
770 cp -- "$fff.res" "$fff.res2"
771 cat -- "$fff.err" "$fff.res2" > "$fff.res"
773 process_result
"$bff" " $name" "$pack"
776 elif [ -f
"$ff.bin" ]; then
777 #Not executable (platform?)"
779 process_result
"$bf" "$bf" "$pack"
782 cat -- "$ff.cmp.err" > "$ff.res"
783 echo "Compilation error" > "$ff.res"
784 process_result
"$bf" "$bf" "$pack"
789 if [ "x$isnode" = "xfalse" ]; then
790 echo "engine: $engine ($enginebinname $OPT)"
791 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
793 if [ -n
"$nok" ]; then
795 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
797 if [ -n
"$nos" ]; then
800 if [ -n
"$todos" ]; then
801 echo "todo/fixme: $todos"
803 if [ -n
"$remains" ]; then
804 echo "sav that remains: $remains"
809 if [ "x$ERRLIST" != "x" ]; then
810 if [ -f
"$ERRLIST_TARGET" ]; then
811 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
813 uniq $ERRLIST > $ERRLIST_TARGET
817 echo >>$xml "</testsuite></testsuites>"
819 if [ -n
"$nok" ]; then