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