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
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.
16 export LANG LC_ALL LC_CTYPE
25 # srcdir is where the source files are found. basedir is where the
26 # source file paths are relative to.
27 # gofiles are the test files. pkgfiles are the source files.
51 srcdir
=`echo $1 | sed -e 's/^--srcdir=//'`
60 basedir
=`echo $1 | sed -e 's/^--basedir=//'`
69 goarch
=`echo $1 | sed -e 's/^--goarch=//'`
78 goos
=`echo $1 | sed -e 's/^--goos=//'`
87 pkgpath
=`echo $1 | sed -e 's/^--pkgpath=//'`
96 prefix
=`echo $1 | sed -e 's/^--prefix=//'`
109 pkgfiles
=`echo $1 | sed -e 's/^--pkgfiles=//'`
118 dejagnu
=`echo $1 | sed -e 's/^--dejagnu=//'`
127 timeout
=`echo $1 | sed -e 's/^--timeout=//'`
136 testname
=`echo $1 | sed -e 's/^--testname=//'`
145 bench
=`echo $1 | sed -e 's/^--bench=//'`
159 gofiles
="$gofiles $1"
173 if test $keep = false
; then
174 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
176 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
183 srcdir
="../../$srcdir"
194 basedir
="../../$basedir"
198 # Link all the files/directories in srcdir into our working directory,
199 # so that the tests do not have to refer to srcdir to find test data.
202 # Some tests refer to a ../testdata directory.
203 if test -e $srcdir/..
/testdata
; then
205 abssrcdir
=`cd $srcdir && pwd`
206 ln -s $abssrcdir/..
/testdata ..
/testdata
209 # Copy the .go files because io/utils_test.go expects a regular file.
214 for f
in `cd $srcdir; ls *.go`; do
220 for f
in $pkgfiles; do
228 if test -f $basedir/$f; then
232 elif test -f ..
/..
/$f; then
237 echo "file $f not found" 1>&2
243 for f
in `cd $srcdir; ls *_test.go`; do
251 for f
in $gofiles; do
258 for f
in `cd $srcdir; ls *.go | grep -v *_test.go`; do
264 for f
in $pkgfiles; do
272 if test -f $basedir/$f; then
276 elif test -f ..
/..
/$f; then
281 echo "file $f not found" 1>&2
292 # Some tests expect the _obj directory created by the gc Makefiles.
295 # Some tests expect the _test directory created by the gc Makefiles.
300 for f
in `ls *_test.go`; do
301 tag1
=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
302 tag2
=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
303 if test x
$tag1 = x
$f; then
306 if test x
$tag2 = x
$f; then
314 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
317 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64
)
326 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
329 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64
)
334 if test x
$tag1 != xnonmatchingtag
-a x
$tag2 != xnonmatchingtag
; then
335 tags
=`sed '/^package /q' < $f | grep '^// +build '`
344 if test "$first" = "true"; then
346 elif test "$match" = "false"; then
351 $goos |
$goarch | cgo
)
354 "!"$goos |
"!"$goarch |
"!cgo")
358 for ctag
in `echo $tag | sed -e 's/,/ /g'`; do
360 $goos |
$goarch | cgo
)
362 "!"$goos |
"!"$goarch |
"!cgo")
372 if test "$cmatch" = "true"; then
382 if test "$match" = "false" -a "$first" = "false"; then
386 if test "$omatch" = "true"; then
387 gofiles
="$gofiles $f"
395 for f
in $xgofiles; do
396 gofiles
="$gofiles `basename $f`"
402 echo 'no test files found' 1>&2
407 # Run any commands given in sources, like
408 # // gotest: $GC foo.go
409 # to build any test-only dependencies.
412 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
417 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
420 for f
in $pkgfiles; do
421 pkgbasefiles
="$pkgbasefiles `basename $f`"
428 echo 'no source files found' 1>&2
433 # Split $gofiles into external gofiles (those in *_test packages)
434 # and internal ones (those in the main package).
436 for f
in $gofiles; do
437 package
=`grep '^package[ ]' $f | sed 1q`
440 xgofiles
="$xgofiles $f"
443 ngofiles
="$ngofiles $f"
452 if [ "x$xgofiles" != "x" ]; then
458 if $havex && fgrep
'func TestMain(' $xgofiles >/dev
/null
2>&1; then
459 package
=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
460 testmain
="${package}.TestMain"
461 elif test -n "$gofiles" && fgrep
'func TestMain(' $gofiles >/dev
/null
2>&1; then
462 package
=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
463 testmain
="${package}.TestMain"
468 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
473 if test -n "$pkgpath"; then
474 pkgpatharg
="-fgo-pkgpath=$pkgpath"
475 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
476 elif test -n "$prefix"; then
477 prefixarg
="-fgo-prefix=$prefix"
480 if test "$trace" = "true"; then
481 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
483 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
486 mkdir
-p `dirname $package`
487 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
488 if test "$trace" = "true"; then
489 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
491 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
494 # They all compile; now generate the code to call them.
497 # Remove the package from the name used with the -test option.
498 echo $1 |
sed 's/^.*\.//'
502 # The package main has been renamed to __main__ when imported.
504 echo $1 |
sed 's/^main\./__main__./'
510 ppc64
*) text
="[TD]" ;;
513 symtogo
='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
515 # test functions are named TestFoo
516 # the grep -v eliminates methods and other special names
517 # that have multiple dots.
518 pattern
='Test([^a-z].*)?'
519 # The -p option tells GNU nm not to sort.
520 # The -v option tells Solaris nm to sort by value.
521 tests
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
522 if [ "x$tests" = x
]; then
523 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
526 # benchmarks are named BenchmarkFoo.
527 pattern
='Benchmark([^a-z].*)?'
528 benchmarks
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
530 # examples are named ExampleFoo
531 pattern
='Example([^a-z].*)?'
532 examples
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
538 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
539 echo 'import "./_gotest_"'
542 echo 'import "./_xtest_"'
544 echo 'import "testing"'
545 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
546 if ! test -n "$testmain"; then
547 echo 'import __os__ "os"'
551 echo 'var tests = []testing.InternalTest {'
555 if test "$n" != "TestMain"; then
557 echo ' {"'$n'", '$j'},'
563 # The comment makes the multiline declaration
564 # gofmt-safe even when there are no benchmarks.
565 echo 'var benchmarks = []testing.InternalBenchmark{ //'
570 echo ' {"'$n'", '$j'},'
575 echo 'var examples = []testing.InternalExample{ //'
576 # This doesn't work because we don't pick up the output.
581 # echo ' {"'$n'", '$j', ""},'
589 var matchRe *__regexp__.Regexp
591 func matchString(pat, str string) (result bool, err error) {
592 if matchRe == nil || matchPat != pat {
594 matchRe, err = __regexp__.Compile(matchPat)
599 return matchRe.MatchString(str), nil
603 m := testing.MainStart(matchString, tests, benchmarks, examples)
605 if test -n "$testmain"; then
606 echo " ${testmain}(m)"
608 echo ' __os__.Exit(m.Run())'
616 if test "$trace" = "true"; then
617 echo ${GC} -g -c _testmain.go
619 ${GC} -g -c _testmain.go
621 if test "$trace" = "true"; then
622 echo ${GL} *.o
${GOLIBS}
627 if test "$bench" = ""; then
628 if test "$trace" = "true"; then
629 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
631 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
633 (sleep `expr $timeout + 10`
634 echo > gotest-timeout
635 echo "timed out in gotest" 1>&2
640 if ! test -f gotest-timeout
; then
641 sleeppid
=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
644 if test "$sleeppid" != ""; then
649 if test "$trace" = "true"; then
650 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
652 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
658 rm -rf ..
/..
/testsuite
/*.o
661 if test "$f" = "_obj" ||
test "$f" = "_test"; then
664 rm -rf ..
/..
/testsuite
/$f
666 cp $f ..
/..
/testsuite
/
668 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
674 if test "$testname" != ""; then
675 GOTESTNAME
="$testname"
678 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
679 # Useful when using make check-target-libgo
680 cat libgo.log
>> libgo-all.log
681 cat libgo.
sum >> libgo-all.
sum