tests: [soso] to filter out warnings and errors
[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
23 usage()
24 {
25 e=`basename "$0"`
26 cat<<END
27 Usage: $e [options] modulenames
28 -o option Pass option to nitc
29 -v Verbose (show tests steps)
30 -h This help
31 END
32 }
33
34 # As argument: the pattern used for the file
35 function process_result()
36 {
37 # Result
38 pattern=$1
39 SAV=""
40 FAIL=""
41 SOSO=""
42 if [ -r "sav/$pattern.sav" ]; then
43 diff -u "out/$pattern.res" "sav/$pattern.sav" > "out/$pattern.diff.sav.log"
44 if [ "$?" == 0 ]; then
45 SAV=OK
46 else
47 SAV=NOK
48 fi
49 sed '/[Ww]arning/d;/[Ee]rror/d' "out/$pattern.res" > "out/$pattern.res2"
50 sed '/[Ww]arning/d;/[Ee]rror/d' "sav/$pattern.sav" > "out/$pattern.sav2"
51 grep '[Ee]rror' "out/$pattern.res" >/dev/null && echo "Error" >> "out/$pattern.res2"
52 grep '[Ee]rror' "sav/$pattern.sav" >/dev/null && echo "Error" >> "out/$pattern.sav2"
53 diff -u "out/$pattern.res2" "out/$pattern.sav2" > "out/$pattern.diff.sav2.log"
54 if [ "$?" == 0 ]; then
55 SOSO=OK
56 else
57 SOSO=NOK
58 fi
59 fi
60 if [ -r "sav/$pattern.fail" ]; then
61 diff -u "out/$pattern.res" "sav/$pattern.fail" > "out/$pattern.diff.fail.log"
62 if [ "$?" == 0 ]; then
63 FAIL=OK
64 else
65 FAIL=NOK
66 fi
67 fi
68 if [ "x$SAV" = "xOK" ]; then
69 if [ "x$FAIL" = "x" ]; then
70 echo "[ok] out/$pattern.res"
71 else
72 echo "[ok] out/$pattern.res - but sav/$pattern.fail remains!"
73 fi
74 ok="$ok $pattern"
75 elif [ "x$FAIL" = "xOK" ]; then
76 echo "[fail] out/$pattern.res"
77 ok="$ok $pattern"
78 elif [ "x$SOSO" = "xOK" ]; then
79 echo "[soso] out/$pattern.res sav/$pattern.sav"
80 ok="$ok $pattern"
81 elif [ "x$SAV" = "xNOK" ]; then
82 echo "[======= fail out/$pattern.res sav/$pattern.sav =======]"
83 nok="$nok $pattern"
84 echo "$ii" >> "$ERRLIST"
85 elif [ "x$FAIL" = "xNOK" ]; then
86 echo "[======= changed out/$pattern.res sav/$pattern.fail ======]"
87 nok="$nok $pattern"
88 echo "$ii" >> "$ERRLIST"
89 else
90 echo "[=== no sav ===] out/$pattern.res"
91 nos="$nos $pattern"
92 fi
93 }
94
95 find_nitc()
96 {
97 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
98 if [[ "x$recent" == "x" ]]; then
99 echo 'Could not find nitc, aborting'
100 exit 1
101 fi
102 echo 'Using nitc from: '$recent
103 NITC=$recent
104 }
105
106 make_alts0()
107 {
108 ii="$1"
109 xalt="$2"
110 fs=""
111 for alt in `sed -n "s/.*#!*\($xalt[0-9]*\)#.*/\1/p" "$ii" | sort -u`; do
112 f=`basename "$ii" .nit`
113 d=`dirname "$ii"`
114 ff="$f"
115 i="$ii"
116
117 if [ "x$alt" != "x" ]; then
118 test -d alt || mkdir -p alt
119 i="alt/${f}_$alt.nit"
120 ff="${ff}_$alt"
121 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
122 fi
123 ff="$ff$MARK"
124 fs="$fs $i"
125 done
126 echo "$fs"
127 }
128 make_alts()
129 {
130 ii="$1"
131 fs="$1"
132 for xalt in `sed -n 's/.*#!*\([0-9]*alt\)[0-9]*#.*/\1/p' "$ii" | sort -u`; do
133 fs2=""
134 for f in $fs; do
135 fs2="$fs2 `make_alts0 $f $xalt`"
136 done
137 fs="$fs $fs2"
138 done
139 echo "$fs"
140 }
141
142 # The default nitc compiler
143 [ -z "$NITC" ] && find_nitc
144
145 # Set NIT_DIR if needed
146 [ -z "$NIT_DIR" ] && export NIT_DIR=..
147
148 verbose=false
149 stop=false
150 while [ $stop = false ]; do
151 case $1 in
152 -o) OPT="$OPT $2"; shift; shift;;
153 -v) verbose=true; shift;;
154 -h) usage; exit;;
155 *) stop=true
156 esac
157 done
158
159 # Mark to distinguish files among tests
160 # MARK=
161
162 # File where error tests are outputed
163 # Old ERRLIST is backuped
164 ERRLIST=${ERRLIST:-errlist}
165 ERRLIST_TARGET=$ERRLIST
166
167 if [ $# = 0 ]; then
168 usage;
169 exit
170 fi
171
172 # Initiate new ERRLIST
173 if [ "x$ERRLIST" = "x" ]; then
174 ERRLIST=/dev=null
175 else
176 ERRLIST=$ERRLIST.tmp
177 > "$ERRLIST"
178 fi
179
180 ok=""
181 nok=""
182
183 # CLEAN the out directory
184 rm -rf out/ 2>/dev/null
185 mkdir out 2>/dev/null
186
187 for ii in "$@"; do
188 if [ ! -f $ii ]; then
189 echo "File '$ii' does not exist."
190 continue
191 fi
192
193 tmp=${ii/../AA}
194 if [ "x$tmp" = "x$ii" ]; then
195 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
196 else
197 includes="-I alt"
198 fi
199
200 f=`basename "$ii" .nit`
201 for i in `make_alts $ii`; do
202 bf=`basename $i .nit`
203 ff="out/$bf"
204 echo -n "=> $bf: "
205
206 # Compile
207 if [ "x$verbose" = "xtrue" ]; then
208 echo ""
209 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
210 fi
211 $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
212 ERR=$?
213 if [ "x$verbose" = "xtrue" ]; then
214 cat "$ff.compile.log"
215 cat >&2 "$ff.cmp.err"
216 fi
217 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
218 if [ "$ERR" != 0 ]; then
219 echo -n "! "
220 cp "$ff.cmp.err" "$ff.res"
221 process_result $bf
222 elif [ -x "./$ff.bin" ]; then
223 cp "$ff.cmp.err" "$ff.res"
224 echo -n ". "
225 # Execute
226 args=""
227 if [ "x$verbose" = "xtrue" ]; then
228 echo ""
229 echo "NIT_NO_STACK=1 ./$ff.bin" $args
230 fi
231 if [ -f "$f.inputs" ]; then
232 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" >> "$ff.res" 2>"$ff.err"
233 else
234 NIT_NO_STACK=1 "./$ff.bin" $args >> "$ff.res" 2>"$ff.err"
235 fi
236 if [ "x$verbose" = "xtrue" ]; then
237 cat "$ff.res"
238 cat >&2 "$ff.err"
239 fi
240 if [ -f "$ff.write" ]; then
241 cat "$ff.write" >> "$ff.res"
242 elif [ -d "$ff.write" ]; then
243 LANG=C /bin/ls -F $ff.write >> "$ff.res"
244 fi
245 if [ -s "$ff.err" ]; then
246 cat "$ff.err" >> "$ff.res"
247 fi
248 process_result $bf
249
250 if [ -f "$f.args" ]; then
251 fargs=$f.args
252 cptr=0
253 while read line; do
254 ((cptr=cptr+1))
255 args=$line
256 bff=$bf"_args"$cptr
257 fff=$ff"_args"$cptr
258 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
259 if [ "x$verbose" = "xtrue" ]; then
260 echo ""
261 echo "NIT_NO_STACK=1 ./$ff.bin" $args
262 fi
263 echo -n "==> args #"$cptr " "
264 if [ -f "$f.inputs" ]; then
265 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
266 else
267 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args > $fff.res 2>$fff.err"
268 fi
269 if [ "x$verbose" = "xtrue" ]; then
270 cat "$fff.res"
271 cat >&2 "$fff.err"
272 fi
273 if [ -f "$fff.write" ]; then
274 cat "$fff.write" >> "$fff.res"
275 elif [ -d "$fff.write" ]; then
276 LANG=C /bin/ls -F $fff.write >> "$fff.res"
277 fi
278 if [ -s "$fff.err" ]; then
279 cat "$fff.err" >> "$fff.res"
280 fi
281 process_result $bff
282 done < $fargs
283 fi
284 else
285 echo -n "! "
286 echo "Compilation error" > "$ff.res"
287 process_result $bf
288 fi
289 done
290 done
291
292 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
293
294 if [ -n "$nok" ]; then
295 echo "fail: $nok"
296 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
297 fi
298 if [ -n "$nos" ]; then
299 echo "no sav: $nos"
300 fi
301
302 # write $ERRLIST
303 if [ "x$ERRLIST" != "x" ]; then
304 if [ -x "$ERRLIST_TARGET" ]; then
305 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
306 fi
307 mv $ERRLIST $ERRLIST_TARGET
308 fi
309
310 if [ -n "$nok" ]; then
311 exit 1
312 else
313 exit 0
314 fi