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