OSDN Git Service

libgo/testsuite/gotest: Run tests in source file order.
[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 NM=${NM:-nm}
23
24 # srcdir is where the source files are found.  basedir is where the
25 # source file paths are relative to.
26 # gofiles are the test files.  pkgfiles are the source files.
27 srcdir=.
28 basedir=.
29 gofiles=""
30 pkgfiles=""
31 loop=true
32 keep=false
33 prefix=
34 dejagnu=no
35 timeout=60
36 testname=""
37 while $loop; do
38         case "x$1" in
39         x--srcdir)
40                 srcdir=$2
41                 shift
42                 shift
43                 ;;
44         x--srcdir=*)
45                 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
46                 shift
47                 ;;
48         x--basedir)
49                 basedir=$2
50                 shift
51                 shift
52                 ;;
53         x--basedir=*)
54                 basedir=`echo $1 | sed -e 's/^--basedir=//'`
55                 shift
56                 ;;
57         x--prefix)
58                 prefix=$2
59                 shift
60                 shift
61                 ;;
62         x--prefix=*)
63                 prefix=`echo $1 | sed -e 's/^--prefix=//'`
64                 shift
65                 ;;
66         x--keep)
67                 keep=true
68                 shift
69                 ;;
70         x--pkgfiles)
71                 pkgfiles=$2
72                 shift
73                 shift
74                 ;;
75         x--pkgfiles=*)
76                 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
77                 shift
78                 ;;
79         x--dejagnu)
80                 dejagnu=$2
81                 shift
82                 shift
83                 ;;
84         x--dejagnu=*)
85                 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
86                 shift
87                 ;;
88         x--timeout)
89                 timeout=$2
90                 shift
91                 shift
92                 ;;
93         x--timeout=*)
94                 timeout=`echo $1 | sed -e 's/^--timeout=//'`
95                 shift
96                 ;;
97         x--testname)
98                 testname=$2
99                 shift
100                 shift
101                 ;;
102         x--testname=*)
103                 testname=`echo $1 | sed -e 's/^--testname=//'`
104                 shift
105                 ;;
106         x-*)
107                 loop=false
108                 ;;
109         x)
110                 loop=false
111                 ;;
112         *)
113                 gofiles="$gofiles $1"
114                 shift
115                 ;;
116         esac
117 done
118
119 DIR=gotest$$
120 rm -rf $DIR
121 mkdir $DIR
122
123 cd $DIR
124
125 if test $keep = false; then
126   trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
127 else
128   trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
129 fi
130
131 case "$srcdir" in
132         /*)
133                 ;;
134         *)
135                 srcdir="../$srcdir"
136                 ;;
137 esac
138
139 SRCDIR=$srcdir
140 export SRCDIR
141
142 case "$basedir" in
143         /*)
144                 ;;
145         *)
146                 basedir="../$basedir"
147                 ;;
148 esac
149
150 # Link all the files/directories in srcdir into our working directory,
151 # so that the tests do not have to refer to srcdir to find test data.
152 ln -s $srcdir/* .
153
154 # Some tests refer to a ../testdata directory.
155 if test -e $srcdir/../testdata; then
156   rm -f ../testdata
157   abssrcdir=`cd $srcdir && pwd`
158   ln -s $abssrcdir/../testdata ../testdata
159 fi
160
161 # Copy the .go files because io/utils_test.go expects a regular file.
162 case "x$gofiles" in
163 x)
164         case "x$pkgfiles" in
165         x)
166                 for f in `cd $srcdir; ls *.go`; do
167                     rm -f $f;
168                     cp $srcdir/$f .
169                 done
170                 ;;
171         *)
172                 for f in $pkgfiles; do
173                     if test -f $basedir/$f; then
174                         b=`basename $f`
175                         rm -f $b
176                         cp $basedir/$f $b
177                     elif test -f ../$f; then
178                         b=`basename $f`
179                         rm -f $b
180                         cp ../$f $b
181                     else
182                         echo "file $f not found" 1>&2
183                         exit 1
184                     fi
185                 done
186                 for f in `cd $srcdir; ls *_test.go`; do
187                     rm -f $f
188                     cp $srcdir/$f .
189                 done
190                 ;;
191         esac
192         ;;
193 *)
194         for f in $gofiles; do
195             b=`basename $f`
196             rm -f $b
197             cp $basedir/$f $b
198         done
199         case "x$pkgfiles" in
200         x)
201                 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
202                     rm -f $f
203                     cp $srcdir/$f .
204                 done
205                 ;;
206         *)
207                 for f in $pkgfiles; do
208                     if test -f $basedir/$f; then
209                         b=`basename $f`
210                         rm -f $b
211                         cp $basedir/$f $b
212                     elif test -f ../$f; then
213                         b=`basename $f`
214                         rm -f $b
215                         cp ../$f $b
216                     else
217                         echo "file $f not found" 1>&2
218                         exit 1
219                     fi
220                 done
221                 ;;
222         esac
223         ;;
224 esac
225
226 # Some tests expect the _obj directory created by the gc Makefiles.
227 mkdir _obj
228
229 # Some tests expect the _test directory created by the gc Makefiles.
230 mkdir _test
231
232 case "x$gofiles" in
233 x)
234         gofiles=`ls *_test.go 2>/dev/null`
235 esac
236
237 case "x$gofiles" in
238 x)
239         echo 'no test files found' 1>&2
240         exit 1
241 esac
242
243 # Run any commands given in sources, like
244 #   // gotest: $GC foo.go
245 # to build any test-only dependencies.
246 holdGC="$GC"
247 GC="$GC -g -c -I ."
248 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
249 GC="$holdGC"
250
251 case "x$pkgfiles" in
252 x)
253         pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
254         ;;
255 *)
256         for f in $pkgfiles; do
257             pkgbasefiles="$pkgbasefiles `basename $f`"
258         done
259         ;;
260 esac
261
262 case "x$pkgfiles" in
263 x)
264         echo 'no source files found' 1>&2
265         exit 1
266         ;;
267 esac
268
269 # Split $gofiles into external gofiles (those in *_test packages)
270 # and internal ones (those in the main package).
271 xgofiles=$(echo $(grep '^package[       ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
272 gofiles=$(echo $(grep '^package[        ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
273
274 # External $O file
275 xofile=""
276 havex=false
277 if [ "x$xgofiles" != "x" ]; then
278         xofile="_xtest_.o"
279         havex=true
280 fi
281
282 set -e
283
284 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
285
286 prefixarg=
287 if test -n "$prefix"; then
288         prefixarg="-fgo-prefix=$prefix"
289 fi
290
291 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
292 if $havex; then
293         mkdir -p `dirname $package`
294         cp _gotest_.o `dirname $package`/lib`basename $package`.a
295         $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
296 fi
297
298 # They all compile; now generate the code to call them.
299
300 localname() {
301         # The package main has been renamed to __main__ when imported.
302         # Adjust its uses.
303         echo $1 | sed 's/^main\./__main__./'
304 }
305
306 {
307         # test functions are named TestFoo
308         # the grep -v eliminates methods and other special names
309         # that have multiple dots.
310         pattern='Test([^a-z].*)?'
311         # The -p option tells GNU nm not to sort.
312         # The -v option tells Solaris nm to sort by value.
313         tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
314         if [ "x$tests" = x ]; then
315                 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
316                 exit 2
317         fi
318         # benchmarks are named BenchmarkFoo.
319         pattern='Benchmark([^a-z].*)?'
320         benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
321
322         # package spec
323         echo 'package main'
324         echo
325         # imports
326         if echo "$tests" | egrep -v '_test\.' >/dev/null; then
327                 echo 'import "./_gotest_"'
328         fi
329         if $havex; then
330                 echo 'import "./_xtest_"'
331         fi
332         echo 'import "testing"'
333         echo 'import __os__     "os"' # rename in case tested package is called os
334         echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
335         # test array
336         echo
337         echo 'var tests = []testing.InternalTest {'
338         for i in $tests
339         do
340                 j=$(localname $i)
341                 echo '  {"'$i'", '$j'},'
342         done
343         echo '}'
344         # benchmark array
345         # The comment makes the multiline declaration
346         # gofmt-safe even when there are no benchmarks.
347         echo 'var benchmarks = []testing.InternalBenchmark{ //'
348         for i in $benchmarks
349         do
350                 j=$(localname $i)
351                 echo '  {"'$i'", '$j'},'
352         done
353         echo '}'
354         # body
355         echo \
356 '
357 var matchPat string
358 var matchRe *__regexp__.Regexp
359
360 func matchString(pat, str string) (result bool, err __os__.Error) {
361         if matchRe == nil || matchPat != pat {
362                 matchPat = pat
363                 matchRe, err = __regexp__.Compile(matchPat)
364                 if err != nil {
365                         return
366                 }
367         }
368         return matchRe.MatchString(str), nil
369 }
370
371 func main() {
372         testing.Main(matchString, tests, benchmarks)
373 }'
374 }>_testmain.go
375
376 case "x$dejagnu" in
377 xno)
378         ${GC} -g -c _testmain.go
379         ${GL} *.o ${GOLIBS}
380
381         ./a.out -test.short -test.timeout=$timeout "$@" &
382         pid=$!
383         (sleep `expr $timeout + 10`
384             echo > gotest-timeout
385             echo "timed out in gotest" 1>&2
386             kill -9 $pid) &
387         alarmpid=$!
388         wait $pid
389         status=$?
390         if ! test -f gotest-timeout; then
391             kill $alarmpid
392         fi
393         exit $status
394         ;;
395 xyes)
396         rm -rf ../testsuite/*.o
397         files=`echo *`
398         for f in $files; do
399                 if test "$f" = "_obj" || test "$f" = "_test"; then
400                         continue
401                 fi
402                 rm -rf ../testsuite/$f
403                 if test -f $f; then
404                         cp $f ../testsuite/
405                 else
406                         ln -s ../$DIR/$f ../testsuite/
407                 fi
408         done
409         cd ../testsuite
410         rm -rf _obj _test
411         mkdir _obj _test
412         if test "$testname" != ""; then
413             GOTESTNAME="$testname"
414             export GOTESTNAME
415         fi
416         $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
417         # Useful when using make check-target-libgo
418         cat libgo.log >> libgo-all.log
419         cat libgo.sum >> libgo-all.sum
420         rm -rf $files
421         ;;
422 esac