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