OSDN Git Service

13211d0e34c8640adaecbe810cd9bc7c73204e98
[pf3gnuchains/gcc-fork.git] / libgo / testsuite / gotest
1 #!/bin/sh
2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
5
6 # Using all the *_test.go files in the current directory, write out a file
7 # _testmain.go that runs all its tests. Compile everything and run the
8 # tests.
9 # If files are named on the command line, use them instead of *_test.go.
10
11 # Makes egrep,grep work better in general if we put them
12 # in ordinary C mode instead of what the current language is.
13 unset LANG
14 export LC_ALL=C
15 export LC_CTYPE=C
16
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=
20 export GC GL GOLIBS
21
22 # srcdir is where the source files are found.  basedir is where the
23 # source file paths are relative to.
24 # gofiles are the test files.  pkgfiles are the source files.
25 srcdir=.
26 basedir=.
27 gofiles=""
28 pkgfiles=""
29 loop=true
30 keep=false
31 prefix=
32 dejagnu=no
33 while $loop; do
34         case "x$1" in
35         x--srcdir)
36                 srcdir=$2
37                 shift
38                 shift
39                 ;;
40         x--srcdir=*)
41                 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
42                 shift
43                 ;;
44         x--basedir)
45                 basedir=$2
46                 shift
47                 shift
48                 ;;
49         x--basedir=*)
50                 basedir=`echo $1 | sed -e 's/^--basedir=//'`
51                 shift
52                 ;;
53         x--prefix)
54                 prefix=$2
55                 shift
56                 shift
57                 ;;
58         x--prefix=*)
59                 prefix=`echo $1 | sed -e 's/^--prefix=//'`
60                 shift
61                 ;;
62         x--keep)
63                 keep=true
64                 shift
65                 ;;
66         x--pkgfiles)
67                 pkgfiles=$2
68                 shift
69                 shift
70                 ;;
71         x--pkgfiles=*)
72                 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
73                 shift
74                 ;;
75         x--dejagnu)
76                 dejagnu=$2
77                 shift
78                 shift
79                 ;;
80         x--dejagnu=*)
81                 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
82                 shift
83                 ;;
84         x-*)
85                 loop=false
86                 ;;
87         x)
88                 loop=false
89                 ;;
90         *)
91                 gofiles="$gofiles $1"
92                 shift
93                 ;;
94         esac
95 done
96
97 DIR=gotest$$
98 rm -rf $DIR
99 mkdir $DIR
100
101 cd $DIR
102
103 if test $keep = false; then
104   trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
105 else
106   trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
107 fi
108
109 case "$srcdir" in
110         /*)
111                 ;;
112         *)
113                 srcdir="../$srcdir"
114                 ;;
115 esac
116
117 SRCDIR=$srcdir
118 export SRCDIR
119
120 case "$basedir" in
121         /*)
122                 ;;
123         *)
124                 basedir="../$basedir"
125                 ;;
126 esac
127
128 # Link all the files/directories in srcdir into our working directory,
129 # so that the tests do not have to refer to srcdir to find test data.
130 ln -s $srcdir/* .
131
132 # Copy the .go files because io/utils_test.go expects a regular file.
133 case "x$gofiles" in
134 x)
135         case "x$pkgfiles" in
136         x)
137                 for f in `cd $srcdir; ls *.go`; do
138                     rm -f $f;
139                     cp $srcdir/$f .
140                 done
141                 ;;
142         *)
143                 for f in $pkgfiles; do
144                     if test -f $basedir/$f; then
145                         b=`basename $f`
146                         rm -f $b
147                         cp $basedir/$f $b
148                     elif test -f ../$f; then
149                         b=`basename $f`
150                         rm -f $b
151                         cp ../$f $b
152                     else
153                         echo "file $f not found" 1>&2
154                         exit 1
155                     fi
156                 done
157                 for f in `cd $srcdir; ls *_test.go`; do
158                     rm -f $f
159                     cp $srcdir/$f .
160                 done
161                 ;;
162         esac
163         ;;
164 *)
165         for f in $gofiles; do
166             b=`basename $f`
167             rm -f $b
168             cp $basedir/$f $b
169         done
170         case "x$pkgfiles" in
171         x)
172                 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
173                     rm -f $f
174                     cp $srcdir/$f .
175                 done
176                 ;;
177         *)
178                 for f in $pkgfiles; do
179                     if test -f $basedir/$f; then
180                         b=`basename $f`
181                         rm -f $b
182                         cp $basedir/$f $b
183                     elif test -f ../$f; then
184                         b=`basename $f`
185                         rm -f $b
186                         cp ../$f $b
187                     else
188                         echo "file $f not found" 1>&2
189                         exit 1
190                     fi
191                 done
192                 ;;
193         esac
194         ;;
195 esac
196
197 # Some tests expect the _obj directory created by the gc Makefiles.
198 mkdir _obj
199
200 # Some tests expect the _test directory created by the gc Makefiles.
201 mkdir _test
202
203 case "x$gofiles" in
204 x)
205         gofiles=$(echo -n $(ls *_test.go 2>/dev/null))
206 esac
207
208 case "x$gofiles" in
209 x)
210         echo 'no test files found' 1>&2
211         exit 1
212 esac
213
214 # Run any commands given in sources, like
215 #   // gotest: $GC foo.go
216 # to build any test-only dependencies.
217 holdGC="$GC"
218 GC="$GC -g -c -I ."
219 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
220 GC="$holdGC"
221
222 case "x$pkgfiles" in
223 x)
224         pkgbasefiles=$(echo -n $(ls *.go | grep -v _test.go 2>/dev/null))
225         ;;
226 *)
227         for f in $pkgfiles; do
228             pkgbasefiles="$pkgbasefiles `basename $f`"
229         done
230         ;;
231 esac
232
233 case "x$pkgfiles" in
234 x)
235         echo 'no source files found' 1>&2
236         exit 1
237         ;;
238 esac
239
240 # Split $gofiles into external gofiles (those in *_test packages)
241 # and internal ones (those in the main package).
242 xgofiles=$(echo $(grep '^package[       ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
243 gofiles=$(echo $(grep '^package[        ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
244
245 # External $O file
246 xofile=""
247 havex=false
248 if [ "x$xgofiles" != "x" ]; then
249         xofile="_xtest_.o"
250         havex=true
251 fi
252
253 set -e
254
255 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
256
257 prefixarg=
258 if test -n "$prefix"; then
259         prefixarg="-fgo-prefix=$prefix"
260 fi
261
262 $GC -g $prefixarg -c -I . -o _gotest_.o $gofiles $pkgbasefiles
263 if $havex; then
264         mkdir -p `dirname $package`
265         cp _gotest_.o `dirname $package`/lib`basename $package`.a
266         $GC -g -c -I . -o $xofile $xgofiles
267 fi
268
269 # They all compile; now generate the code to call them.
270 {
271         # test functions are named TestFoo
272         # the grep -v eliminates methods and other special names
273         # that have multiple dots.
274         pattern='Test([^a-z].*)?'
275         tests=$(nm -s _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
276         if [ "x$tests" = x ]; then
277                 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
278                 exit 2
279         fi
280
281         # package spec
282         echo 'package main'
283         echo
284         # imports
285         if echo "$tests" | egrep -v '_test\.' >/dev/null; then
286                 echo 'import "./_gotest_"'
287         fi
288         if $havex; then
289                 echo 'import "./_xtest_"'
290         fi
291         if [ $package != "testing" ]; then
292                 echo 'import "testing"'
293                 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
294         fi
295         # test array
296         echo
297         echo 'var tests = []testing.InternalTest {'
298         for i in $tests
299         do
300                 echo '  { "'$i'", '$i' },'
301         done
302         echo '}'
303         # body
304         echo
305         echo 'func main() {'
306         echo '  testing.Main(__regexp__.MatchString, tests)'
307         echo '}'
308 }>_testmain.go
309
310 case "x$dejagnu" in
311 xno)
312         ${GC} -g -c _testmain.go
313         ${GL} *.o ${GOLIBS}
314         ./a.out "$@"
315         ;;
316 xyes)
317         # This is the only file which is optionally made.
318         # All others are overwritten on copying/building,
319         # but this may remain and cause conflicts if not
320         # deleted.
321         rm -rf ../testsuite/_xtest_.o
322         cp *.o _testmain.go ../testsuite
323         cd ../testsuite
324         $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
325         # Useful when using make check-target-libgo
326         cat libgo.log >> libgo-all.log
327         cat libgo.sum >> libgo-all.sum
328         ;;
329 esac