tests: remove old bin before compiling
[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 # The default nitc compiler
24 [ -z "$NITC" ] && NITC=../bin/nitc
25
26 usage()
27 {
28 e=`basename "$0"`
29 cat<<END
30 Usage: $e [options] modulenames
31 -o option Pass option to nitc
32 -v Verbose (show tests steps)
33 -h This help
34 END
35 }
36
37 verbose=false
38 stop=false
39 while [ $stop = false ]; do
40 case $1 in
41 -o) OPT="$OPT $2"; shift; shift;;
42 -v) verbose=true; shift;;
43 -h) usage; exit;;
44 *) stop=true
45 esac
46 done
47
48 # Mark to distinguish files among tests
49 # MARK=
50
51 # File where error tests are outputed
52 # Old ERRLIST is backuped
53 ERRLIST=${ERRLIST:-errlist}
54
55 if [ $# = 0 ]; then
56 usage;
57 exit
58 fi
59
60 # Backup and initiate new ERRLIST
61 if [ "x$ERRLIST" = "x" ]; then
62 ERRLIST=/dev=null
63 else
64 if [ -x "$ERRLIST" ]; then
65 mv "$ERRLIST" "${ERRLIST}.bak"
66 fi
67 > "$ERRLIST"
68 fi
69
70 ok=""
71 nok=""
72
73 for ii in "$@"; do
74 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
75 f=`basename "$ii" .nit`
76 d=`dirname "$ii"`
77 ff="$f"
78 i="$ii"
79 if [ "x$alt" != "x" ]; then
80 test -d alt || mkdir -p alt
81 i="alt/${f}_$alt.nit"
82 ff="${ff}_$alt"
83 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
84 fi
85 ff="$ff$MARK"
86
87 echo -n "=> $i: "
88
89 rm "$ff.res" "$ff.err" "$ff.write" "$ff.bin" 2> /dev/null
90
91 # Compile
92 if [ "x$verbose" = "xtrue" ]; then
93 echo ""
94 echo $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard
95 fi
96 $NITC $OPT -o "$ff.bin" "$i" -I . -I alt -I ../lib/standard 2> "$ff.cmp.err" > "$ff.compile.log"
97 ERR=$?
98 if [ "x$verbose" = "xtrue" ]; then
99 cat "$ff.compile.log"
100 cat >&2 "$ff.cmp.err"
101 fi
102 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
103 if [ "$ERR" != 0 ]; then
104 echo -n "! "
105 cp "$ff.cmp.err" "$ff.res"
106 else
107 echo -n ". "
108 # Execute
109 if [ -f "$f.args" ]; then
110 args=`cat "$f.args"`
111 else
112 args=""
113 fi
114 if [ "x$verbose" = "xtrue" ]; then
115 echo ""
116 echo "./$ff.bin" $args
117 fi
118 if [ -f "$f.inputs" ]; then
119 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
120 else
121 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
122 fi
123 if [ "x$verbose" = "xtrue" ]; then
124 cat "$ff.res"
125 cat >&2 "$ff.err"
126 fi
127 if [ -f "$ff.write" ]; then
128 cat "$ff.write" >> "$ff.res"
129 fi
130 if [ -s "$ff.err" ]; then
131 cat "$ff.err" >> "$ff.res"
132 fi
133 fi
134
135 # Result
136 SAV=""
137 FAIL=""
138 if [ -r "sav/$ff.sav" ]; then
139 diff -u "$ff.res" "sav/$ff.sav" > "$ff.diff.sav.log"
140 if [ "$?" == 0 ]; then
141 SAV=OK
142 else
143 SAV=NOK
144 fi
145 fi
146 if [ -r "sav/$ff.fail" ]; then
147 diff -u "$ff.res" "sav/$ff.fail" > "$ff.diff.fail.log"
148 if [ "$?" == 0 ]; then
149 FAIL=OK
150 else
151 FAIL=NOK
152 fi
153 fi
154 if [ "x$SAV" = "xOK" ]; then
155 if [ "x$FAIL" = "x" ]; then
156 echo "[ok] $ff.res"
157 else
158 echo "[ok] $ff.res - but sav/$ff.fail remains!"
159 fi
160 ok="$ok $ff"
161 elif [ "x$FAIL" = "xOK" ]; then
162 echo "[fail] $ff.res"
163 ok="$ok $ff"
164 elif [ "x$SAV" = "xNOK" ]; then
165 echo "[======= fail $ff.res sav/$ff.sav =======]"
166 nok="$nok $ff"
167 echo "$ii" >> "$ERRLIST"
168 elif [ "x$FAIL" = "xNOK" ]; then
169 echo "[======= changed $ff.res sav/$ff.fail ======]"
170 nok="$nok $ff"
171 echo "$ii" >> "$ERRLIST"
172 else
173 echo "[=== no sav ===] $ff.res"
174 nos="$nos $ff"
175 fi
176 done
177 done
178
179 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
180
181 if [ -n "$nok" ]; then
182 echo "fail: $nok"
183 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
184 fi
185 if [ -n "$nos" ]; then
186 echo "no sav: $nos"
187 fi
188
189 if [ -n "$nok" ]; then
190 exit 1
191 else
192 exit 0
193 fi