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.
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.
50 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
59 basedir=`echo $1 | sed -e 's/^--basedir=//'`
68 goarch=`echo $1 | sed -e 's/^--goarch=//'`
77 goos=`echo $1 | sed -e 's/^--goos=//'`
86 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
95 prefix=`echo $1 | sed -e 's/^--prefix=//'`
108 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
117 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
126 timeout=`echo $1 | sed -e 's/^--timeout=//'`
135 testname=`echo $1 | sed -e 's/^--testname=//'`
144 bench=`echo $1 | sed -e 's/^--bench=//'`
158 gofiles="$gofiles $1"
172 if test $keep = false; then
173 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
175 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
182 srcdir="../../$srcdir"
193 basedir="../../$basedir"
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.
201 # Some tests refer to a ../testdata directory.
202 if test -e $srcdir/../testdata; then
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.
213 for f in `cd $srcdir; ls *.go`; do
219 for f in $pkgfiles; do
220 if test -f $basedir/$f; then
224 elif test -f ../../$f; then
229 echo "file $f not found" 1>&2
233 for f in `cd $srcdir; ls *_test.go`; do
241 for f in $gofiles; do
248 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
254 for f in $pkgfiles; do
255 if test -f $basedir/$f; then
259 elif test -f ../../$f; then
264 echo "file $f not found" 1>&2
273 # Some tests expect the _obj directory created by the gc Makefiles.
276 # Some tests expect the _test directory created by the gc Makefiles.
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
287 if test x$tag2 = x$f; then
295 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
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)
307 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
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)
315 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
316 taglines=`sed '/^package /q' < $f | fgrep '// +build '`
317 if test "$taglines" = ""; then
322 for tags in $taglines; do
329 tag=`echo $tag | sed -e 's/^!//'`
336 $goos | $goarch | cgo)
341 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
343 $goos | $goarch | cgo)
353 if test "$reverse" = true; then
354 if test "$match" = true; then
361 if test "$match" = "true"; then
366 if test "$omatch" = "true"; then
367 gofiles="$gofiles $f"
375 for f in $xgofiles; do
376 gofiles="$gofiles `basename $f`"
382 echo 'no test files found' 1>&2
387 # Run any commands given in sources, like
388 # // gotest: $GC foo.go
389 # to build any test-only dependencies.
392 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
397 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
400 for f in $pkgfiles; do
401 pkgbasefiles="$pkgbasefiles `basename $f`"
408 echo 'no source files found' 1>&2
413 # Split $gofiles into external gofiles (those in *_test packages)
414 # and internal ones (those in the main package).
416 for f in $gofiles; do
417 package=`grep '^package[ ]' $f | sed 1q`
420 xgofiles="$xgofiles $f"
423 ngofiles="$ngofiles $f"
432 if [ "x$xgofiles" != "x" ]; then
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"
448 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
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
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.
477 # Remove the package from the name used with the -test option.
478 echo $1 | sed 's/^.*\.//'
482 # The package main has been renamed to __main__ when imported.
484 echo $1 | sed 's/^main\./__main__./'
490 ppc64*) text="[TD]" ;;
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
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)
518 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
519 echo 'import "./_gotest_"'
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"'
531 echo 'var tests = []testing.InternalTest {'
535 if test "$n" != "TestMain"; then
537 echo ' {"'$n'", '$j'},'
543 # The comment makes the multiline declaration
544 # gofmt-safe even when there are no benchmarks.
545 echo 'var benchmarks = []testing.InternalBenchmark{ //'
550 echo ' {"'$n'", '$j'},'
555 echo 'var examples = []testing.InternalExample{ //'
556 # This doesn't work because we don't pick up the output.
561 # echo ' {"'$n'", '$j', ""},'
569 var matchRe *__regexp__.Regexp
571 func matchString(pat, str string) (result bool, err error) {
572 if matchRe == nil || matchPat != pat {
574 matchRe, err = __regexp__.Compile(matchPat)
579 return matchRe.MatchString(str), nil
583 m := testing.MainStart(matchString, tests, benchmarks, examples)
585 if test -n "$testmain"; then
586 echo " ${testmain}(m)"
588 echo ' __os__.Exit(m.Run())'
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}
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 "$@" &
613 (sleep `expr $timeout + 10`
614 echo > gotest-timeout
615 echo "timed out in gotest" 1>&2
620 if ! test -f gotest-timeout; then
621 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
624 if test "$sleeppid" != ""; then
629 if test "$trace" = "true"; then
630 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
632 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
638 rm -rf ../../testsuite/*.o
641 if test "$f" = "_obj" || test "$f" = "_test"; then
644 rm -rf ../../testsuite/$f
646 cp $f ../../testsuite/
648 ln -s ../$DIR/test/$f ../../testsuite/
654 if test "$testname" != ""; then
655 GOTESTNAME="$testname"
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