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