tests: generate files in out/ instead of tests/
[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 "out/$pattern.res" "sav/$pattern.sav" > "out/$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 "out/$pattern.res" "sav/$pattern.fail" > "out/$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] out/$pattern.res"
60 else
61 echo "[ok] out/$pattern.res - but sav/$pattern.fail remains!"
62 fi
63 ok="$ok $pattern"
64 elif [ "x$FAIL" = "xOK" ]; then
65 echo "[fail] out/$pattern.res"
66 ok="$ok $pattern"
67 elif [ "x$SAV" = "xNOK" ]; then
68 echo "[======= fail out/$pattern.res sav/$pattern.sav =======]"
69 nok="$nok $pattern"
70 echo "$ii" >> "$ERRLIST"
71 elif [ "x$FAIL" = "xNOK" ]; then
72 echo "[======= changed out/$pattern.res sav/$pattern.fail ======]"
73 nok="$nok $pattern"
74 echo "$ii" >> "$ERRLIST"
75 else
76 echo "[=== no sav ===] out/$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 make_alts0()
93 {
94 ii="$1"
95 xalt="$2"
96 fs=""
97 for alt in `sed -n "s/.*#!*\($xalt[0-9]*\)#.*/\1/p" "$ii" | sort -u`; do
98 f=`basename "$ii" .nit`
99 d=`dirname "$ii"`
100 ff="$f"
101 i="$ii"
102
103 if [ "x$alt" != "x" ]; then
104 test -d alt || mkdir -p alt
105 i="alt/${f}_$alt.nit"
106 ff="${ff}_$alt"
107 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
108 fi
109 ff="$ff$MARK"
110 fs="$fs $i"
111 done
112 echo "$fs"
113 }
114 make_alts()
115 {
116 ii="$1"
117 fs="$1"
118 for xalt in `sed -n 's/.*#!*\([0-9]*alt\)[0-9]*#.*/\1/p' "$ii" | sort -u`; do
119 fs2=""
120 for f in $fs; do
121 fs2="$fs2 `make_alts0 $f $xalt`"
122 done
123 fs="$fs $fs2"
124 done
125 echo "$fs"
126 }
127
128 # The default nitc compiler
129 [ -z "$NITC" ] && find_nitc
130
131 # Set NIT_DIR if needed
132 [ -z "$NIT_DIR" ] && export NIT_DIR=..
133
134 verbose=false
135 stop=false
136 while [ $stop = false ]; do
137 case $1 in
138 -o) OPT="$OPT $2"; shift; shift;;
139 -v) verbose=true; shift;;
140 -h) usage; exit;;
141 *) stop=true
142 esac
143 done
144
145 # Mark to distinguish files among tests
146 # MARK=
147
148 # File where error tests are outputed
149 # Old ERRLIST is backuped
150 ERRLIST=${ERRLIST:-errlist}
151 ERRLIST_TARGET=$ERRLIST
152
153 if [ $# = 0 ]; then
154 usage;
155 exit
156 fi
157
158 # Initiate new ERRLIST
159 if [ "x$ERRLIST" = "x" ]; then
160 ERRLIST=/dev=null
161 else
162 ERRLIST=$ERRLIST.tmp
163 > "$ERRLIST"
164 fi
165
166 ok=""
167 nok=""
168
169 # CLEAN the out directory
170 rm -rf out/ 2>/dev/null
171 mkdir out 2>/dev/null
172
173 for ii in "$@"; do
174 if [ ! -f $ii ]; then
175 echo "File '$ii' does not exist."
176 continue
177 fi
178
179 tmp=${ii/../AA}
180 if [ "x$tmp" = "x$ii" ]; then
181 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
182 else
183 includes="-I alt"
184 fi
185
186 f=`basename "$ii" .nit`
187 for i in `make_alts $ii`; do
188 bf=`basename $i .nit`
189 ff="out/$bf"
190 echo -n "=> $bf: "
191
192 # Compile
193 if [ "x$verbose" = "xtrue" ]; then
194 echo ""
195 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
196 fi
197 $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
198 ERR=$?
199 if [ "x$verbose" = "xtrue" ]; then
200 cat "$ff.compile.log"
201 cat >&2 "$ff.cmp.err"
202 fi
203 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
204 if [ "$ERR" != 0 ]; then
205 echo -n "! "
206 cp "$ff.cmp.err" "$ff.res"
207 process_result $bf
208 elif [ -x "./$ff.bin" ]; then
209 cp "$ff.cmp.err" "$ff.res"
210 echo -n ". "
211 # Execute
212 args=""
213 if [ "x$verbose" = "xtrue" ]; then
214 echo ""
215 echo "NIT_NO_STACK=1 ./$ff.bin" $args
216 fi
217 if [ -f "$f.inputs" ]; then
218 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" >> "$ff.res" 2>"$ff.err"
219 else
220 NIT_NO_STACK=1 "./$ff.bin" $args >> "$ff.res" 2>"$ff.err"
221 fi
222 if [ "x$verbose" = "xtrue" ]; then
223 cat "$ff.res"
224 cat >&2 "$ff.err"
225 fi
226 if [ -f "$ff.write" ]; then
227 cat "$ff.write" >> "$ff.res"
228 elif [ -d "$ff.write" ]; then
229 LANG=C /bin/ls -F $ff.write >> "$ff.res"
230 fi
231 if [ -s "$ff.err" ]; then
232 cat "$ff.err" >> "$ff.res"
233 fi
234 process_result $bf
235
236 if [ -f "$f.args" ]; then
237 fargs=$f.args
238 cptr=0
239 while read line; do
240 ((cptr=cptr+1))
241 args=$line
242 bff=$bf"_args"$cptr
243 fff=$ff"_args"$cptr
244 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
245 if [ "x$verbose" = "xtrue" ]; then
246 echo ""
247 echo "NIT_NO_STACK=1 ./$ff.bin" $args
248 fi
249 echo -n "==> args #"$cptr " "
250 if [ -f "$f.inputs" ]; then
251 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
252 else
253 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args > $fff.res 2>$fff.err"
254 fi
255 if [ "x$verbose" = "xtrue" ]; then
256 cat "$fff.res"
257 cat >&2 "$fff.err"
258 fi
259 if [ -f "$fff.write" ]; then
260 cat "$fff.write" >> "$fff.res"
261 elif [ -d "$fff.write" ]; then
262 LANG=C /bin/ls -F $fff.write >> "$fff.res"
263 fi
264 if [ -s "$fff.err" ]; then
265 cat "$fff.err" >> "$fff.res"
266 fi
267 process_result $bff
268 done < $fargs
269 fi
270 else
271 echo -n "! "
272 echo "Compilation error" > "$ff.res"
273 process_result $bf
274 fi
275 done
276 done
277
278 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
279
280 if [ -n "$nok" ]; then
281 echo "fail: $nok"
282 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
283 fi
284 if [ -n "$nos" ]; then
285 echo "no sav: $nos"
286 fi
287
288 # write $ERRLIST
289 if [ "x$ERRLIST" != "x" ]; then
290 if [ -x "$ERRLIST_TARGET" ]; then
291 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
292 fi
293 mv $ERRLIST $ERRLIST_TARGET
294 fi
295
296 if [ -n "$nok" ]; then
297 exit 1
298 else
299 exit 0
300 fi