gcc/
[official-gcc.git] / libgo / testsuite / gotest
blob79e02e2a1ea07f1f5eaebb77bdc4d5e8717121c6
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.
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.
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
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=${GOLIBS:-}
20 export GC GL GOLIBS
22 NM=${NM:-nm}
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 goarch=""
30 gofiles=""
31 goos=""
32 pkgfiles=""
33 loop=true
34 keep=false
35 pkgpath=
36 prefix=
37 dejagnu=no
38 timeout=240
39 testname=""
40 bench=""
41 trace=false
42 while $loop; do
43 case "x$1" in
44 x--srcdir)
45 srcdir=$2
46 shift
47 shift
49 x--srcdir=*)
50 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
51 shift
53 x--basedir)
54 basedir=$2
55 shift
56 shift
58 x--basedir=*)
59 basedir=`echo $1 | sed -e 's/^--basedir=//'`
60 shift
62 x--goarch)
63 goarch=$2
64 shift
65 shift
67 x--goarch=*)
68 goarch=`echo $1 | sed -e 's/^--goarch=//'`
69 shift
71 x--goos)
72 goos=$2
73 shift
74 shift
76 x--goos=*)
77 goos=`echo $1 | sed -e 's/^--goos=//'`
78 shift
80 x--pkgpath)
81 pkgpath=$2
82 shift
83 shift
85 x--pkgpath=*)
86 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
87 shift
89 x--prefix)
90 prefix=$2
91 shift
92 shift
94 x--prefix=*)
95 prefix=`echo $1 | sed -e 's/^--prefix=//'`
96 shift
98 x--keep)
99 keep=true
100 shift
102 x--pkgfiles)
103 pkgfiles=$2
104 shift
105 shift
107 x--pkgfiles=*)
108 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
109 shift
111 x--dejagnu)
112 dejagnu=$2
113 shift
114 shift
116 x--dejagnu=*)
117 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
118 shift
120 x--timeout)
121 timeout=$2
122 shift
123 shift
125 x--timeout=*)
126 timeout=`echo $1 | sed -e 's/^--timeout=//'`
127 shift
129 x--testname)
130 testname=$2
131 shift
132 shift
134 x--testname=*)
135 testname=`echo $1 | sed -e 's/^--testname=//'`
136 shift
138 x--bench)
139 bench=$2
140 shift
141 shift
143 x--bench=*)
144 bench=`echo $1 | sed -e 's/^--bench=//'`
145 shift
147 x--trace)
148 trace=true
149 shift
151 x-*)
152 loop=false
155 loop=false
158 gofiles="$gofiles $1"
159 shift
161 esac
162 done
164 DIR=gotest$$
165 rm -rf $DIR
166 mkdir $DIR
168 cd $DIR
169 mkdir test
170 cd test
172 if test $keep = false; then
173 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
174 else
175 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
178 case "$srcdir" in
182 srcdir="../../$srcdir"
184 esac
186 SRCDIR=$srcdir
187 export SRCDIR
189 case "$basedir" in
193 basedir="../../$basedir"
195 esac
197 # Link all the files/directories in srcdir into our working directory,
198 # so that the tests do not have to refer to srcdir to find test data.
199 ln -s $srcdir/* .
201 # Some tests refer to a ../testdata directory.
202 if test -e $srcdir/../testdata; then
203 rm -f ../testdata
204 abssrcdir=`cd $srcdir && pwd`
205 ln -s $abssrcdir/../testdata ../testdata
208 # Copy the .go files because io/utils_test.go expects a regular file.
209 case "x$gofiles" in
211 case "x$pkgfiles" in
213 for f in `cd $srcdir; ls *.go`; do
214 rm -f $f;
215 cp $srcdir/$f .
216 done
219 for f in $pkgfiles; do
220 if test -f $basedir/$f; then
221 b=`basename $f`
222 rm -f $b
223 cp $basedir/$f $b
224 elif test -f ../../$f; then
225 b=`basename $f`
226 rm -f $b
227 cp ../../$f $b
228 else
229 echo "file $f not found" 1>&2
230 exit 1
232 done
233 for f in `cd $srcdir; ls *_test.go`; do
234 rm -f $f
235 cp $srcdir/$f .
236 done
238 esac
241 for f in $gofiles; do
242 b=`basename $f`
243 rm -f $b
244 cp $basedir/$f $b
245 done
246 case "x$pkgfiles" in
248 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
249 rm -f $f
250 cp $srcdir/$f .
251 done
254 for f in $pkgfiles; do
255 if test -f $basedir/$f; then
256 b=`basename $f`
257 rm -f $b
258 cp $basedir/$f $b
259 elif test -f ../../$f; then
260 b=`basename $f`
261 rm -f $b
262 cp ../../$f $b
263 else
264 echo "file $f not found" 1>&2
265 exit 1
267 done
269 esac
271 esac
273 # Some tests expect the _obj directory created by the gc Makefiles.
274 mkdir _obj
276 # Some tests expect the _test directory created by the gc Makefiles.
277 mkdir _test
279 case "x$gofiles" in
281 for f in `ls *_test.go`; do
282 tag1=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
283 tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
284 if test x$tag1 = x$f; then
285 tag1=
287 if test x$tag2 = x$f; then
288 tag2=
291 case "$tag1" in
292 "") ;;
293 $goarch) ;;
294 $goos) ;;
295 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
296 tag1=nonmatchingtag
298 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64)
299 tag1=nonmatchingtag
301 esac
303 case "$tag2" in
304 "") ;;
305 $goarch) ;;
306 $goos) ;;
307 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
308 tag2=nonmatchingtag
310 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64)
311 tag2=nonmatchingtag
313 esac
315 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
316 taglines=`sed '/^package /q' < $f | fgrep '// +build '`
317 if test "$taglines" = ""; then
318 omatch=true
319 else
320 omatch=false
322 for tags in $taglines; do
323 match=false
324 for tag in $tags; do
325 reverse=false
326 case $tag in
327 "!"*)
328 reverse=true
329 tag=`echo $tag | sed -e 's/^!//'`
331 esac
333 case $tag in
334 "//" | "+build")
336 $goos | $goarch | cgo)
337 match=true
339 *,*)
340 match=true
341 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
342 case $ctag in
343 $goos | $goarch | cgo)
346 match=false
348 esac
349 done
351 esac
353 if test "$reverse" = true; then
354 if test "$match" = true; then
355 match=false
356 else
357 match=true
360 done
361 if test "$match" = "true"; then
362 omatch=true
364 done
366 if test "$omatch" = "true"; then
367 gofiles="$gofiles $f"
370 done
373 xgofiles=$gofiles
374 gofiles=
375 for f in $xgofiles; do
376 gofiles="$gofiles `basename $f`"
377 done
378 esac
380 case "x$gofiles" in
382 echo 'no test files found' 1>&2
383 exit 1
385 esac
387 # Run any commands given in sources, like
388 # // gotest: $GC foo.go
389 # to build any test-only dependencies.
390 holdGC="$GC"
391 GC="$GC -g -c -I ."
392 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
393 GC="$holdGC"
395 case "x$pkgfiles" in
397 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
400 for f in $pkgfiles; do
401 pkgbasefiles="$pkgbasefiles `basename $f`"
402 done
404 esac
406 case "x$pkgfiles" in
408 echo 'no source files found' 1>&2
409 exit 1
411 esac
413 # Split $gofiles into external gofiles (those in *_test packages)
414 # and internal ones (those in the main package).
415 xgofiles=
416 for f in $gofiles; do
417 package=`grep '^package[ ]' $f | sed 1q`
418 case "$package" in
419 *_test)
420 xgofiles="$xgofiles $f"
423 ngofiles="$ngofiles $f"
425 esac
426 done
427 gofiles=$ngofiles
429 # External $O file
430 xofile=""
431 havex=false
432 if [ "x$xgofiles" != "x" ]; then
433 xofile="_xtest_.o"
434 havex=true
437 testmain=
438 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then
439 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
440 testmain="${package}.TestMain"
441 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then
442 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
443 testmain="${package}.TestMain"
446 set -e
448 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
450 pkgpatharg=
451 xpkgpatharg=
452 prefixarg=
453 if test -n "$pkgpath"; then
454 pkgpatharg="-fgo-pkgpath=$pkgpath"
455 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
456 elif test -n "$prefix"; then
457 prefixarg="-fgo-prefix=$prefix"
460 if test "$trace" = "true"; then
461 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
463 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
465 if $havex; then
466 mkdir -p `dirname $package`
467 cp _gotest_.o `dirname $package`/lib`basename $package`.a
468 if test "$trace" = "true"; then
469 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
471 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
474 # They all compile; now generate the code to call them.
476 testname() {
477 # Remove the package from the name used with the -test option.
478 echo $1 | sed 's/^.*\.//'
481 localname() {
482 # The package main has been renamed to __main__ when imported.
483 # Adjust its uses.
484 echo $1 | sed 's/^main\./__main__./'
488 text="T"
489 case "$goarch" in
490 ppc64*) text="[TD]" ;;
491 esac
493 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
495 # test functions are named TestFoo
496 # the grep -v eliminates methods and other special names
497 # that have multiple dots.
498 pattern='Test([^a-z].*)?'
499 # The -p option tells GNU nm not to sort.
500 # The -v option tells Solaris nm to sort by value.
501 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
502 if [ "x$tests" = x ]; then
503 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
504 exit 2
506 # benchmarks are named BenchmarkFoo.
507 pattern='Benchmark([^a-z].*)?'
508 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
510 # examples are named ExampleFoo
511 pattern='Example([^a-z].*)?'
512 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
514 # package spec
515 echo 'package main'
516 echo
517 # imports
518 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
519 echo 'import "./_gotest_"'
521 if $havex; then
522 echo 'import "./_xtest_"'
524 echo 'import "testing"'
525 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
526 if ! test -n "$testmain"; then
527 echo 'import __os__ "os"'
529 # test array
530 echo
531 echo 'var tests = []testing.InternalTest {'
532 for i in $tests
534 n=$(testname $i)
535 if test "$n" != "TestMain"; then
536 j=$(localname $i)
537 echo ' {"'$n'", '$j'},'
539 done
540 echo '}'
542 # benchmark array
543 # The comment makes the multiline declaration
544 # gofmt-safe even when there are no benchmarks.
545 echo 'var benchmarks = []testing.InternalBenchmark{ //'
546 for i in $benchmarks
548 n=$(testname $i)
549 j=$(localname $i)
550 echo ' {"'$n'", '$j'},'
551 done
552 echo '}'
554 # examples array
555 echo 'var examples = []testing.InternalExample{ //'
556 # This doesn't work because we don't pick up the output.
557 #for i in $examples
559 # n=$(testname $i)
560 # j=$(localname $i)
561 # echo ' {"'$n'", '$j', ""},'
562 #done
563 echo '}'
565 # body
566 echo \
568 var matchPat string
569 var matchRe *__regexp__.Regexp
571 func matchString(pat, str string) (result bool, err error) {
572 if matchRe == nil || matchPat != pat {
573 matchPat = pat
574 matchRe, err = __regexp__.Compile(matchPat)
575 if err != nil {
576 return
579 return matchRe.MatchString(str), nil
582 func main() {
583 m := testing.MainStart(matchString, tests, benchmarks, examples)
585 if test -n "$testmain"; then
586 echo " ${testmain}(m)"
587 else
588 echo ' __os__.Exit(m.Run())'
591 echo '}'
592 }>_testmain.go
594 case "x$dejagnu" in
595 xno)
596 if test "$trace" = "true"; then
597 echo ${GC} -g -c _testmain.go
599 ${GC} -g -c _testmain.go
601 if test "$trace" = "true"; then
602 echo ${GL} *.o ${GOLIBS}
604 ${GL} *.o ${GOLIBS}
606 set +e
607 if test "$bench" = ""; then
608 if test "$trace" = "true"; then
609 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
611 ./a.out -test.short -test.timeout=${timeout}s "$@" &
612 pid=$!
613 (sleep `expr $timeout + 10`
614 echo > gotest-timeout
615 echo "timed out in gotest" 1>&2
616 kill -9 $pid) &
617 alarmpid=$!
618 wait $pid
619 status=$?
620 if ! test -f gotest-timeout; then
621 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
622 kill $alarmpid
623 wait $alarmpid
624 if test "$sleeppid" != ""; then
625 kill $sleeppid
628 else
629 if test "$trace" = "true"; then
630 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
632 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
633 status=$?
635 exit $status
637 xyes)
638 rm -rf ../../testsuite/*.o
639 files=`echo *`
640 for f in $files; do
641 if test "$f" = "_obj" || test "$f" = "_test"; then
642 continue
644 rm -rf ../../testsuite/$f
645 if test -f $f; then
646 cp $f ../../testsuite/
647 else
648 ln -s ../$DIR/test/$f ../../testsuite/
650 done
651 cd ../../testsuite
652 rm -rf _obj _test
653 mkdir _obj _test
654 if test "$testname" != ""; then
655 GOTESTNAME="$testname"
656 export GOTESTNAME
658 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
659 # Useful when using make check-target-libgo
660 cat libgo.log >> libgo-all.log
661 cat libgo.sum >> libgo-all.sum
662 rm -rf $files
664 esac