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 export NIT_TESTING
=true
24 # Use the pid as a collision prevention
25 export NIT_TESTING_ID
=$$
30 # Get the first Java lib available
31 if which_java
=$
(which javac
2>/dev
/null
); then
32 JAVA_HOME
=$
(dirname $
(dirname $
(readlink
-f
"$which_java")))
35 paths
=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
37 JNI_LIB_PATH
=`dirname ${paths[0]}`
44 # User CPU time limit (in seconds)
45 # Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
46 usertimelimit
=600 # 1 CPU minute
48 # Real-time limit (in seconds)
49 # Is used to avoid waiting or sleeping tests.
50 # Require timeout or timelimit, or else is not used.
51 realtimelimit
=300 # 5 min
53 # User limit for write files (in kilo-bytes)
54 # Is used to avoid execution that loop and fill the hard drive. See ulimit -f
55 # Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
56 filelimit
=100000 # ~100MB
58 # Limit (in bytes) for generated .res file.
59 # Larger ones are truncated and will fail tests
60 # Is used to avoid processing huge crappy res file (diff, xml, etc)
61 reslimit
=100000 # ~100KB
67 Usage
: $e [options
] modulenames
68 -o option Pass option to the engine
69 -v Verbose
(show tests steps
)
71 --engine Use a specific engine
(default
=nitc
)
72 --noskip Do not skip a
test even
if the .skip
file matches
73 --outdir Use a specific output folder
(default
=out
/)
74 --compdir Use a specific temporary compilation folder
(default
=$compdir)
75 --node Run as a node
in parallel
, will not output context information
76 --autosav Copy the .res files directly
in the sav folder overriding existing .res files
80 # Run a command with a timeout and a time count.
82 # -o file write the user time into file (REQUIRED). see `-o` in `man time`
83 # -a append the time to the file (instead of overwriting it). see `-a` in `man time`
89 while [ $stop = false
]; do
91 -o
) o
="$2"; shift; shift;;
97 ulimit -f
"$filelimit" 2> /dev
/null
98 ulimit -t
"$usertimelimit" 2> /dev
/null
101 elif test -n
"$TIME"; then
102 $TIME -o
"$o" $a $TIMEOUT "$@"
104 if test -n
"$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
110 # Output a timestamp attribute for XML, or an empty line
113 if test -n
"$TIMESTAMP"; then
114 echo "timestamp='`$TIMESTAMP`'"
121 # Get platform specific commands ##########################
123 # Detect a working timeout
124 if sh
-c
"timelimit echo" 1>/dev
/null
2>&1; then
125 TIMEOUT
="timelimit -t $realtimelimit"
126 elif sh
-c
"timeout 1 echo" 1>/dev
/null
2>&1; then
127 TIMEOUT
="timeout ${realtimelimit}s"
129 echo "No timelimit or timeout command detected. Tests may hang :("
132 # Detect a working time command
133 if env
time --quiet
-f
%U true
2>/dev
/null
; then
134 TIME
="env time --quiet -f%U"
135 elif env
time -f
%U true
2>/dev
/null
; then
141 # Detect a working date command
142 if date -Iseconds
>/dev
/null
2>&1; then
143 TIMESTAMP
="date -Iseconds"
148 # $1 is the pattern of the test
149 # $2 is the file to compare to
151 # 0: if the file to compare to do not exists
152 # 1: if the file match
153 # 2: if the file match with soso
154 # 3: if the file do not match
155 function compare_to_result
()
159 if [ ! -r
"$sav" ]; then return 0; fi
160 test "`cat -- "$sav"`" = "UNDEFINED" && return 1
161 diff -u
-- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
162 if [ "$?" == 0 ]; then
165 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
166 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
167 grep '[Ee]rror' "$outdir/$pattern.res" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.res2"
168 grep '[Ee]rror' "$sav" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.sav2"
169 diff -u
"$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
170 if [ "$?" == 0 ]; then
179 sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'<<EOF
184 # As argument: the pattern used for the file
185 function process_result
()
203 # Truncate too big res file
204 local size
=$
(wc -c
< "$outdir/$pattern.res")
205 if test -n
"$reslimit" -a
"$size" -gt
"$reslimit"; then
206 # The most portable way to truncate a file is with Perl
207 perl
-e
"truncate \"$outdir/$pattern.res\", $reslimit;"
208 echo "***TRUNCATED***" >> "$outdir/$pattern.res"
211 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.
time.out
"`' `timestamp`>"
212 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
213 for savdir
in $savdirs; do
214 local sav
=$savdir/fixme
/$pattern.res
215 compare_to_result
"$pattern" "$sav"
225 if [ -z
"$FIRST" ]; then
232 if [ -z
"$FIRST" ]; then
240 sav
=$savdir/$pattern.res
241 compare_to_result
"$pattern" "$sav"
251 if [ -z
"$FIRST" ]; then
258 if [ -z
"$FIRST" ]; then
266 OLD
=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
267 istodo
"$outdir/$pattern.res"
269 if [ -n
"$SAV" ]; then
270 if [ -n
"$OLD" ]; then
271 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
272 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
273 remains
="$remains $OLD"
274 test "$autosav" = "true" && rm "$OLD"
276 echo "[ok] $outdir/$pattern.res $SAV"
279 elif [ -n
"$FIXME" ]; then
280 if [ -n
"$OLD" ]; then
281 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
282 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
283 remains
="$remains $OLD"
284 test "$autosav" = "true" && rm "$OLD"
286 echo "[fixme] $outdir/$pattern.res $FIXME"
287 echo >>$xml "<skipped/>"
289 todos
="$todos $pattern"
290 elif [ "x$NYI" = "x0" ]; then
291 echo "[todo] $outdir/$pattern.res -> not yet implemented"
292 echo >>$xml "<skipped/>"
293 todos
="$todos $pattern"
294 elif [ -n
"$SOSO" ]; then
295 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
296 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
297 echo >>$xml "<system-out><![CDATA["
298 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
299 echo >>$xml "]]></system-out>"
301 echo "$ii" >> "$ERRLIST"
302 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$SOSO"
303 elif [ -n
"$SOSOF" ]; then
304 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
305 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
306 echo >>$xml "<system-out><![CDATA["
307 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
308 echo >>$xml "]]></system-out>"
310 echo "$ii" >> "$ERRLIST"
311 test "$autosav" = "true" && cp "$outdir/$pattern.res" && "$SOSO"
312 elif [ -n
"$NSAV" ]; then
313 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
314 echo >>$xml "<error message='`xmlesc "fail
$outdir/$pattern.res
$NSAV"`'/>"
315 echo >>$xml "<system-out><![CDATA["
316 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
317 echo >>$xml "]]></system-out>"
319 echo "$ii" >> "$ERRLIST"
320 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NSAV"
321 elif [ -n
"$NFIXME" ]; then
322 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
323 echo >>$xml "<error message='`xmlesc "changed
$outdir/$pattern.res
$NFIXME"`'/>"
324 echo >>$xml "<system-out><![CDATA["
325 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
326 echo >>$xml "]]></system-out>"
328 echo "$ii" >> "$ERRLIST"
329 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$NFIXME"
330 elif [ -s
"$outdir/$pattern.res" ]; then
331 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
332 echo >>$xml "<error message='no sav and not empty'/>"
333 echo >>$xml "<system-out><![CDATA["
334 cat -v
>>$xml -- "$outdir/$pattern.res"
335 echo >>$xml "]]></system-out>"
337 echo "$ii" >> "$ERRLIST"
338 test "$autosav" = "true" && cp "$outdir/$pattern.res" "sav/"
340 # no sav but empty res
341 echo "[0k] $outdir/$pattern.res is empty"
344 if test -s
"$outdir/$pattern.cmp.err"; then
345 echo >>$xml "<system-err><![CDATA["
346 cat -v
>>$xml -- "$outdir/$pattern.cmp.err"
347 echo >>$xml "]]></system-err>"
349 echo >>$xml "</testcase>"
354 test "$noskip" = true
&& return 1
355 if echo "$1" |
grep -f
"$engine.skip" >/dev
/null
2>&1; then
357 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
360 if test -n
"$isinterpret" && echo "$1" |
grep -f
"exec.skip" >/dev
/null
2>&1; then
361 echo "=> $2: [skip exec]"
362 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
367 local os_skip_file
=`uname`.skip
368 if test -e
$os_skip_file && echo "$1" |
grep -f
"$os_skip_file" >/dev
/null
2>&1; then
369 echo "=> $2: [skip os]"
370 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
375 local host_skip_file
=`hostname -s`.skip
376 if test -e
$host_skip_file && echo "$1" |
grep -f
"$host_skip_file" >/dev
/null
2>&1; then
377 echo "=> $2: [skip hostname]"
378 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
386 test "$noskip" = true
&& return 1
387 for savdir
in $savdirs .
; do
388 local f
="$savdir/exec.skip"
389 test -f
"$f" ||
continue
390 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
391 echo -n
"_ no exec by $f; "
400 test "$noskip" = true
&& return 1
401 for savdir
in $savdirs .
; do
402 local f
="$savdir/cc.skip"
403 test -f
"$f" ||
continue
404 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
411 # Check that the resfile ($1) matches some magic strings in `todo` files.
414 test "$no" = true
&& return 1
415 for savdir
in $savdirs .
; do
416 local f
="$savdir/todo"
417 test -f
"$f" ||
continue
418 if grep -f
"$f" "$1" >/dev
/null
2>&1; then
427 local name
="$enginebinname"
428 local recent
=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
429 if [[ "x$recent" == "x" ]]; then
430 echo "Could not find binary for engine $engine, aborting"
433 if [ "x$isnode" = "xfalse" ]; then
434 echo "Found binary for engine $engine: $recent $OPT"
446 while [ $stop = false
]; do
448 -o
) OPT
="$OPT $2"; shift; shift;;
449 -v
) verbose
=true
; shift;;
451 --engine
) engine
="$2"; shift; shift;;
452 --noskip
) noskip
=true
; shift;;
453 --outdir
) outdir
="$2"; shift; shift;;
454 --compdir
) compdir
="$2"; shift; shift;;
455 --node
) isnode
=true
; shift;;
456 --autosav
) autosav
=true
; shift;;
460 enginebinname
=$engine
466 OPT
="--separate $OPT --compile-dir $compdir"
467 savdirs
="sav/nitc-common/"
472 OPT
="--separate $OPT --compile-dir $compdir"
473 savdirs
="sav/nitc-common/"
478 OPT
="--erasure $OPT --compile-dir $compdir"
479 savdirs
="sav/nitc-common/"
484 OPT
="--semi-global $OPT --compile-dir $compdir"
485 savdirs
="sav/nitc-common/"
490 OPT
="--global $OPT --compile-dir $compdir"
491 savdirs
="sav/nitc-common/"
509 OPT
="--compile-dir $compdir --ant"
511 savdirs
="sav/nitc-common/"
515 OPT
="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
516 savdirs
="sav/nitcsg/"
519 echo "unknown engine $engine"
524 savdirs
="sav/`hostname -s` sav/`uname` sav/$engine $savdirs sav/"
526 # The default nitc compiler
527 [ -z
"$NITC" ] && find_nitc
529 # Set NIT_DIR if needed
530 [ -z
"$NIT_DIR" ] && export NIT_DIR
=..
532 # Mark to distinguish files among tests
540 # CLEAN the out directory
541 rm -rf
"$outdir/" 2>/dev
/null
542 mkdir
"$outdir" 2>/dev
/null
544 # File where error tests are outputed
545 # Old ERRLIST is backuped
546 ERRLIST
=${ERRLIST:-errlist}
547 ERRLIST_TARGET
=$ERRLIST
549 # Initiate new ERRLIST
550 if [ "x$ERRLIST" = "x" ]; then
561 if [ "x$XMLDIR" = "x" ]; then
562 xml
="tests-$engine.xml"
564 sum=`echo $@ | md5sum | cut -f1 -d " "`
565 xml
="$XMLDIR/tests-$engine-$sum.xml"
569 echo >$xml "<testsuites><testsuite>"
572 if [ ! -f
"$ii" ]; then
573 echo "File '$ii' does not exist."
576 f
=`basename -- "$ii" .nit`
578 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|'`
580 # Sould we skip the file for this engine?
581 need_skip
"$f" "$f" "$pack" && continue
584 if [ "x$tmp" = "x$ii" ]; then
585 includes
="-I . -I ../lib/core -I ../lib/core/collection -I alt"
590 for i
in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
591 bf
=`basename -- "$i" .nit`
594 # Sould we skip the alternative for this engine?
595 need_skip
"$bf" "$bf" "$pack" && continue
599 if [ -f
"$f.inputs" ]; then
601 export MNIT_READ_INPUT
="$f.inputs"
604 export MNIT_READ_INPUT
=/dev
/null
608 if [ "$engine" = "emscripten" ]; then
612 if [ -n
"$isinterpret" ]; then
613 cat > "$ff.bin" <<END
614 exec $NITC --no
-color
$OPT $includes -- $
(printf '%q' "$i") "\$@"
620 echo 0.0 > "$ff.time.out"
622 if skip_cc
"$bf"; then
628 if [ "x$verbose" = "xtrue" ]; then
630 echo $NITC --no
-color
$OPT -o
"$ffout" "$includes" $nocc "$i"
632 NIT_NO_STACK
=1 JNI_LIB_PATH
=$JNI_LIB_PATH JAVA_HOME
=$JAVA_HOME \
633 saferun
-o
"$ff.time.out" $NITC --no
-color
$OPT -o
"$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
635 if [ "x$verbose" = "xtrue" ]; then
636 cat -- "$ff.compile.log"
637 cat >&2 -- "$ff.cmp.err"
640 rm -r
"$compdir" 2>/dev
/null
642 if [ "$engine" = "emscripten" ]; then
643 echo > "$ff.bin" "nodejs $ffout \"\$@\""
645 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev
/null
; then
646 echo " [skip] do no not imports kernel"
647 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
651 if [ "$ERR" != 0 ]; then
653 cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
654 process_result
"$bf" "$bf" "$pack"
655 elif [ -n
"$nocc" ]; then
659 process_result
"$bf" "$bf" "$pack"
660 elif [ -x
"$ff.bin" ]; then
661 if skip_exec
"$bf"; then
664 process_result
"$bf" "$bf" "$pack"
670 if [ "x$verbose" = "xtrue" ]; then
672 echo "NIT_NO_STACK=1 $ff.bin" $args
674 NIT_NO_STACK
=1 LD_LIBRARY_PATH
=$JNI_LIB_PATH \
675 saferun
-a
-o
"$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
676 mv "$ff.time.out" "$ff.times.out"
677 awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
679 if [ "x$verbose" = "xtrue" ]; then
683 if [ -f
"$ff.write" ]; then
684 cat -- "$ff.write" >> "$ff.res"
685 elif [ -d
"$ff.write" ]; then
686 LANG
=C
/bin
/ls -F
"$ff.write" >> "$ff.res"
688 cp -- "$ff.res" "$ff.res2"
689 cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
690 process_result
"$bf" "$bf" "$pack"
692 if [ -f
"$f.args" ]; then
700 name
="$bf args $cptr"
702 # Sould we skip the input for this engine?
703 need_skip
"$bff" " $name" "$pack" && continue
705 # use a specific inputs file, if required
706 if [ -f
"$bff.inputs" ]; then
707 ffinputs
="$bff.inputs"
712 rm -rf
"$fff.res" "$fff.err" "$fff.write" 2> /dev
/null
713 if [ "x$verbose" = "xtrue" ]; then
715 echo "NIT_NO_STACK=1 $ff.bin" $args
718 echo "$ff.bin $args" > "$fff.bin"
720 WRITE
="$fff.write" saferun
-o
"$fff.time.out" sh
-c
"NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
721 if [ "x$verbose" = "xtrue" ]; then
723 cat >&2 -- "$fff.err"
725 if [ -f
"$fff.write" ]; then
726 cat -- "$fff.write" >> "$fff.res"
727 elif [ -d
"$fff.write" ]; then
728 LANG
=C
/bin
/ls -F
-- "$fff.write" >> "$fff.res"
730 if [ -s
"$fff.err" ]; then
731 cp -- "$fff.res" "$fff.res2"
732 cat -- "$fff.err" "$fff.res2" > "$fff.res"
734 process_result
"$bff" " $name" "$pack"
737 elif [ -f
"$ff.bin" ]; then
738 #Not executable (platform?)"
740 process_result
"$bf" "$bf" "$pack"
743 cat -- "$ff.cmp.err" > "$ff.res"
744 echo "Compilation error" > "$ff.res"
745 process_result
"$bf" "$bf" "$pack"
750 if [ "x$isnode" = "xfalse" ]; then
751 echo "engine: $engine ($enginebinname $OPT)"
752 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
754 if [ -n
"$nok" ]; then
756 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
758 if [ -n
"$nos" ]; then
761 if [ -n
"$todos" ]; then
762 echo "todo/fixme: $todos"
764 if [ -n
"$remains" ]; then
765 echo "sav that remains: $remains"
770 if [ "x$ERRLIST" != "x" ]; then
771 if [ -f
"$ERRLIST_TARGET" ]; then
772 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
774 uniq $ERRLIST > $ERRLIST_TARGET
778 echo >>$xml "</testsuite></testsuites>"
780 if [ -n
"$nok" ]; then