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
28 # Get the first Java lib available
29 if which_java
=$
(which javac
2>/dev
/null
); then
30 JAVA_HOME
=$
(dirname $
(dirname $
(readlink
-f
"$which_java")))
33 paths
=`echo $JAVA_HOME/jre/lib/*/{client,server}/libjvm.so`
35 JNI_LIB_PATH
=`dirname ${paths[0]}`
42 # User CPU time limit (in seconds)
43 # Is used to avoid to CPU intensive test (infinite loops). See ulimit -t
44 usertimelimit
=600 # 1 CPU minute
46 # Real-time limit (in seconds)
47 # Is used to avoid waiting or sleeping tests.
48 # Require timeout or timelimit, or else is not used.
49 realtimelimit
=300 # 5 min
51 # User limit for write files (in kilo-bytes)
52 # Is used to avoid execution that loop and fill the hard drive. See ulimit -f
53 # Note that a test might require a lot of temporary disk space (eg. nitc+gcc)
54 filelimit
=100000 # ~100MB
56 # Limit (in bytes) for generated .res file.
57 # Larger ones are truncated and will fail tests
58 # Is used to avoid processing huge crappy res file (diff, xml, etc)
59 reslimit
=100000 # ~100KB
65 Usage
: $e [options
] modulenames
66 -o option Pass option to the engine
67 -v Verbose
(show tests steps
)
69 --engine Use a specific engine
(default
=nitc
)
70 --noskip Do not skip a
test even
if the .skip
file matches
71 --outdir Use a specific output folder
(default
=out
/)
72 --compdir Use a specific temporary compilation folder
(default
=$compdir)
73 --node Run as a node
in parallel
, will not output context information
74 --autosav Copy the .res files directly
in the sav folder overriding existing .res files
78 # Run a command with a timeout and a time count.
80 # -o file write the user time into file (REQUIRED). see `-o` in `man time`
81 # -a append the time to the file (instead of overwriting it). see `-a` in `man time`
87 while [ $stop = false
]; do
89 -o
) o
="$2"; shift; shift;;
95 ulimit -f
"$filelimit" 2> /dev
/null
96 ulimit -t
"$usertimelimit" 2> /dev
/null
99 elif test -n
"$TIME"; then
100 $TIME -o
"$o" $a $TIMEOUT "$@"
102 if test -n
"$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
108 # Output a timestamp attribute for XML, or an empty line
111 if test -n
"$TIMESTAMP"; then
112 echo "timestamp='`$TIMESTAMP`'"
119 # Get platform specific commands ##########################
121 # Detect a working timeout
122 if sh
-c
"timelimit echo" 1>/dev
/null
2>&1; then
123 TIMEOUT
="timelimit -t $realtimelimit"
124 elif sh
-c
"timeout 1 echo" 1>/dev
/null
2>&1; then
125 TIMEOUT
="timeout ${realtimelimit}s"
127 echo "No timelimit or timeout command detected. Tests may hang :("
130 # Detect a working time command
131 if env
time --quiet
-f
%U true
2>/dev
/null
; then
132 TIME
="env time --quiet -f%U"
133 elif env
time -f
%U true
2>/dev
/null
; then
139 # Detect a working date command
140 if date -Iseconds
>/dev
/null
2>&1; then
141 TIMESTAMP
="date -Iseconds"
146 # $1 is the pattern of the test
147 # $2 is the file to compare to
149 # 0: if the file to compare to do not exists
150 # 1: if the file match
151 # 2: if the file match with soso
152 # 3: if the file do not match
153 function compare_to_result
()
157 if [ ! -r
"$sav" ]; then return 0; fi
158 test "`cat -- "$sav"`" = "UNDEFINED" && return 1
159 diff -u
-- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
160 if [ "$?" == 0 ]; then
163 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
164 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
165 grep '[Ee]rror' "$outdir/$pattern.res" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.res2"
166 grep '[Ee]rror' "$sav" >/dev
/null
&& echo "Error" >> "$outdir/$pattern.sav2"
167 diff -u
"$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
168 if [ "$?" == 0 ]; then
177 sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'<<EOF
182 # As argument: the pattern used for the file
183 function process_result
()
201 # Truncate too big res file
202 local size
=$
(wc -c
< "$outdir/$pattern.res")
203 if test -n
"$reslimit" -a
"$size" -gt
"$reslimit"; then
204 # The most portable way to truncate a file is with Perl
205 perl
-e
"truncate \"$outdir/$pattern.res\", $reslimit;"
206 echo "***TRUNCATED***" >> "$outdir/$pattern.res"
209 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.
time.out
"`' `timestamp`>"
210 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
211 for savdir
in $savdirs; do
212 local sav
=$savdir/fixme
/$pattern.res
213 compare_to_result
"$pattern" "$sav"
223 if [ -z
"$FIRST" ]; then
230 if [ -z
"$FIRST" ]; then
238 sav
=$savdir/$pattern.res
239 compare_to_result
"$pattern" "$sav"
249 if [ -z
"$FIRST" ]; then
256 if [ -z
"$FIRST" ]; then
264 OLD
=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
265 istodo
"$outdir/$pattern.res"
267 if [ -n
"$SAV" ]; then
268 if [ -n
"$OLD" ]; then
269 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
270 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
271 remains
="$remains $OLD"
272 test "$autosav" = "true" && rm "$OLD"
274 echo "[ok] $outdir/$pattern.res $SAV"
277 elif [ -n
"$FIXME" ]; then
278 if [ -n
"$OLD" ]; then
279 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
280 echo >>$xml "<error message='`xmlesc "ok
$outdir/$pattern.res
- but
$OLD remains
"`'/>"
281 remains
="$remains $OLD"
282 test "$autosav" = "true" && rm "$OLD"
284 echo "[fixme] $outdir/$pattern.res $FIXME"
285 echo >>$xml "<skipped/>"
287 todos
="$todos $pattern"
288 elif [ "x$NYI" = "x0" ]; then
289 echo "[todo] $outdir/$pattern.res -> not yet implemented"
290 echo >>$xml "<skipped/>"
291 todos
="$todos $pattern"
292 elif [ -n
"$SOSO" ]; then
293 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
294 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
295 echo >>$xml "<system-out><![CDATA["
296 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
297 echo >>$xml "]]></system-out>"
299 echo "$ii" >> "$ERRLIST"
300 test "$autosav" = "true" && cp "$outdir/$pattern.res" "$SOSO"
301 elif [ -n
"$SOSOF" ]; then
302 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
303 echo >>$xml "<error message='`xmlesc "soso
$outdir/$pattern.res
$SOSO"`'/>"
304 echo >>$xml "<system-out><![CDATA["
305 cat -v
-- "$outdir/$pattern.diff.sav.log" |
head >>$xml -n
50
306 echo >>$xml "]]></system-out>"
308 echo "$ii" >> "$ERRLIST"
309 test "$autosav" = "true" && cp "$outdir/$pattern.res" && "$SOSO"
310 elif [ -n
"$NSAV" ]; then
311 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
312 echo >>$xml "<error message='`xmlesc "fail
$outdir/$pattern.res
$NSAV"`'/>"
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" "$NSAV"
319 elif [ -n
"$NFIXME" ]; then
320 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
321 echo >>$xml "<error message='`xmlesc "changed
$outdir/$pattern.res
$NFIXME"`'/>"
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" "$NFIXME"
328 elif [ -s
"$outdir/$pattern.res" ]; then
329 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
330 echo >>$xml "<error message='no sav and not empty'/>"
331 echo >>$xml "<system-out><![CDATA["
332 cat -v
>>$xml -- "$outdir/$pattern.res"
333 echo >>$xml "]]></system-out>"
335 echo "$ii" >> "$ERRLIST"
336 test "$autosav" = "true" && cp "$outdir/$pattern.res" "sav/"
338 # no sav but empty res
339 echo "[0k] $outdir/$pattern.res is empty"
342 if test -s
"$outdir/$pattern.cmp.err"; then
343 echo >>$xml "<system-err><![CDATA["
344 cat -v
>>$xml -- "$outdir/$pattern.cmp.err"
345 echo >>$xml "]]></system-err>"
347 echo >>$xml "</testcase>"
352 test "$noskip" = true
&& return 1
353 if echo "$1" |
grep -f
"$engine.skip" >/dev
/null
2>&1; then
355 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
358 if test -n
"$isinterpret" && echo "$1" |
grep -f
"exec.skip" >/dev
/null
2>&1; then
359 echo "=> $2: [skip exec]"
360 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
365 local os_skip_file
=`uname`.skip
366 if test -e
$os_skip_file && echo "$1" |
grep -f
"$os_skip_file" >/dev
/null
2>&1; then
367 echo "=> $2: [skip os]"
368 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
373 local host_skip_file
=`hostname -s`.skip
374 if test -e
$host_skip_file && echo "$1" |
grep -f
"$host_skip_file" >/dev
/null
2>&1; then
375 echo "=> $2: [skip hostname]"
376 echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
384 test "$noskip" = true
&& return 1
385 for savdir
in $savdirs .
; do
386 local f
="$savdir/exec.skip"
387 test -f
"$f" ||
continue
388 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
389 echo -n
"_ no exec by $f; "
398 test "$noskip" = true
&& return 1
399 for savdir
in $savdirs .
; do
400 local f
="$savdir/cc.skip"
401 test -f
"$f" ||
continue
402 if echo "$1" |
grep -f
"$f" >/dev
/null
2>&1; then
409 # Check that the resfile ($1) matches some magic strings in `todo` files.
412 test "$no" = true
&& return 1
413 for savdir
in $savdirs .
; do
414 local f
="$savdir/todo"
415 test -f
"$f" ||
continue
416 if grep -f
"$f" "$1" >/dev
/null
2>&1; then
425 local name
="$enginebinname"
426 local recent
=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
427 if [[ "x$recent" == "x" ]]; then
428 echo "Could not find binary for engine $engine, aborting"
431 if [ "x$isnode" = "xfalse" ]; then
432 echo "Found binary for engine $engine: $recent $OPT"
444 while [ $stop = false
]; do
446 -o
) OPT
="$OPT $2"; shift; shift;;
447 -v
) verbose
=true
; shift;;
449 --engine
) engine
="$2"; shift; shift;;
450 --noskip
) noskip
=true
; shift;;
451 --outdir
) outdir
="$2"; shift; shift;;
452 --compdir
) compdir
="$2"; shift; shift;;
453 --node
) isnode
=true
; shift;;
454 --autosav
) autosav
=true
; shift;;
458 enginebinname
=$engine
464 OPT
="--separate $OPT --compile-dir $compdir"
465 savdirs
="sav/nitc-common/"
470 OPT
="--separate $OPT --compile-dir $compdir"
471 savdirs
="sav/nitc-common/"
476 OPT
="--erasure $OPT --compile-dir $compdir"
477 savdirs
="sav/nitc-common/"
482 OPT
="--semi-global $OPT --compile-dir $compdir"
483 savdirs
="sav/nitc-common/"
488 OPT
="--global $OPT --compile-dir $compdir"
489 savdirs
="sav/nitc-common/"
507 OPT
="--compile-dir $compdir --ant"
509 savdirs
="sav/nitc-common/"
513 OPT
="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
514 savdirs
="sav/nitcsg/"
517 echo "unknown engine $engine"
522 savdirs
="sav/`hostname -s` sav/`uname` sav/$engine $savdirs sav/"
524 # The default nitc compiler
525 [ -z
"$NITC" ] && find_nitc
527 # Set NIT_DIR if needed
528 [ -z
"$NIT_DIR" ] && export NIT_DIR
=..
530 # Mark to distinguish files among tests
538 # CLEAN the out directory
539 rm -rf
"$outdir/" 2>/dev
/null
540 mkdir
"$outdir" 2>/dev
/null
542 # File where error tests are outputed
543 # Old ERRLIST is backuped
544 ERRLIST
=${ERRLIST:-errlist}
545 ERRLIST_TARGET
=$ERRLIST
547 # Initiate new ERRLIST
548 if [ "x$ERRLIST" = "x" ]; then
559 if [ "x$XMLDIR" = "x" ]; then
560 xml
="tests-$engine.xml"
562 sum=`echo $@ | md5sum | cut -f1 -d " "`
563 xml
="$XMLDIR/tests-$engine-$sum.xml"
567 echo >$xml "<testsuites><testsuite>"
570 if [ ! -f
"$ii" ]; then
571 echo "File '$ii' does not exist."
574 f
=`basename -- "$ii" .nit`
576 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|'`
578 # Sould we skip the file for this engine?
579 need_skip
"$f" "$f" "$pack" && continue
582 if [ "x$tmp" = "x$ii" ]; then
583 includes
="-I . -I ../lib/core -I ../lib/core/collection -I alt"
588 for i
in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
589 bf
=`basename -- "$i" .nit`
592 # Sould we skip the alternative for this engine?
593 need_skip
"$bf" "$bf" "$pack" && continue
597 if [ -f
"$f.inputs" ]; then
599 export MNIT_READ_INPUT
="$f.inputs"
602 export MNIT_READ_INPUT
=/dev
/null
606 if [ "$engine" = "emscripten" ]; then
610 if [ -n
"$isinterpret" ]; then
611 cat > "$ff.bin" <<END
612 exec $NITC --no
-color
$OPT $includes -- $
(printf '%q' "$i") "\$@"
618 echo 0.0 > "$ff.time.out"
620 if skip_cc
"$bf"; then
626 if [ "x$verbose" = "xtrue" ]; then
628 echo $NITC --no
-color
$OPT -o
"$ffout" "$includes" $nocc "$i"
630 NIT_NO_STACK
=1 JNI_LIB_PATH
=$JNI_LIB_PATH JAVA_HOME
=$JAVA_HOME \
631 saferun
-o
"$ff.time.out" $NITC --no
-color
$OPT -o
"$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
633 if [ "x$verbose" = "xtrue" ]; then
634 cat -- "$ff.compile.log"
635 cat >&2 -- "$ff.cmp.err"
638 rm -r
"$compdir" 2>/dev
/null
640 if [ "$engine" = "emscripten" ]; then
641 echo > "$ff.bin" "nodejs $ffout \"\$@\""
643 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev
/null
; then
644 echo " [skip] do no not imports kernel"
645 echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
649 if [ "$ERR" != 0 ]; then
651 cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
652 process_result
"$bf" "$bf" "$pack"
653 elif [ -n
"$nocc" ]; then
657 process_result
"$bf" "$bf" "$pack"
658 elif [ -x
"$ff.bin" ]; then
659 if skip_exec
"$bf"; then
662 process_result
"$bf" "$bf" "$pack"
668 if [ "x$verbose" = "xtrue" ]; then
670 echo "NIT_NO_STACK=1 $ff.bin" $args
672 NIT_NO_STACK
=1 LD_LIBRARY_PATH
=$JNI_LIB_PATH \
673 saferun
-a
-o
"$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
674 mv "$ff.time.out" "$ff.times.out"
675 awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
677 if [ "x$verbose" = "xtrue" ]; then
681 if [ -f
"$ff.write" ]; then
682 cat -- "$ff.write" >> "$ff.res"
683 elif [ -d
"$ff.write" ]; then
684 LANG
=C
/bin
/ls -F
"$ff.write" >> "$ff.res"
686 cp -- "$ff.res" "$ff.res2"
687 cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
688 process_result
"$bf" "$bf" "$pack"
690 if [ -f
"$f.args" ]; then
698 name
="$bf args $cptr"
700 # Sould we skip the input for this engine?
701 need_skip
"$bff" " $name" "$pack" && continue
703 # use a specific inputs file, if required
704 if [ -f
"$bff.inputs" ]; then
705 ffinputs
="$bff.inputs"
710 rm -rf
"$fff.res" "$fff.err" "$fff.write" 2> /dev
/null
711 if [ "x$verbose" = "xtrue" ]; then
713 echo "NIT_NO_STACK=1 $ff.bin" $args
716 echo "$ff.bin $args" > "$fff.bin"
718 WRITE
="$fff.write" saferun
-o
"$fff.time.out" sh
-c
"NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
719 if [ "x$verbose" = "xtrue" ]; then
721 cat >&2 -- "$fff.err"
723 if [ -f
"$fff.write" ]; then
724 cat -- "$fff.write" >> "$fff.res"
725 elif [ -d
"$fff.write" ]; then
726 LANG
=C
/bin
/ls -F
-- "$fff.write" >> "$fff.res"
728 if [ -s
"$fff.err" ]; then
729 cp -- "$fff.res" "$fff.res2"
730 cat -- "$fff.err" "$fff.res2" > "$fff.res"
732 process_result
"$bff" " $name" "$pack"
735 elif [ -f
"$ff.bin" ]; then
736 #Not executable (platform?)"
738 process_result
"$bf" "$bf" "$pack"
741 cat -- "$ff.cmp.err" > "$ff.res"
742 echo "Compilation error" > "$ff.res"
743 process_result
"$bf" "$bf" "$pack"
748 if [ "x$isnode" = "xfalse" ]; then
749 echo "engine: $engine ($enginebinname $OPT)"
750 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
752 if [ -n
"$nok" ]; then
754 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
756 if [ -n
"$nos" ]; then
759 if [ -n
"$todos" ]; then
760 echo "todo/fixme: $todos"
762 if [ -n
"$remains" ]; then
763 echo "sav that remains: $remains"
768 if [ "x$ERRLIST" != "x" ]; then
769 if [ -f
"$ERRLIST_TARGET" ]; then
770 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
772 uniq $ERRLIST > $ERRLIST_TARGET
776 echo >>$xml "</testsuite></testsuites>"
778 if [ -n
"$nok" ]; then