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