tests: set LANG to avoid locale perturbation
[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" 2> /dev/null
90
91 # Compile
92 if [ "x$verbose" = "xtrue" ]; then
93 echo ""
94 echo $NITC $OPT -o "$f.bin" "$i" -I . -I alt -I ../lib/standard
95 fi
96 $NITC $OPT -o "$f.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 mv "$f.bin" "$ff.bin" 2> /dev/null
103 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
104 if [ "$ERR" != 0 ]; then
105 echo -n "! "
106 cp "$ff.cmp.err" "$ff.res"
107 else
108 echo -n ". "
109 # Execute
110 if [ -f "$f.args" ]; then
111 args=`cat "$f.args"`
112 else
113 args=""
114 fi
115 if [ "x$verbose" = "xtrue" ]; then
116 echo ""
117 echo "./$ff.bin" $args
118 fi
119 if [ -f "$f.inputs" ]; then
120 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
121 else
122 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
123 fi
124 if [ "x$verbose" = "xtrue" ]; then
125 cat "$ff.res"
126 cat >&2 "$ff.err"
127 fi
128 if [ -f "$ff.write" ]; then
129 cat "$ff.write" >> "$ff.res"
130 fi
131 if [ -s "$ff.err" ]; then
132 cat "$ff.err" >> "$ff.res"
133 fi
134 fi
135
136 # Result
137 SAV=""
138 FAIL=""
139 if [ -r "sav/$ff.sav" ]; then
140 diff -u "$ff.res" "sav/$ff.sav" > "$ff.diff.sav.log"
141 if [ "$?" == 0 ]; then
142 SAV=OK
143 else
144 SAV=NOK
145 fi
146 fi
147 if [ -r "sav/$ff.fail" ]; then
148 diff -u "$ff.res" "sav/$ff.fail" > "$ff.diff.fail.log"
149 if [ "$?" == 0 ]; then
150 FAIL=OK
151 else
152 FAIL=NOK
153 fi
154 fi
155 if [ "x$SAV" = "xOK" ]; then
156 if [ "x$FAIL" = "x" ]; then
157 echo "[ok] $ff.res"
158 else
159 echo "[ok] $ff.res - but sav/$ff.fail remains!"
160 fi
161 ok="$ok $ff"
162 elif [ "x$FAIL" = "xOK" ]; then
163 echo "[fail] $ff.res"
164 ok="$ok $ff"
165 elif [ "x$SAV" = "xNOK" ]; then
166 echo "[======= fail $ff.res sav/$ff.sav =======]"
167 nok="$nok $ff"
168 echo "$ii" >> "$ERRLIST"
169 elif [ "x$FAIL" = "xNOK" ]; then
170 echo "[======= changed $ff.res sav/$ff.fail ======]"
171 nok="$nok $ff"
172 echo "$ii" >> "$ERRLIST"
173 else
174 echo "[=== no sav ===] $ff.res"
175 nos="$nos $ff"
176 fi
177 done
178 done
179
180 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
181
182 if [ -n "$nok" ]; then
183 echo "fail: $nok"
184 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
185 fi
186 if [ -n "$nos" ]; then
187 echo "no sav: $nos"
188 fi
189
190 if [ -n "$nok" ]; then
191 exit 1
192 else
193 exit 0
194 fi