tests: modifies tests.sh to execute code using the jvm module
[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 --tap Produce TAP output
46 --engine Use a specific engine (default=nitg)
47 --noskip Do not skip a test even if the .skip file matches
48 --[no]soso Force enable (or disable) SOSO
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 diff -u "$sav" "out/$pattern.res" > "out/$pattern.diff.sav.log"
65 if [ "$?" == 0 ]; then
66 return 1
67 fi
68 [ -z "$soso" ] && return 3
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.log"
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 ((tapcount=tapcount+1))
85 # Result
86 pattern=$1
87 description=$2
88 pack=$3
89 SAV=""
90 NSAV=""
91 FIXME=""
92 NFIXME=""
93 SOSO=""
94 NSOSO=""
95 SOSOF=""
96 NSOSOF=""
97 OLD=""
98 LIST=""
99 FIRST=""
100 echo >>$xml "<testcase classname='$pack' name='$description'>"
101 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
102 for savdir in $savdirs; do
103 sav=$savdir/$pattern.res
104 compare_to_result "$pattern" "$sav"
105
106 case "$? $sav" in
107 0*)
108 continue;; # no file
109 1*/fixme/*)
110 OLD="$LIST"
111 FIXME="$sav"
112 ;;
113 1*)
114 OLD="$LIST"
115 SAV="$sav"
116 ;;
117 2*/fixme/*)
118 SOSOF="$sav" ;;
119 2*)
120 SOSO="$sav" ;;
121 3*/fixme/*)
122 if [ -z "$FIRST" ]; then
123 NFIXME="$sav"
124 FIRST="$sav"
125 fi
126 ;;
127 3*)
128 if [ -z "$FIRST" ]; then
129 NSAV="$sav"
130 FIRST="$sav"
131 fi
132 ;;
133 esac
134 LIST="$LIST $sav"
135 done
136 OLD=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
137 grep 'NOT YET IMPLEMENTED' "out/$pattern.res" >/dev/null
138 NYI="$?"
139 if [ -n "$SAV" ]; then
140 if [ -n "$tap" ]; then
141 echo "ok - $description"
142 elif [ -n "$OLD" ]; then
143 echo "[*ok*] out/$pattern.res $SAV - but $OLD remains!"
144 echo >>$xml "<error message='ok out/$pattern.res - but $OLD remains'/>"
145 remains="$remains $OLD"
146 else
147 echo "[ok] out/$pattern.res $SAV"
148 fi
149 ok="$ok $pattern"
150 elif [ -n "$FIXME" ]; then
151 if [ -n "$tap" ]; then
152 echo "not ok - $description # TODO expected failure"
153 elif [ -n "$OLD" ]; then
154 echo "[*fixme*] out/$pattern.res $FIXME - but $OLD remains!"
155 echo >>$xml "<error message='ok out/$pattern.res - but $OLD remains'/>"
156 remains="$remains $OLD"
157 else
158 echo "[fixme] out/$pattern.res $FIXME"
159 fi
160 todos="$todos $pattern"
161 elif [ -n "$SOSO" ]; then
162 if [ -n "$tap" ]; then
163 echo "ok - $description # SOSO"
164 else
165 echo "[soso] out/$pattern.res $SOSO"
166 fi
167 ok="$ok $pattern"
168 elif [ "x$NYI" = "x0" ]; then
169 if [ -n "$tap" ]; then
170 echo "not ok - $description # TODO not yet implemented"
171 else
172 echo "[todo] out/$pattern.res -> not yet implemented"
173 fi
174 todos="$todos $pattern"
175 elif [ -n "$SOSOF" ]; then
176 if [ -n "$tap" ]; then
177 echo "not ok - $description # TODO SOSO expected failure"
178 else
179 echo "[fixme soso] out/$pattern.res $SOSOF"
180 fi
181 todos="$todos $pattern"
182 elif [ -n "$NSAV" ]; then
183 if [ -n "$tap" ]; then
184 echo "not ok - $description"
185 else
186 echo "[======= fail out/$pattern.res $NSAV =======]"
187 fi
188 echo >>$xml "<error message='fail out/$pattern.res $NSAV'/>"
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 "$NFIXME" ]; then
195 if [ -n "$tap" ]; then
196 echo "not ok - $description"
197 else
198 echo "[======= changed out/$pattern.res $NFIXME ======]"
199 fi
200 echo >>$xml "<error message='changed out/$pattern.res $NFIXME'/>"
201 echo >>$xml "<system-out><![CDATA["
202 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
203 echo >>$xml "]]></system-out>"
204 nok="$nok $pattern"
205 echo "$ii" >> "$ERRLIST"
206 else
207 if [ -n "$tap" ]; then
208 echo "ok - $description # skip no sav"
209 else
210 echo "[=== no sav ===] out/$pattern.res"
211 fi
212 echo >>$xml "<skipped/>"
213 echo >>$xml "<system-out><![CDATA["
214 cat -v >>$xml out/$pattern.res
215 echo >>$xml "]]></system-out>"
216 nos="$nos $pattern"
217 fi
218 if test -s out/$pattern.cmp.err; then
219 echo >>$xml "<system-err><![CDATA["
220 cat -v >>$xml out/$pattern.cmp.err
221 echo >>$xml "]]></system-err>"
222 fi
223 echo >>$xml "</testcase>"
224 }
225
226 need_skip()
227 {
228 test "$noskip" = true && return 1
229 if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
230 ((tapcount=tapcount+1))
231 if [ -n "$tap" ]; then
232 echo "ok - $2 # skip"
233 else
234 echo "=> $2: [skip]"
235 fi
236 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
237 return 0
238 fi
239 return 1
240 }
241
242 find_nitc()
243 {
244 ((tapcount=tapcount+1))
245 name="$enginebinname"
246 recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
247 if [[ "x$recent" == "x" ]]; then
248 if [ -n "$tap" ]; then
249 echo "not ok - find binary for $engine"
250 echo "Bail out! Could not find binary for engine $engine, aborting"
251 else
252 echo "Could not find binary for engine $engine, aborting"
253 fi
254 exit 1
255 fi
256 if [ -n "$tap" ]; then
257 echo "ok - find binary for $engine: $recent $OPT"
258 else
259 echo "Find binary for engine $engine: $recent $OPT"
260 fi
261 NITC=$recent
262 }
263
264 verbose=false
265 stop=false
266 tapcount=0
267 engine=nitg
268 noskip=
269 while [ $stop = false ]; do
270 case $1 in
271 -o) OPT="$OPT $2"; shift; shift;;
272 -v) verbose=true; shift;;
273 -h) usage; exit;;
274 --tap) tap=true; shift;;
275 --engine) engine="$2"; shift; shift;;
276 --noskip) noskip=true; shift;;
277 --soso) soso=true; shift;;
278 --nososo) nososo=true; shift;;
279 *) stop=true
280 esac
281 done
282 enginebinname=$engine
283 case $engine in
284 nitg)
285 engine=nitg-s;
286 enginebinname=nitg;
287 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
288 OPT="--separate $OPT"
289 ;;
290 nitg-s)
291 enginebinname=nitg;
292 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
293 OPT="--separate $OPT"
294 ;;
295 nitg-e)
296 enginebinname=nitg;
297 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
298 OPT="--erasure $OPT"
299 ;;
300 nitg-g)
301 enginebinname=nitg;
302 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
303 OPT="--global $OPT"
304 ;;
305 nit)
306 engine=niti
307 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
308 ;;
309 niti)
310 enginebinname=nit
311 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
312 ;;
313 nitc)
314 echo "disabled engine $engine"
315 exit 0
316 ;;
317 *)
318 echo "unknown engine $engine"
319 exit 1
320 ;;
321 esac
322
323 # The default nitc compiler
324 [ -z "$NITC" ] && find_nitc
325
326 # Set NIT_DIR if needed
327 [ -z "$NIT_DIR" ] && export NIT_DIR=..
328
329 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
330 TIMEOUT="timelimit -t 600"
331 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
332 TIMEOUT="timeout 600s"
333 else
334 echo "No timelimit or timeout command detected. Tests may hang :("
335 fi
336
337 # Mark to distinguish files among tests
338 # MARK=
339
340 # File where error tests are outputed
341 # Old ERRLIST is backuped
342 ERRLIST=${ERRLIST:-errlist}
343 ERRLIST_TARGET=$ERRLIST
344
345 if [ $# = 0 ]; then
346 usage;
347 exit
348 fi
349
350 # Initiate new ERRLIST
351 if [ "x$ERRLIST" = "x" ]; then
352 ERRLIST=/dev=null
353 else
354 ERRLIST=$ERRLIST.tmp
355 > "$ERRLIST"
356 fi
357
358 ok=""
359 nok=""
360 todos=""
361 xml="tests-$engine.xml"
362 echo >$xml "<testsuites><testsuite>"
363
364 # CLEAN the out directory
365 rm -rf out/ 2>/dev/null
366 mkdir out 2>/dev/null
367
368 for ii in "$@"; do
369 if [ ! -f $ii ]; then
370 echo "File '$ii' does not exist."
371 continue
372 fi
373 f=`basename "$ii" .nit`
374
375 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|'`
376
377 # Sould we skip the file for this engine?
378 need_skip $f $f $pack && continue
379
380 tmp=${ii/../AA}
381 if [ "x$tmp" = "x$ii" ]; then
382 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
383 else
384 includes="-I alt"
385 fi
386
387 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
388 bf=`basename $i .nit`
389 ff="out/$bf"
390
391 # Sould we skip the alternative for this engine?
392 need_skip $bf $bf $pack && continue
393
394 test -z "$tap" && echo -n "=> $bf: "
395
396 if [ -f "$f.inputs" ]; then
397 inputs="$f.inputs"
398 else
399 inputs=/dev/null
400 fi
401
402 if [ "$engine" = "niti" ]; then
403 cat > "./$ff.bin" <<END
404 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
405 END
406 chmod +x "./$ff.bin"
407 > "$ff.cmp.err"
408 > "$ff.compile.log"
409 ERR=0
410 else
411 # Compile
412 if [ "x$verbose" = "xtrue" ]; then
413 echo ""
414 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
415 fi
416 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
417 $TIMEOUT $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
418 ERR=$?
419 if [ "x$verbose" = "xtrue" ]; then
420 cat "$ff.compile.log"
421 cat >&2 "$ff.cmp.err"
422 fi
423 fi
424 if [ "$ERR" != 0 ]; then
425 test -z "$tap" && echo -n "! "
426 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
427 process_result $bf $bf $pack
428 elif [ -x "./$ff.bin" ]; then
429 test -z "$tap" && echo -n ". "
430 # Execute
431 args=""
432 if [ "x$verbose" = "xtrue" ]; then
433 echo ""
434 echo "NIT_NO_STACK=1 ./$ff.bin" $args
435 fi
436 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
437 $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
438 if [ "x$verbose" = "xtrue" ]; then
439 cat "$ff.res"
440 cat >&2 "$ff.err"
441 fi
442 if [ -f "$ff.write" ]; then
443 cat "$ff.write" >> "$ff.res"
444 elif [ -d "$ff.write" ]; then
445 LANG=C /bin/ls -F $ff.write >> "$ff.res"
446 fi
447 cp "$ff.res" "$ff.res2"
448 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
449 process_result $bf $bf $pack
450
451 if [ -f "$f.args" ]; then
452 fargs=$f.args
453 cptr=0
454 while read line; do
455 ((cptr=cptr+1))
456 args="$line"
457 bff=$bf"_args"$cptr
458 fff=$ff"_args"$cptr
459 name="$bf args $cptr"
460
461 # Sould we skip the input for this engine?
462 need_skip $bff " $name" $pack && continue
463
464 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
465 if [ "x$verbose" = "xtrue" ]; then
466 echo ""
467 echo "NIT_NO_STACK=1 ./$ff.bin" $args
468 fi
469 test -z "$tap" && echo -n "==> $name "
470 echo "./$ff.bin $args" > "./$fff.bin"
471 chmod +x "./$fff.bin"
472 WRITE="$fff.write" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $inputs > $fff.res 2>$fff.err"
473 if [ "x$verbose" = "xtrue" ]; then
474 cat "$fff.res"
475 cat >&2 "$fff.err"
476 fi
477 if [ -f "$fff.write" ]; then
478 cat "$fff.write" >> "$fff.res"
479 elif [ -d "$fff.write" ]; then
480 LANG=C /bin/ls -F $fff.write >> "$fff.res"
481 fi
482 if [ -s "$fff.err" ]; then
483 cp "$fff.res" "$fff.res2"
484 cat "$fff.err" "$fff.res2" > "$fff.res"
485 fi
486 process_result $bff " $name" $pack
487 done < $fargs
488 fi
489 else
490 test -z "$tap" && echo -n "! "
491 cat "$ff.cmp.err" > "$ff.res"
492 echo "Compilation error" > "$ff.res"
493 process_result $bf "$bf" $pack
494 fi
495 done
496 done
497
498 if [ -n "$tap" ]; then
499 echo "1..$tapcount"
500 echo "# ok:" `echo $ok | wc -w`
501 echo "# not ok:" `echo $nok | wc -w`
502 echo "# no sav:" `echo $nos | wc -w`
503 echo "# todo/fixme:" `echo $todos | wc -w`
504 echo "# of sav that remains:" `echo $remains | wc -w`
505 exit
506 fi
507
508 echo "engine: $engine ($enginebinname $OPT)"
509 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
510
511 if [ -n "$nok" ]; then
512 echo "fail: $nok"
513 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
514 fi
515 if [ -n "$nos" ]; then
516 echo "no sav: $nos"
517 fi
518 if [ -n "$todos" ]; then
519 echo "todo/fixme: $todos"
520 fi
521 if [ -n "$remains" ]; then
522 echo "sav that remains: $remains"
523 fi
524
525 # write $ERRLIST
526 if [ "x$ERRLIST" != "x" ]; then
527 if [ -x "$ERRLIST_TARGET" ]; then
528 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
529 fi
530 mv $ERRLIST $ERRLIST_TARGET
531 fi
532
533 echo >>$xml "</testsuite></testsuites>"
534
535 if [ -n "$nok" ]; then
536 exit 1
537 else
538 exit 0
539 fi