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
=$$
33 # Identify this as a tests.sh test to differentiate from a nitunit test
34 export NIT_TESTING_TESTS_SH
=true
38 # Get the first Java lib available
39 if which_java
=$
(which javac
2>/dev
/null
); then
41 if sh
-c
"readlink -f ." 1>/dev
/null
2>&1; then
42 READLINK
="readlink -f"
47 JAVA_HOME
=$
(dirname $
(dirname $
($READLINK "$which_java")))
50 paths
=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
51 if [ -n
"$paths" ]; then
53 JNI_LIB_PATH
=`dirname ${paths[0]}`
61 # User CPU time limit (in seconds)
62 # Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
63 usertimelimit
=600 # 1 CPU minute
65 # Real-time limit (in seconds)
66 # Is used to avoid waiting or sleeping tests.
67 # Require timeout or timelimit, or else is not used.
68 realtimelimit
=300 # 5 min
70 # User limit for write files (in kilo-bytes)
71 # Is used to avoid execution that loop and fill the hard drive. See ulimit -f
72 # Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
73 filelimit
=100000 # ~100MB
75 # Limit (in bytes) for generated .res file.
76 # Larger ones are truncated and will fail tests
77 # Is used to avoid processing huge crappy res file (diff, xml, etc)
78 reslimit
=100000 # ~100KB
84 Usage
: $e [options
] modulenames
85 -o option Pass option to the engine
86 -v Verbose
(show tests steps
)
88 --engine Use a specific engine
(default
=nitc
)
89 --noskip Do not skip a
test even
if the .skip
file matches
90 --outdir Use a specific output folder
(default
=out
/)
91 --compdir Use a specific temporary compilation folder
(default
=$compdir)
92 --node Run as a node
in parallel
, will not output context information
93 --autosav Copy the .res files directly
in the sav folder overriding existing .res files
97 # Run a command with a timeout and a time count.
99 # -o file write the user time into file (REQUIRED). see `-o` in `man time`
100 # -a append the time to the file (instead of overwriting it). see `-a` in `man time`
106 while [ $stop = false
]; do
108 -o
) o
="$2"; shift; shift;;
114 ulimit -f
"$filelimit" 2> /dev
/null
115 ulimit -t
"$usertimelimit" 2> /dev
/null
116 if test -d
"$1"; then
118 elif test -n
"$TIME"; then
119 $TIME -o
"$o" $a $TIMEOUT "$@"
121 if test -n
"$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
127 # Output a timestamp attribute for XML, or an empty line
130 if test -n
"$TIMESTAMP"; then
131 echo "timestamp='`$TIMESTAMP`'"
138 # Get platform specific commands ##########################
140 # Detect a working timeout
141 if sh
-c
"timelimit echo" 1>/dev
/null
2>&1; then
142 TIMEOUT
="timelimit -t $realtimelimit"
143 elif sh
-c
"timeout 1 echo" 1>/dev
/null
2>&1; then
144 TIMEOUT
="timeout ${realtimelimit}s"
146 echo "No timelimit or timeout command detected. Tests may hang :("
149 # Detect a working time command
150 if command time --quiet
-f
%e true
2>/dev
/null
; then
151 TIME
="command time --quiet -f%e"
152 elif command time -f
%e true
2>/dev
/null
; then
153 TIME
="command time -f%e"
154 elif command gtime
-f
%e true
2>/dev
/null
; then
155 TIME
="command gtime -f%e"
160 # Detect a working date command
161 if date -Iseconds
>/dev
/null
2>&1; then
162 TIMESTAMP
="date -Iseconds"
167 # Detect a working hostname command
168 if hostname
--version
2>&1 |
grep coreutils
>/dev
/null
2>&1; then
171 HOSTNAME
="hostname -s"
174 UNAME
=`uname | sed s/-.*//`
176 # $1 is the pattern of the test
177 # $2 is the file to compare to
179 # 0: if the file to compare to do not exists
180 # 1: if the file match
181 # 2: if the file match with soso
182 # 3: if the file do not match
183 function compare_to_result
()
187 if [ ! -r
"$sav" ]; then return 0; fi
188 test "`cat -- "$sav"`" = "UNDEFINED" && return 1
189 diff -u
--strip
-trailing
-cr
-- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
190 if [ "$?" == 0 ]; then
193 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
194 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
195 grep '[Ee]rror' "$outdir/$pattern.res" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.res2"
196 grep '[Ee]rror' "$sav" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.sav2"
197 diff -u
--strip
-trailing
-cr
"$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
198 if [ "$?" == 0 ]; then
207 sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'<<EOF
212 # As argument: the pattern used for the file
213 function process_result
()
231 # Truncate too big res file
232 local size
=$
(wc -c
< "$outdir/$pattern.res")
233 if test -n
"$reslimit" -a
"$size" -gt
"$reslimit"; then
234 # The most portable way to truncate a file is with Perl
235 perl
-e
"truncate \"$outdir/$pattern.res\", $reslimit;"
236 echo "***TRUNCATED***" >> "$outdir/$pattern.res"
239 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.
time.out
"`' `timestamp`>"
240 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
241 for savdir
in $savdirs; do
242 local sav
=$savdir/fixme
/$pattern.res
243 compare_to_result
"$pattern" "$sav"
253 if [ -z
"$FIRST" ]; then
260 if [ -z
"$FIRST" ]; then
268 sav
=$savdir/$pattern.res
269 compare_to_result
"$pattern" "$sav"
279 if [ -z
"$FIRST" ]; then
286 if [ -z
"$FIRST" ]; then
294 OLD
=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
295 istodo
"$outdir/$pattern.res"
297 if [ -n
"$SAV" ]; then
298 if [ -n
"$OLD" ]; then
299 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
300 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
301 remains
="$remains $OLD"
302 test "$autosav" = "true" && rm "$OLD"
304 echo "[ok] $outdir/$pattern.res $SAV"
307 elif [ -n
"$FIXME" ]; then
308 if [ -n
"$OLD" ]; then
309 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
310 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
311 remains
="$remains $OLD"
312 test "$autosav" = "true" && rm "$OLD"
314 echo "[fixme] $outdir/$pattern.res $FIXME"
315 echo >>$xml "<skipped/>"
317 todos
="$todos $pattern"
318 elif [ "x$NYI" = "x0" ]; then
319 echo "[todo] $outdir/$pattern.res -> not yet implemented"
320 echo >>$xml "<skipped/>"
321 todos
="$todos $pattern"
322 elif [ -n
"$SOSO" ]; then
323 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
324 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
325 echo >>$xml "<system-out><![CDATA["
326 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
327 echo >>$xml "]]></system-out>"
329 echo "$ii" >> "$ERRLIST"
330 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$SOSO"
331 elif [ -n
"$SOSOF" ]; then
332 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
333 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
334 echo >>$xml "<system-out><![CDATA["
335 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
336 echo >>$xml "]]></system-out>"
338 echo "$ii" >> "$ERRLIST"
339 test "$autosav" = "true" && cp "$outdir/$pattern.res" && "$SOSO"
340 elif [ -n
"$NSAV" ]; then
341 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
342 echo >>$xml "<error message='`xmlesc "fail
$outdir/$pattern.res
$NSAV"`'/>"
343 echo >>$xml "<system-out><![CDATA["
344 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
345 echo >>$xml "]]></system-out>"
347 echo "$ii" >> "$ERRLIST"
348 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NSAV"
349 elif [ -n
"$NFIXME" ]; then
350 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
351 echo >>$xml "<error message='`xmlesc "changed
$outdir/$pattern.res
$NFIXME"`'/>"
352 echo >>$xml "<system-out><![CDATA["
353 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
354 echo >>$xml "]]></system-out>"
356 echo "$ii" >> "$ERRLIST"
357 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NFIXME"
358 elif [ -s
"$outdir/$pattern.res" ]; then
359 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
360 echo >>$xml "<error message='no sav and not empty'/>"
361 echo >>$xml "<system-out><![CDATA["
362 cat -v
>>$xml -- "$outdir/$pattern.res"
363 echo >>$xml "]]></system-out>"
365 echo "$ii" >> "$ERRLIST"
366 test "$autosav" = "true" && cp "$outdir/$pattern.res" "sav/"
368 # no sav but empty res
369 echo "[0k] $outdir/$pattern.res is empty"
372 if test -s
"$outdir/$pattern.cmp.err"; then
373 echo >>$xml "<system-err><![CDATA["
374 cat -v
>>$xml -- "$outdir/$pattern.cmp.err"
375 echo >>$xml "]]></system-err>"
377 echo >>$xml "</testcase>"
382 test "$noskip" = true
&& return 1
383 if echo "$1" |
grep -f
"$engine.skip" >/dev
/null
2>&1; then
385 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
388 if test -n
"$isinterpret" && echo "$1" |
grep -f
"exec.skip" >/dev
/null
2>&1; then
389 echo "=> $2: [skip exec]"
390 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
395 local os_skip_file
=$UNAME.skip
396 if test -e
$os_skip_file && echo "$1" |
grep -f
"$os_skip_file" >/dev
/null
2>&1; then
397 echo "=> $2: [skip os]"
398 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
403 local host_skip_file
=`$HOSTNAME`.skip
404 if test -e
$host_skip_file && echo "$1" |
grep -f
"$host_skip_file" >/dev
/null
2>&1; then
405 echo "=> $2: [skip hostname]"
406 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
414 test "$noskip" = true
&& return 1
415 for savdir
in $savdirs .
; do
416 local f
="$savdir/exec.skip"
417 test -f
"$f" ||
continue
418 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
419 echo -n
"_ no exec by $f; "
428 test "$noskip" = true
&& return 1
429 for savdir
in $savdirs .
; do
430 local f
="$savdir/cc.skip"
431 test -f
"$f" ||
continue
432 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
439 # Check that the resfile ($1) matches some magic strings in `todo` files.
442 test "$no" = true
&& return 1
443 for savdir
in $savdirs .
; do
444 local f
="$savdir/todo"
445 test -f
"$f" ||
continue
446 if grep -f
"$f" "$1" >/dev
/null
2>&1; then
455 local name
="$enginebinname"
456 local recent
=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../contrib/nitin/bin/$name ../c_src/$name 2>/dev/null | head -1`
457 if [[ "x$recent" == "x" ]]; then
458 echo "Could not find binary for engine $engine, aborting"
461 if [ "x$isnode" = "xfalse" ]; then
462 echo "Found binary for engine $engine: $recent $OPT"
474 while [ $stop = false
]; do
476 -o
) OPT
="$OPT $2"; shift; shift;;
477 -v
) verbose
=true
; shift;;
479 --engine
) engine
="$2"; shift; shift;;
480 --noskip
) noskip
=true
; shift;;
481 --outdir
) outdir
="$2"; shift; shift;;
482 --compdir
) compdir
="$2"; shift; shift;;
483 --node
) isnode
=true
; shift;;
484 --autosav
) autosav
=true
; shift;;
488 enginebinname
=$engine
495 OPT
="--separate $OPT --compile-dir $compdir"
496 savdirs
="sav/nitc-common/"
501 OPT
="--separate $OPT --compile-dir $compdir"
502 savdirs
="sav/nitc-common/"
507 OPT
="--erasure $OPT --compile-dir $compdir"
508 savdirs
="sav/nitc-common/"
513 OPT
="--semi-global $OPT --compile-dir $compdir"
514 savdirs
="sav/nitc-common/"
519 OPT
="--global $OPT --compile-dir $compdir"
520 savdirs
="sav/nitc-common/"
542 OPT
="--compile-dir $compdir --ant"
544 savdirs
="sav/nitc-common/"
548 OPT
="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
549 savdirs
="sav/nitcsg/"
552 echo "unknown engine $engine"
557 savdirs
="sav/`$HOSTNAME` sav/$UNAME sav/$engine $savdirs sav/"
559 # The default nitc compiler
560 [ -z
"$NITC" ] && find_nitc
562 # Set NIT_DIR if needed
563 [ -z
"$NIT_DIR" ] && export NIT_DIR
=..
565 # Mark to distinguish files among tests
573 # CLEAN the out directory
574 rm -rf
"$outdir/" 2>/dev
/null
575 mkdir
"$outdir" 2>/dev
/null
577 # File where error tests are outputed
578 # Old ERRLIST is backuped
579 ERRLIST
=${ERRLIST:-errlist}
580 ERRLIST_TARGET
=$ERRLIST
582 # Initiate new ERRLIST
583 if [ "x$ERRLIST" = "x" ]; then
594 if [ "x$XMLDIR" = "x" ]; then
595 xml
="tests-$engine.xml"
597 sum=`echo $@ | md5sum | cut -f1 -d " "`
598 xml
="$XMLDIR/tests-$engine-$sum.xml"
602 echo >$xml "<testsuites><testsuite>"
605 if [ ! -f
"$ii" ]; then
606 echo "File '$ii' does not exist."
609 f
=`basename -- "$ii" .nit`
611 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|'`
613 # Sould we skip the file for this engine?
614 need_skip
"$f" "$f" "$pack" && continue
616 local_tmp
=${ii/../AA}
617 if [ "x$local_tmp" = "x$ii" ]; then
618 includes
="-I . -I ../lib/core -I ../lib/core/collection -I alt"
623 for i
in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
624 bf
=`basename -- "$i" .nit`
627 # Sould we skip the alternative for this engine?
628 need_skip
"$bf" "$bf" "$pack" && continue
632 if [ -f
"$f.inputs" ]; then
634 export MNIT_READ_INPUT
="$f.inputs"
637 export MNIT_READ_INPUT
=/dev
/null
641 if [ "$engine" = "emscripten" ]; then
645 if [ -n
"$isinterpret" ]; then
646 cat > "$ff.bin" <<END
647 exec $NITC --no
-color
$OPT $includes -- $
(printf '%q' "$i") "\$@"
653 echo 0.0 > "$ff.time.out"
654 elif [ -n
"$isinteractive" ]; then
655 cat > "$ff.bin" <<END
656 exec $NITC --no
-color
--no
-prompt
--source-name $
(printf '%q' "$i") $OPT $includes < $
(printf '%q' "$i") "\$@"
662 echo 0.0 > "$ff.time.out"
664 if skip_cc
"$bf"; then
670 if [ "x$verbose" = "xtrue" ]; then
672 echo $NITC --no
-color
$OPT -o
"$ffout" "$includes" $nocc "$i"
674 NIT_NO_STACK
=1 JNI_LIB_PATH
=$JNI_LIB_PATH JAVA_HOME
=$JAVA_HOME \
675 saferun
-o
"$ff.time.out" $NITC --no
-color
$OPT -o
"$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
677 if [ "x$verbose" = "xtrue" ]; then
678 cat -- "$ff.compile.log"
679 cat >&2 -- "$ff.cmp.err"
682 rm -r
"$compdir" 2>/dev
/null
684 if [ "$engine" = "emscripten" ]; then
685 echo > "$ff.bin" "nodejs $ffout \"\$@\""
687 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev
/null
; then
688 echo " [skip] do no not imports kernel"
689 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
693 if [ "$ERR" != 0 ]; then
695 cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
696 process_result
"$bf" "$bf" "$pack"
697 elif [ -n
"$nocc" ]; then
701 process_result
"$bf" "$bf" "$pack"
702 elif [ -x
"$ff.bin" ]; then
703 if skip_exec
"$bf"; then
706 process_result
"$bf" "$bf" "$pack"
712 if [ "x$verbose" = "xtrue" ]; then
714 echo "NIT_NO_STACK=1 $ff.bin" $args
716 NIT_NO_STACK
=1 LD_LIBRARY_PATH
=$JNI_LIB_PATH WRITE
="$ff.write" \
717 saferun
-a
-o
"$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
718 mv "$ff.time.out" "$ff.times.out"
719 awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
721 if [ "x$verbose" = "xtrue" ]; then
725 if [ -f
"$ff.write" ]; then
726 cat -- "$ff.write" >> "$ff.res"
727 elif [ -d
"$ff.write" ]; then
728 /bin
/ls -F
"$ff.write" >> "$ff.res"
730 cp -- "$ff.res" "$ff.res2"
731 cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
732 process_result
"$bf" "$bf" "$pack"
734 if [ -f
"$f.args" ]; then
742 name
="$bf args $cptr"
744 # Sould we skip the input for this engine?
745 need_skip
"$bff" " $name" "$pack" && continue
747 # use a specific inputs file, if required
748 if [ -f
"$bff.inputs" ]; then
749 ffinputs
="$bff.inputs"
754 rm -rf
"$fff.res" "$fff.err" "$fff.write" 2> /dev
/null
755 if [ "x$verbose" = "xtrue" ]; then
757 echo "NIT_NO_STACK=1 $ff.bin" $args
760 echo "$ff.bin $args" > "$fff.bin"
762 WRITE
="$fff.write" saferun
-o
"$fff.time.out" sh
-c
"NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
763 if [ "x$verbose" = "xtrue" ]; then
765 cat >&2 -- "$fff.err"
767 if [ -f
"$fff.write" ]; then
768 cat -- "$fff.write" >> "$fff.res"
769 elif [ -d
"$fff.write" ]; then
770 /bin
/ls -F
-- "$fff.write" >> "$fff.res"
772 if [ -s
"$fff.err" ]; then
773 cp -- "$fff.res" "$fff.res2"
774 cat -- "$fff.err" "$fff.res2" > "$fff.res"
776 process_result
"$bff" " $name" "$pack"
779 elif [ -f
"$ff.bin" ]; then
780 #Not executable (platform?)"
782 process_result
"$bf" "$bf" "$pack"
785 cat -- "$ff.cmp.err" > "$ff.res"
786 echo "Compilation error" > "$ff.res"
787 process_result
"$bf" "$bf" "$pack"
792 if [ "x$isnode" = "xfalse" ]; then
793 echo "engine: $engine ($enginebinname $OPT)"
794 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
796 if [ -n
"$nok" ]; then
798 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
800 if [ -n
"$nos" ]; then
803 if [ -n
"$todos" ]; then
804 echo "todo/fixme: $todos"
806 if [ -n
"$remains" ]; then
807 echo "sav that remains: $remains"
812 if [ "x$ERRLIST" != "x" ]; then
813 if [ -f
"$ERRLIST_TARGET" ]; then
814 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
816 uniq $ERRLIST > $ERRLIST_TARGET
820 echo >>$xml "</testsuite></testsuites>"
822 if [ -n
"$nok" ]; then