OSDN Git Service

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