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