tests: reorganize imports for nitc call
[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"
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 "./$ff.bin" $args
186 fi
187 if [ -f "$f.inputs" ]; then
188 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
189 else
190 "./$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 cat $fargs |
210 while read line; do
211 ((cptr=cptr+1))
212 args=$line
213 fff=$ff"_args"$cptr
214 if [ "x$verbose" = "xtrue" ]; then
215 echo ""
216 echo "./$ff.bin" $args
217 fi
218 echo -n "==> args #"$cptr " "
219 if [ -f "$f.inputs" ]; then
220 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
221 else
222 sh -c "./$ff.bin ''$args > $fff.res 2>$fff.err"
223 fi
224 if [ "x$verbose" = "xtrue" ]; then
225 cat "$fff.res"
226 cat >&2 "$fff.err"
227 fi
228 if [ -f "$fff.write" ]; then
229 cat "$fff.write" >> "$fff.res"
230 elif [ -d "$fff.write" ]; then
231 ls -F $fff.write >> "$fff.res"
232 fi
233 if [ -s "$fff.err" ]; then
234 cat "$fff.err" >> "$fff.res"
235 fi
236 process_result $fff
237 done
238 fi
239 else
240 echo -n "! "
241 echo "Compilation error" > "$ff.res"
242 process_result $ff
243 fi
244 done
245 done
246
247 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
248
249 if [ -n "$nok" ]; then
250 echo "fail: $nok"
251 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
252 fi
253 if [ -n "$nos" ]; then
254 echo "no sav: $nos"
255 fi
256
257 # write $ERRLIST
258 if [ "x$ERRLIST" != "x" ]; then
259 if [ -x "$ERRLIST_TARGET" ]; then
260 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
261 fi
262 mv $ERRLIST $ERRLIST_TARGET
263 fi
264
265 if [ -n "$nok" ]; then
266 exit 1
267 else
268 exit 0
269 fi