OSDN Git Service

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