tests.sh: support absolute path to outdir
[nit.git] / tests / tests.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17
18 # This shell script compile, run and verify Nit program files
19
20 # Set lang do default to avoid failed tests because of locale
21 export LANG=C
22 export LC_ALL=C
23 export NIT_TESTING=true
24 export MNIT_SRAND=0
25
26 unset NIT_DIR
27
28 # Get the first Java lib available
29 shopt -s nullglob
30 paths=`echo /usr/lib/jvm/*/`
31 paths=($paths)
32 JAVA_HOME=${paths[0]}
33
34 paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/`
35 paths=($paths)
36 JNI_LIB_PATH=${paths[0]}
37 shopt -u nullglob
38
39 outdir="out"
40 compdir=".nit_compile"
41
42 usage()
43 {
44 e=`basename "$0"`
45 cat<<END
46 Usage: $e [options] modulenames
47 -o option Pass option to the engine
48 -v Verbose (show tests steps)
49 -h This help
50 --engine Use a specific engine (default=nitg)
51 --noskip Do not skip a test even if the .skip file matches
52 --outdir Use a specific output folder (default=out/)
53 --compdir Use a specific temporary compilation folder (default=.nit_compile)
54 END
55 }
56
57 # $1 is the pattern of the test
58 # $2 is the file to compare to
59 # the result is:
60 # 0: if the file to compare to do not exists
61 # 1: if the file match
62 # 2: if the file match with soso
63 # 3: if the file do not match
64 function compare_to_result()
65 {
66 local pattern="$1"
67 local sav="$2"
68 if [ ! -r "$sav" ]; then return 0; fi
69 test "`cat "$sav"`" = "UNDEFINED" && return 1
70 diff -u "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
71 if [ "$?" == 0 ]; then
72 return 1
73 fi
74 sed '/[Ww]arning/d;/[Ee]rror/d' "$outdir/$pattern.res" > "$outdir/$pattern.res2"
75 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "$outdir/$pattern.sav2"
76 grep '[Ee]rror' "$outdir/$pattern.res" >/dev/null && echo "Error" >> "$outdir/$pattern.res2"
77 grep '[Ee]rror' "$sav" >/dev/null && echo "Error" >> "$outdir/$pattern.sav2"
78 diff -u "$outdir/$pattern.sav2" "$outdir/$pattern.res2" > "$outdir/$pattern.diff.sav.log2"
79 if [ "$?" == 0 ]; then
80 return 2
81 else
82 return 3
83 fi
84 }
85
86 # As argument: the pattern used for the file
87 function process_result()
88 {
89 # Result
90 pattern=$1
91 description=$2
92 pack=$3
93 SAV=""
94 NSAV=""
95 FIXME=""
96 NFIXME=""
97 SOSO=""
98 NSOSO=""
99 SOSOF=""
100 NSOSOF=""
101 OLD=""
102 LIST=""
103 FIRST=""
104 echo >>$xml "<testcase classname='$pack' name='$description' time='`cat $outdir/$pattern.time.out`' timestamp='`date -Iseconds`'>"
105 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
106 for savdir in $savdirs; do
107 sav=$savdir/fixme/$pattern.res
108 compare_to_result "$pattern" "$sav"
109 case "$?" in
110 0)
111 ;; # no file
112 1)
113 OLD="$LIST"
114 FIXME="$sav"
115 LIST="$LIST $sav"
116 ;;
117 2)
118 if [ -z "$FIRST" ]; then
119 SOSOF="$sav"
120 FIRST="$sav"
121 fi
122 LIST="$LIST $sav"
123 ;;
124 3)
125 if [ -z "$FIRST" ]; then
126 NFIXME="$sav"
127 FIRST="$sav"
128 fi
129 LIST="$LIST $sav"
130 ;;
131 esac
132
133 sav=$savdir/$pattern.res
134 compare_to_result "$pattern" "$sav"
135 case "$?" in
136 0)
137 ;; # no file
138 1)
139 OLD="$LIST"
140 SAV="$sav"
141 LIST="$LIST $sav"
142 ;;
143 2)
144 if [ -z "$FIRST" ]; then
145 SOSO="$sav"
146 FIRST="$sav"
147 fi
148 LIST="$LIST $sav"
149 ;;
150 3)
151 if [ -z "$FIRST" ]; then
152 NSAV="$sav"
153 FIRST="$sav"
154 fi
155 LIST="$LIST $sav"
156 ;;
157 esac
158 done
159 OLD=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
160 grep 'NOT YET IMPLEMENTED' "$outdir/$pattern.res" >/dev/null
161 NYI="$?"
162 if [ -n "$SAV" ]; then
163 if [ -n "$OLD" ]; then
164 echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
165 echo >>$xml "<error message='ok $outdir/$pattern.res - but $OLD remains'/>"
166 remains="$remains $OLD"
167 else
168 echo "[ok] $outdir/$pattern.res $SAV"
169 fi
170 ok="$ok $pattern"
171 elif [ -n "$FIXME" ]; then
172 if [ -n "$OLD" ]; then
173 echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
174 echo >>$xml "<error message='ok $outdir/$pattern.res - but $OLD remains'/>"
175 remains="$remains $OLD"
176 else
177 echo "[fixme] $outdir/$pattern.res $FIXME"
178 echo >>$xml "<skipped/>"
179 fi
180 todos="$todos $pattern"
181 elif [ "x$NYI" = "x0" ]; then
182 echo "[todo] $outdir/$pattern.res -> not yet implemented"
183 echo >>$xml "<skipped/>"
184 todos="$todos $pattern"
185 elif [ -n "$SOSO" ]; then
186 echo "[======= soso $outdir/$pattern.res $SOSO =======]"
187 echo >>$xml "<error message='soso $outdir/$pattern.res $SOSO'/>"
188 echo >>$xml "<system-out><![CDATA["
189 cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
190 echo >>$xml "]]></system-out>"
191 nok="$nok $pattern"
192 echo "$ii" >> "$ERRLIST"
193 elif [ -n "$SOSOF" ]; then
194 echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
195 echo >>$xml "<error message='soso $outdir/$pattern.res $SOSO'/>"
196 echo >>$xml "<system-out><![CDATA["
197 cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
198 echo >>$xml "]]></system-out>"
199 nok="$nok $pattern"
200 echo "$ii" >> "$ERRLIST"
201 elif [ -n "$NSAV" ]; then
202 echo "[======= fail $outdir/$pattern.res $NSAV =======]"
203 echo >>$xml "<error message='fail $outdir/$pattern.res $NSAV'/>"
204 echo >>$xml "<system-out><![CDATA["
205 cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
206 echo >>$xml "]]></system-out>"
207 nok="$nok $pattern"
208 echo "$ii" >> "$ERRLIST"
209 elif [ -n "$NFIXME" ]; then
210 echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
211 echo >>$xml "<error message='changed $outdir/$pattern.res $NFIXME'/>"
212 echo >>$xml "<system-out><![CDATA["
213 cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
214 echo >>$xml "]]></system-out>"
215 nok="$nok $pattern"
216 echo "$ii" >> "$ERRLIST"
217 elif [ -s $outdir/$pattern.res ]; then
218 echo "[=== no sav ===] $outdir/$pattern.res is not empty"
219 echo >>$xml "<error message='no sav and not empty'/>"
220 echo >>$xml "<system-out><![CDATA["
221 cat -v >>$xml $outdir/$pattern.res
222 echo >>$xml "]]></system-out>"
223 nos="$nos $pattern"
224 echo "$ii" >> "$ERRLIST"
225 else
226 # no sav but empty res
227 echo "[0k] $outdir/$pattern.res is empty"
228 ok="$ok $pattern"
229 fi
230 if test -s $outdir/$pattern.cmp.err; then
231 echo >>$xml "<system-err><![CDATA["
232 cat -v >>$xml $outdir/$pattern.cmp.err
233 echo >>$xml "]]></system-err>"
234 fi
235 echo >>$xml "</testcase>"
236 }
237
238 need_skip()
239 {
240 test "$noskip" = true && return 1
241 if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
242 echo "=> $2: [skip]"
243 echo >>$xml "<testcase classname='$3' name='$2' timestamp='`date -Iseconds`'><skipped/></testcase>"
244 return 0
245 fi
246 if test -n "$isinterpret" && echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
247 echo "=> $2: [skip exec]"
248 echo >>$xml "<testcase classname='$3' name='$2' timestamp='`date -Iseconds`'><skipped/></testcase>"
249 return 0
250 fi
251 return 1
252 }
253
254 skip_exec()
255 {
256 test "$noskip" = true && return 1
257 if echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
258 echo -n "_ "
259 return 0
260 fi
261 return 1
262 }
263
264 skip_cc()
265 {
266 test "$noskip" = true && return 1
267 if echo "$1" | grep -f "cc.skip" >/dev/null 2>&1; then
268 return 0
269 fi
270 return 1
271 }
272
273 find_nitc()
274 {
275 name="$enginebinname"
276 recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
277 if [[ "x$recent" == "x" ]]; then
278 echo "Could not find binary for engine $engine, aborting"
279 exit 1
280 fi
281 echo "Find binary for engine $engine: $recent $OPT"
282 NITC=$recent
283 }
284
285 verbose=false
286 stop=false
287 engine=nitg
288 noskip=
289 savdirs=
290 while [ $stop = false ]; do
291 case $1 in
292 -o) OPT="$OPT $2"; shift; shift;;
293 -v) verbose=true; shift;;
294 -h) usage; exit;;
295 --engine) engine="$2"; shift; shift;;
296 --noskip) noskip=true; shift;;
297 --outdir) outdir="$2"; shift; shift;;
298 --compdir) compdir="$2"; shift; shift;;
299 *) stop=true
300 esac
301 done
302 enginebinname=$engine
303 isinterpret=
304 case $engine in
305 nitg)
306 engine=nitg-s;
307 enginebinname=nitg;
308 OPT="--separate $OPT --compile-dir $compdir"
309 ;;
310 nitg-s)
311 enginebinname=nitg;
312 OPT="--separate $OPT --compile-dir $compdir"
313 ;;
314 nitg-e)
315 enginebinname=nitg;
316 OPT="--erasure $OPT --compile-dir $compdir"
317 ;;
318 nitg-sg)
319 enginebinname=nitg;
320 OPT="--semi-global $OPT --compile-dir $compdir"
321 ;;
322 nitg-g)
323 enginebinname=nitg;
324 OPT="--global $OPT --compile-dir $compdir"
325 ;;
326 nit)
327 engine=niti
328 isinterpret=true
329 ;;
330 niti)
331 enginebinname=nit
332 isinterpret=true
333 ;;
334 nitvm)
335 isinterpret=true
336 savdirs="sav/niti/"
337 ;;
338 emscripten)
339 enginebinname=nitg
340 OPT="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"
341 savdirs="sav/nitg-sg/"
342 ;;
343 nitc)
344 echo "disabled engine $engine"
345 exit 0
346 ;;
347 *)
348 echo "unknown engine $engine"
349 exit 1
350 ;;
351 esac
352
353 savdirs="sav/$engine $savdirs sav/"
354
355 # The default nitc compiler
356 [ -z "$NITC" ] && find_nitc
357
358 # Set NIT_DIR if needed
359 [ -z "$NIT_DIR" ] && export NIT_DIR=..
360
361 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
362 TIMEOUT="timelimit -t 600"
363 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
364 TIMEOUT="timeout 600s"
365 else
366 echo "No timelimit or timeout command detected. Tests may hang :("
367 fi
368
369 # Mark to distinguish files among tests
370 # MARK=
371
372 # File where error tests are outputed
373 # Old ERRLIST is backuped
374 ERRLIST=${ERRLIST:-errlist}
375 ERRLIST_TARGET=$ERRLIST
376
377 if [ $# = 0 ]; then
378 usage;
379 exit
380 fi
381
382 # Initiate new ERRLIST
383 if [ "x$ERRLIST" = "x" ]; then
384 ERRLIST=/dev/null
385 else
386 ERRLIST=$ERRLIST.tmp
387 > "$ERRLIST"
388 fi
389
390 ok=""
391 nok=""
392 todos=""
393 xml="tests-$engine.xml"
394 echo >$xml "<testsuites><testsuite>"
395
396 # CLEAN the out directory
397 rm -rf "$outdir/" 2>/dev/null
398 mkdir "$outdir" 2>/dev/null
399
400 for ii in "$@"; do
401 if [ ! -f $ii ]; then
402 echo "File '$ii' does not exist."
403 continue
404 fi
405 f=`basename "$ii" .nit`
406
407 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|'`
408
409 # Sould we skip the file for this engine?
410 need_skip $f $f $pack && continue
411
412 tmp=${ii/../AA}
413 if [ "x$tmp" = "x$ii" ]; then
414 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
415 else
416 includes="-I alt"
417 fi
418
419 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
420 bf=`basename $i .nit`
421 ff="$outdir/$bf"
422
423 # Sould we skip the alternative for this engine?
424 need_skip $bf $bf $pack && continue
425
426 echo -n "=> $bf: "
427
428 if [ -f "$f.inputs" ]; then
429 inputs="$f.inputs"
430 export MNIT_READ_INPUT="$f.inputs"
431 else
432 inputs=/dev/null
433 export MNIT_READ_INPUT=/dev/null
434 fi
435
436 ffout="$ff.bin"
437 if [ "$engine" = "emscripten" ]; then
438 ffout="$ff.bin.js"
439 fi
440
441 if [ -n "$isinterpret" ]; then
442 cat > "$ff.bin" <<END
443 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
444 END
445 chmod +x "$ff.bin"
446 > "$ff.cmp.err"
447 > "$ff.compile.log"
448 ERR=0
449 echo 0.0 > "$ff.time.out"
450 else
451 if skip_cc "$bf"; then
452 nocc="--no-cc"
453 else
454 nocc=
455 fi
456 # Compile
457 if [ "x$verbose" = "xtrue" ]; then
458 echo ""
459 echo $NITC --no-color $OPT -o "$ffout" "$i" "$includes" $nocc
460 fi
461 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
462 /usr/bin/time --quiet -f%U -o "$ff.time.out" $TIMEOUT $NITC --no-color $OPT -o "$ffout" "$i" $includes $nocc 2> "$ff.cmp.err" > "$ff.compile.log"
463 ERR=$?
464 if [ "x$verbose" = "xtrue" ]; then
465 cat "$ff.compile.log"
466 cat >&2 "$ff.cmp.err"
467 fi
468 fi
469 if [ "$engine" = "emscripten" ]; then
470 echo > "$ff.bin" "nodejs $ffout \"\$@\""
471 chmod +x "$ff.bin"
472 if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev/null; then
473 echo " [skip] do no not imports kernel"
474 echo >>$xml "<testcase classname='$pack' name='$bf' timestamp='`date -Iseconds`'><skipped/></testcase>"
475 continue
476 fi
477 fi
478 if [ "$ERR" != 0 ]; then
479 echo -n "! "
480 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
481 process_result $bf $bf $pack
482 elif [ -n "$nocc" ]; then
483 # not compiled
484 echo -n "nocc "
485 > "$ff.res"
486 process_result $bf $bf $pack
487 elif [ -x "$ff.bin" ]; then
488 if skip_exec "$bf"; then
489 # No exec
490 > "$ff.res"
491 process_result $bf $bf $pack
492 break
493 fi
494 echo -n ". "
495 # Execute
496 args=""
497 if [ "x$verbose" = "xtrue" ]; then
498 echo ""
499 echo "NIT_NO_STACK=1 $ff.bin" $args
500 fi
501 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
502 /usr/bin/time --quiet -f%U -a -o "$ff.time.out" $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
503 mv $ff.time.out $ff.times.out
504 awk '{ SUM += $1} END { print SUM }' $ff.times.out > $ff.time.out
505
506 if [ "x$verbose" = "xtrue" ]; then
507 cat "$ff.res"
508 cat >&2 "$ff.err"
509 fi
510 if [ -f "$ff.write" ]; then
511 cat "$ff.write" >> "$ff.res"
512 elif [ -d "$ff.write" ]; then
513 LANG=C /bin/ls -F $ff.write >> "$ff.res"
514 fi
515 cp "$ff.res" "$ff.res2"
516 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
517 process_result $bf $bf $pack
518
519 if [ -f "$f.args" ]; then
520 fargs=$f.args
521 cptr=0
522 while read line; do
523 ((cptr=cptr+1))
524 args="$line"
525 bff=$bf"_args"$cptr
526 fff=$ff"_args"$cptr
527 name="$bf args $cptr"
528
529 # Sould we skip the input for this engine?
530 need_skip $bff " $name" $pack && continue
531
532 # use a specific inputs file, if required
533 if [ -f "$bff.inputs" ]; then
534 ffinputs="$bff.inputs"
535 else
536 ffinputs=$inputs
537 fi
538
539 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
540 if [ "x$verbose" = "xtrue" ]; then
541 echo ""
542 echo "NIT_NO_STACK=1 $ff.bin" $args
543 fi
544 echo -n "==> $name "
545 echo "$ff.bin $args" > "$fff.bin"
546 chmod +x "$fff.bin"
547 WRITE="$fff.write" /usr/bin/time --quiet -f%U -o "$fff.time.out" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $ffinputs > $fff.res 2>$fff.err"
548 if [ "x$verbose" = "xtrue" ]; then
549 cat "$fff.res"
550 cat >&2 "$fff.err"
551 fi
552 if [ -f "$fff.write" ]; then
553 cat "$fff.write" >> "$fff.res"
554 elif [ -d "$fff.write" ]; then
555 LANG=C /bin/ls -F $fff.write >> "$fff.res"
556 fi
557 if [ -s "$fff.err" ]; then
558 cp "$fff.res" "$fff.res2"
559 cat "$fff.err" "$fff.res2" > "$fff.res"
560 fi
561 process_result $bff " $name" $pack
562 done < $fargs
563 fi
564 elif [ -f "$ff.bin" ]; then
565 #Not executable (platform?)"
566 > "$ff.res"
567 process_result $bf "$bf" $pack
568 else
569 echo -n "! "
570 cat "$ff.cmp.err" > "$ff.res"
571 echo "Compilation error" > "$ff.res"
572 process_result $bf "$bf" $pack
573 fi
574 done
575 done
576
577 echo "engine: $engine ($enginebinname $OPT)"
578 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
579
580 if [ -n "$nok" ]; then
581 echo "fail: $nok"
582 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
583 fi
584 if [ -n "$nos" ]; then
585 echo "no sav: $nos"
586 fi
587 if [ -n "$todos" ]; then
588 echo "todo/fixme: $todos"
589 fi
590 if [ -n "$remains" ]; then
591 echo "sav that remains: $remains"
592 fi
593
594 # write $ERRLIST
595 if [ "x$ERRLIST" != "x" ]; then
596 if [ -f "$ERRLIST_TARGET" ]; then
597 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
598 fi
599 uniq $ERRLIST > $ERRLIST_TARGET
600 rm $ERRLIST
601 fi
602
603 echo >>$xml "</testsuite></testsuites>"
604
605 if [ -n "$nok" ]; then
606 exit 1
607 else
608 exit 0
609 fi