tests: validate file existence of file to test before 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 "$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 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
136 f=`basename "$ii" .nit`
137 d=`dirname "$ii"`
138 ff="$f"
139 i="$ii"
140 if [ "x$alt" != "x" ]; then
141 test -d alt || mkdir -p alt
142 i="alt/${f}_$alt.nit"
143 ff="${ff}_$alt"
144 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
145 fi
146 ff="$ff$MARK"
147
148 echo -n "=> $i: "
149
150 rm "$ff.res" "$ff.err" "$ff.write" "$ff.bin" 2> /dev/null
151
152 # Compile
153 if [ "x$verbose" = "xtrue" ]; then
154 echo ""
155 echo $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard
156 fi
157 $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard 2> "$ff.cmp.err" > "$ff.compile.log"
158 ERR=$?
159 if [ "x$verbose" = "xtrue" ]; then
160 cat "$ff.compile.log"
161 cat >&2 "$ff.cmp.err"
162 fi
163 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
164 if [ "$ERR" != 0 ]; then
165 echo -n "! "
166 cp "$ff.cmp.err" "$ff.res"
167 process_result $ff
168 elif [ -x "./$ff.bin" ]; then
169 echo -n ". "
170 # Execute
171 args=""
172 if [ "x$verbose" = "xtrue" ]; then
173 echo ""
174 echo "./$ff.bin" $args
175 fi
176 if [ -f "$f.inputs" ]; then
177 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
178 else
179 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
180 fi
181 if [ "x$verbose" = "xtrue" ]; then
182 cat "$ff.res"
183 cat >&2 "$ff.err"
184 fi
185 if [ -f "$ff.write" ]; then
186 cat "$ff.write" >> "$ff.res"
187 fi
188 if [ -s "$ff.err" ]; then
189 cat "$ff.err" >> "$ff.res"
190 fi
191 process_result $ff
192
193 if [ -f "$f.args" ]; then
194 fargs=$f.args
195 cptr=0
196 cat $fargs |
197 while read line; do
198 ((cptr=cptr+1))
199 args=$line
200 fff=$ff"_args"$cptr
201 if [ "x$verbose" = "xtrue" ]; then
202 echo ""
203 echo "./$ff.bin" $args
204 fi
205 echo -n "==> args #"$cptr " "
206 if [ -f "$f.inputs" ]; then
207 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
208 else
209 sh -c "./$ff.bin ''$args > $fff.res 2>$fff.err"
210 fi
211 if [ "x$verbose" = "xtrue" ]; then
212 cat "$fff.res"
213 cat >&2 "$fff.err"
214 fi
215 if [ -f "$fff.write" ]; then
216 cat "$fff.write" >> "$fff.res"
217 fi
218 if [ -s "$fff.err" ]; then
219 cat "$fff.err" >> "$fff.res"
220 fi
221 process_result $fff
222 done
223 fi
224 else
225 echo -n "! "
226 echo "Compilation error" > "$ff.res"
227 process_result $ff
228 fi
229 done
230 done
231
232 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
233
234 if [ -n "$nok" ]; then
235 echo "fail: $nok"
236 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
237 fi
238 if [ -n "$nos" ]; then
239 echo "no sav: $nos"
240 fi
241
242 # write $ERRLIST
243 if [ "x$ERRLIST" != "x" ]; then
244 if [ -x "$ERRLIST_TARGET" ]; then
245 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
246 fi
247 mv $ERRLIST $ERRLIST_TARGET
248 fi
249
250 if [ -n "$nok" ]; then
251 exit 1
252 else
253 exit 0
254 fi