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