tools: don't print stack for tests that fail, fix saves
[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 if [ -r "sav/$pattern.sav" ]; then
42 diff -u "$pattern.res" "sav/$pattern.sav" > "$pattern.diff.sav.log"
43 if [ "$?" == 0 ]; then
44 SAV=OK
45 else
46 SAV=NOK
47 fi
48 fi
49 if [ -r "sav/$pattern.fail" ]; then
50 diff -u "$pattern.res" "sav/$pattern.fail" > "$pattern.diff.fail.log"
51 if [ "$?" == 0 ]; then
52 FAIL=OK
53 else
54 FAIL=NOK
55 fi
56 fi
57 if [ "x$SAV" = "xOK" ]; then
58 if [ "x$FAIL" = "x" ]; then
59 echo "[ok] $pattern.res"
60 else
61 echo "[ok] $pattern.res - but sav/$pattern.fail remains!"
62 fi
63 ok="$ok $pattern"
64 elif [ "x$FAIL" = "xOK" ]; then
65 echo "[fail] $pattern.res"
66 ok="$ok $pattern"
67 elif [ "x$SAV" = "xNOK" ]; then
68 echo "[======= fail $pattern.res sav/$pattern.sav =======]"
69 nok="$nok $ff"
70 echo "$ii" >> "$ERRLIST"
71 elif [ "x$FAIL" = "xNOK" ]; then
72 echo "[======= changed $pattern.res sav/$pattern.fail ======]"
73 nok="$nok $ff"
74 echo "$ii" >> "$ERRLIST"
75 else
76 echo "[=== no sav ===] $pattern.res"
77 nos="$nos $pattern"
78 fi
79 }
80
81 find_nitc()
82 {
83 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
84 if [[ "x$recent" == "x" ]]; then
85 echo 'Could not find nitc, aborting'
86 exit 1
87 fi
88 echo 'Using nitc from: '$recent
89 NITC=$recent
90 }
91
92 # The default nitc compiler
93 [ -z "$NITC" ] && find_nitc
94
95 verbose=false
96 stop=false
97 while [ $stop = false ]; do
98 case $1 in
99 -o) OPT="$OPT $2"; shift; shift;;
100 -v) verbose=true; shift;;
101 -h) usage; exit;;
102 *) stop=true
103 esac
104 done
105
106 # Mark to distinguish files among tests
107 # MARK=
108
109 # File where error tests are outputed
110 # Old ERRLIST is backuped
111 ERRLIST=${ERRLIST:-errlist}
112 ERRLIST_TARGET=$ERRLIST
113
114 if [ $# = 0 ]; then
115 usage;
116 exit
117 fi
118
119 # Initiate new ERRLIST
120 if [ "x$ERRLIST" = "x" ]; then
121 ERRLIST=/dev=null
122 else
123 ERRLIST=$ERRLIST.tmp
124 > "$ERRLIST"
125 fi
126
127 ok=""
128 nok=""
129
130 for ii in "$@"; do
131 if [ ! -f $ii ]; then
132 echo "File '$ii' does not exist."
133 continue
134 fi
135
136 tmp=${ii/../AA}
137 if [ "x$tmp" = "x$ii" ]; then
138 oincludes="-I . -I ../lib/standard -I ../lib/standard/collection"
139 else
140 oincludes=""
141 fi
142
143 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
144 f=`basename "$ii" .nit`
145 d=`dirname "$ii"`
146 ff="$f"
147 i="$ii"
148 includes="$oincludes"
149
150 if [ "x$alt" != "x" ]; then
151 test -d alt || mkdir -p alt
152 i="alt/${f}_$alt.nit"
153 ff="${ff}_$alt"
154 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
155 includes="$includes -I alt"
156 fi
157 ff="$ff$MARK"
158
159 echo -n "=> $i: "
160
161 rm -rf "$ff.res" "$ff.err" "$ff.write" "$ff.bin" 2> /dev/null
162
163 # Compile
164 if [ "x$verbose" = "xtrue" ]; then
165 echo ""
166 echo $NITC $OPT -o "$ff.bin" "$i" "$includes"
167 fi
168 $NITC $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
169 ERR=$?
170 if [ "x$verbose" = "xtrue" ]; then
171 cat "$ff.compile.log"
172 cat >&2 "$ff.cmp.err"
173 fi
174 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
175 if [ "$ERR" != 0 ]; then
176 echo -n "! "
177 cp "$ff.cmp.err" "$ff.res"
178 process_result $ff
179 elif [ -x "./$ff.bin" ]; then
180 echo -n ". "
181 # Execute
182 args=""
183 if [ "x$verbose" = "xtrue" ]; then
184 echo ""
185 echo "NIT_NO_STACK=1 ./$ff.bin" $args
186 fi
187 if [ -f "$f.inputs" ]; then
188 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
189 else
190 NIT_NO_STACK=1 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
191 fi
192 if [ "x$verbose" = "xtrue" ]; then
193 cat "$ff.res"
194 cat >&2 "$ff.err"
195 fi
196 if [ -f "$ff.write" ]; then
197 cat "$ff.write" >> "$ff.res"
198 elif [ -d "$ff.write" ]; then
199 ls -F $ff.write >> "$ff.res"
200 fi
201 if [ -s "$ff.err" ]; then
202 cat "$ff.err" >> "$ff.res"
203 fi
204 process_result $ff
205
206 if [ -f "$f.args" ]; then
207 fargs=$f.args
208 cptr=0
209 while read line; do
210 ((cptr=cptr+1))
211 args=$line
212 fff=$ff"_args"$cptr
213 if [ "x$verbose" = "xtrue" ]; then
214 echo ""
215 echo "NIT_NO_STACK=1 ./$ff.bin" $args
216 fi
217 echo -n "==> args #"$cptr " "
218 if [ -f "$f.inputs" ]; then
219 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
220 else
221 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args > $fff.res 2>$fff.err"
222 fi
223 if [ "x$verbose" = "xtrue" ]; then
224 cat "$fff.res"
225 cat >&2 "$fff.err"
226 fi
227 if [ -f "$fff.write" ]; then
228 cat "$fff.write" >> "$fff.res"
229 elif [ -d "$fff.write" ]; then
230 ls -F $fff.write >> "$fff.res"
231 fi
232 if [ -s "$fff.err" ]; then
233 cat "$fff.err" >> "$fff.res"
234 fi
235 process_result $fff
236 done < $fargs
237 fi
238 else
239 echo -n "! "
240 echo "Compilation error" > "$ff.res"
241 process_result $ff
242 fi
243 done
244 done
245
246 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
247
248 if [ -n "$nok" ]; then
249 echo "fail: $nok"
250 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
251 fi
252 if [ -n "$nos" ]; then
253 echo "no sav: $nos"
254 fi
255
256 # write $ERRLIST
257 if [ "x$ERRLIST" != "x" ]; then
258 if [ -x "$ERRLIST_TARGET" ]; then
259 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
260 fi
261 mv $ERRLIST $ERRLIST_TARGET
262 fi
263
264 if [ -n "$nok" ]; then
265 exit 1
266 else
267 exit 0
268 fi