tests: niti fully skips tests in exec.skip
[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 echo >>$xml "<skipped/>"
160 fi
161 todos="$todos $pattern"
162 elif [ -n "$SOSO" ]; then
163 if [ -n "$tap" ]; then
164 echo "ok - $description # SOSO"
165 else
166 echo "[soso] out/$pattern.res $SOSO"
167 fi
168 ok="$ok $pattern"
169 elif [ "x$NYI" = "x0" ]; then
170 if [ -n "$tap" ]; then
171 echo "not ok - $description # TODO not yet implemented"
172 else
173 echo "[todo] out/$pattern.res -> not yet implemented"
174 echo >>$xml "<skipped/>"
175 fi
176 todos="$todos $pattern"
177 elif [ -n "$SOSOF" ]; then
178 if [ -n "$tap" ]; then
179 echo "not ok - $description # TODO SOSO expected failure"
180 else
181 echo "[fixme soso] out/$pattern.res $SOSOF"
182 echo >>$xml "<skipped/>"
183 fi
184 todos="$todos $pattern"
185 elif [ -n "$NSAV" ]; then
186 if [ -n "$tap" ]; then
187 echo "not ok - $description"
188 else
189 echo "[======= fail out/$pattern.res $NSAV =======]"
190 fi
191 echo >>$xml "<error message='fail out/$pattern.res $NSAV'/>"
192 echo >>$xml "<system-out><![CDATA["
193 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
194 echo >>$xml "]]></system-out>"
195 nok="$nok $pattern"
196 echo "$ii" >> "$ERRLIST"
197 elif [ -n "$NFIXME" ]; then
198 if [ -n "$tap" ]; then
199 echo "not ok - $description"
200 else
201 echo "[======= changed out/$pattern.res $NFIXME ======]"
202 fi
203 echo >>$xml "<error message='changed out/$pattern.res $NFIXME'/>"
204 echo >>$xml "<system-out><![CDATA["
205 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
206 echo >>$xml "]]></system-out>"
207 nok="$nok $pattern"
208 echo "$ii" >> "$ERRLIST"
209 else
210 if [ -n "$tap" ]; then
211 echo "ok - $description # skip no sav"
212 else
213 echo "[=== no sav ===] out/$pattern.res"
214 fi
215 echo >>$xml "<skipped/>"
216 echo >>$xml "<system-out><![CDATA["
217 cat -v >>$xml out/$pattern.res
218 echo >>$xml "]]></system-out>"
219 nos="$nos $pattern"
220 fi
221 if test -s out/$pattern.cmp.err; then
222 echo >>$xml "<system-err><![CDATA["
223 cat -v >>$xml out/$pattern.cmp.err
224 echo >>$xml "]]></system-err>"
225 fi
226 echo >>$xml "</testcase>"
227 }
228
229 need_skip()
230 {
231 test "$noskip" = true && return 1
232 if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
233 ((tapcount=tapcount+1))
234 if [ -n "$tap" ]; then
235 echo "ok - $2 # skip"
236 else
237 echo "=> $2: [skip]"
238 fi
239 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
240 return 0
241 fi
242 if test $engine = niti && echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
243 ((tapcount=tapcount+1))
244 if [ -n "$tap" ]; then
245 echo "ok - $2 # skip"
246 else
247 echo "=> $2: [skip exec]"
248 fi
249 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
250 return 0
251 fi
252 return 1
253 }
254
255 skip_exec()
256 {
257 test "$noskip" = true && return 1
258 if echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
259 echo -n "_ "
260 return 0
261 fi
262 return 1
263 }
264
265 find_nitc()
266 {
267 ((tapcount=tapcount+1))
268 name="$enginebinname"
269 recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
270 if [[ "x$recent" == "x" ]]; then
271 if [ -n "$tap" ]; then
272 echo "not ok - find binary for $engine"
273 echo "Bail out! Could not find binary for engine $engine, aborting"
274 else
275 echo "Could not find binary for engine $engine, aborting"
276 fi
277 exit 1
278 fi
279 if [ -n "$tap" ]; then
280 echo "ok - find binary for $engine: $recent $OPT"
281 else
282 echo "Find binary for engine $engine: $recent $OPT"
283 fi
284 NITC=$recent
285 }
286
287 verbose=false
288 stop=false
289 tapcount=0
290 engine=nitg
291 noskip=
292 while [ $stop = false ]; do
293 case $1 in
294 -o) OPT="$OPT $2"; shift; shift;;
295 -v) verbose=true; shift;;
296 -h) usage; exit;;
297 --tap) tap=true; shift;;
298 --engine) engine="$2"; shift; shift;;
299 --noskip) noskip=true; shift;;
300 --soso) soso=true; shift;;
301 --nososo) nososo=true; shift;;
302 *) stop=true
303 esac
304 done
305 enginebinname=$engine
306 case $engine in
307 nitg)
308 engine=nitg-s;
309 enginebinname=nitg;
310 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
311 OPT="--separate $OPT"
312 ;;
313 nitg-s)
314 enginebinname=nitg;
315 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
316 OPT="--separate $OPT"
317 ;;
318 nitg-e)
319 enginebinname=nitg;
320 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
321 OPT="--erasure $OPT"
322 ;;
323 nitg-g)
324 enginebinname=nitg;
325 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
326 OPT="--global $OPT"
327 ;;
328 nit)
329 engine=niti
330 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
331 ;;
332 niti)
333 enginebinname=nit
334 savdirs="sav/$engine/fixme/ sav/$engine/ sav/fixme/ sav/"
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 # The default nitc compiler
347 [ -z "$NITC" ] && find_nitc
348
349 # Set NIT_DIR if needed
350 [ -z "$NIT_DIR" ] && export NIT_DIR=..
351
352 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
353 TIMEOUT="timelimit -t 600"
354 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
355 TIMEOUT="timeout 600s"
356 else
357 echo "No timelimit or timeout command detected. Tests may hang :("
358 fi
359
360 # Mark to distinguish files among tests
361 # MARK=
362
363 # File where error tests are outputed
364 # Old ERRLIST is backuped
365 ERRLIST=${ERRLIST:-errlist}
366 ERRLIST_TARGET=$ERRLIST
367
368 if [ $# = 0 ]; then
369 usage;
370 exit
371 fi
372
373 # Initiate new ERRLIST
374 if [ "x$ERRLIST" = "x" ]; then
375 ERRLIST=/dev=null
376 else
377 ERRLIST=$ERRLIST.tmp
378 > "$ERRLIST"
379 fi
380
381 ok=""
382 nok=""
383 todos=""
384 xml="tests-$engine.xml"
385 echo >$xml "<testsuites><testsuite>"
386
387 # CLEAN the out directory
388 rm -rf out/ 2>/dev/null
389 mkdir out 2>/dev/null
390
391 for ii in "$@"; do
392 if [ ! -f $ii ]; then
393 echo "File '$ii' does not exist."
394 continue
395 fi
396 f=`basename "$ii" .nit`
397
398 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|'`
399
400 # Sould we skip the file for this engine?
401 need_skip $f $f $pack && continue
402
403 tmp=${ii/../AA}
404 if [ "x$tmp" = "x$ii" ]; then
405 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
406 else
407 includes="-I alt"
408 fi
409
410 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
411 bf=`basename $i .nit`
412 ff="out/$bf"
413
414 # Sould we skip the alternative for this engine?
415 need_skip $bf $bf $pack && continue
416
417 test -z "$tap" && echo -n "=> $bf: "
418
419 if [ -f "$f.inputs" ]; then
420 inputs="$f.inputs"
421 else
422 inputs=/dev/null
423 fi
424
425 if [ "$engine" = "niti" ]; then
426 cat > "./$ff.bin" <<END
427 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
428 END
429 chmod +x "./$ff.bin"
430 > "$ff.cmp.err"
431 > "$ff.compile.log"
432 ERR=0
433 else
434 # Compile
435 if [ "x$verbose" = "xtrue" ]; then
436 echo ""
437 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
438 fi
439 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
440 $TIMEOUT $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
441 ERR=$?
442 if [ "x$verbose" = "xtrue" ]; then
443 cat "$ff.compile.log"
444 cat >&2 "$ff.cmp.err"
445 fi
446 fi
447 if [ "$ERR" != 0 ]; then
448 test -z "$tap" && echo -n "! "
449 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
450 process_result $bf $bf $pack
451 elif skip_exec "$bf"; then
452 # No exec
453 > "$ff.res"
454 process_result $bf $bf $pack
455 elif [ -x "./$ff.bin" ]; then
456 test -z "$tap" && echo -n ". "
457 # Execute
458 args=""
459 if [ "x$verbose" = "xtrue" ]; then
460 echo ""
461 echo "NIT_NO_STACK=1 ./$ff.bin" $args
462 fi
463 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
464 $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
465 if [ "x$verbose" = "xtrue" ]; then
466 cat "$ff.res"
467 cat >&2 "$ff.err"
468 fi
469 if [ -f "$ff.write" ]; then
470 cat "$ff.write" >> "$ff.res"
471 elif [ -d "$ff.write" ]; then
472 LANG=C /bin/ls -F $ff.write >> "$ff.res"
473 fi
474 cp "$ff.res" "$ff.res2"
475 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
476 process_result $bf $bf $pack
477
478 if [ -f "$f.args" ]; then
479 fargs=$f.args
480 cptr=0
481 while read line; do
482 ((cptr=cptr+1))
483 args="$line"
484 bff=$bf"_args"$cptr
485 fff=$ff"_args"$cptr
486 name="$bf args $cptr"
487
488 # Sould we skip the input for this engine?
489 need_skip $bff " $name" $pack && continue
490
491 # use a specific inputs file, if required
492 if [ -f "$bff.inputs" ]; then
493 ffinputs="$bff.inputs"
494 else
495 ffinputs=$inputs
496 fi
497
498 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
499 if [ "x$verbose" = "xtrue" ]; then
500 echo ""
501 echo "NIT_NO_STACK=1 ./$ff.bin" $args
502 fi
503 test -z "$tap" && echo -n "==> $name "
504 echo "./$ff.bin $args" > "./$fff.bin"
505 chmod +x "./$fff.bin"
506 WRITE="$fff.write" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $ffinputs > $fff.res 2>$fff.err"
507 if [ "x$verbose" = "xtrue" ]; then
508 cat "$fff.res"
509 cat >&2 "$fff.err"
510 fi
511 if [ -f "$fff.write" ]; then
512 cat "$fff.write" >> "$fff.res"
513 elif [ -d "$fff.write" ]; then
514 LANG=C /bin/ls -F $fff.write >> "$fff.res"
515 fi
516 if [ -s "$fff.err" ]; then
517 cp "$fff.res" "$fff.res2"
518 cat "$fff.err" "$fff.res2" > "$fff.res"
519 fi
520 process_result $bff " $name" $pack
521 done < $fargs
522 fi
523 elif [ -f "./$ff.bin" ]; then
524 echo "Not executable (platform?)" > "$ff.res"
525 process_result $bf "$bf" $pack
526 else
527 test -z "$tap" && echo -n "! "
528 cat "$ff.cmp.err" > "$ff.res"
529 echo "Compilation error" > "$ff.res"
530 process_result $bf "$bf" $pack
531 fi
532 done
533 done
534
535 if [ -n "$tap" ]; then
536 echo "1..$tapcount"
537 echo "# ok:" `echo $ok | wc -w`
538 echo "# not ok:" `echo $nok | wc -w`
539 echo "# no sav:" `echo $nos | wc -w`
540 echo "# todo/fixme:" `echo $todos | wc -w`
541 echo "# of sav that remains:" `echo $remains | wc -w`
542 exit
543 fi
544
545 echo "engine: $engine ($enginebinname $OPT)"
546 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
547
548 if [ -n "$nok" ]; then
549 echo "fail: $nok"
550 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
551 fi
552 if [ -n "$nos" ]; then
553 echo "no sav: $nos"
554 fi
555 if [ -n "$todos" ]; then
556 echo "todo/fixme: $todos"
557 fi
558 if [ -n "$remains" ]; then
559 echo "sav that remains: $remains"
560 fi
561
562 # write $ERRLIST
563 if [ "x$ERRLIST" != "x" ]; then
564 if [ -x "$ERRLIST_TARGET" ]; then
565 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
566 fi
567 mv $ERRLIST $ERRLIST_TARGET
568 fi
569
570 echo >>$xml "</testsuite></testsuites>"
571
572 if [ -n "$nok" ]; then
573 exit 1
574 else
575 exit 0
576 fi