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