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 aix | android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
317 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le·| nios2 | ppc | ppc64 | ppc64le | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm
)
326 aix | android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
329 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le·| nios2 | ppc | ppc64 | ppc64le | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm
)
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 | gccgo | go1.
[0-9])
354 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!"go1.
[0-9])
358 for ctag
in `echo $tag | sed -e 's/,/ /g'`; do
360 $goos |
$goarch | cgo | gccgo | go1.
[0-9])
362 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!"go1.
[0-9])
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__./'
507 # Takes a list of tests derived from 'nm' output (whose symbols are mangled)
508 # and emits a demangled list of tests, using only the terminal package.
511 # Original symbol: foo/bar/leaf.Mumble
512 # Mangled symbol: foo..z2fbar..z2fleaf.Mumble
513 # Returned: leaf.Mumble
519 s
=$
(echo "$tp" |
sed -e 's/\.\.z2f/%/g' |
sed -e 's/.*%//')
520 # screen out methods (X.Y.Z)
521 if ! expr "$s" : '^[^.]*\.[^.]*$' >/dev
/null
2>&1; then
531 # On systems using PPC64 ELF ABI v1 function symbols show up
532 # as descriptors in the data section. We assume that $goarch
533 # distinguishes v1 (ppc64) from v2 (ppc64le).
534 if test "$goos" != "aix" && test "$goarch" = "ppc64"; then
538 # test functions are named TestFoo
539 # the grep -v eliminates methods and other special names
540 # that have multiple dots.
541 pattern
='Test([^a-z].*)?'
542 # The -p option tells GNU nm not to sort.
543 # The -v option tells Solaris nm to sort by value.
544 testsyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
545 tests
=$
(symtogo
"$testsyms")
546 if [ "x$tests" = x
]; then
547 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
550 # benchmarks are named BenchmarkFoo.
551 pattern
='Benchmark([^a-z].*)?'
552 benchmarksyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
553 benchmarks
=$
(symtogo
"$benchmarksyms")
555 # examples are named ExampleFoo
556 pattern
='Example([^a-z].*)?'
557 examplesyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
558 examples
=$
(symtogo
"$examplesyms")
564 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
565 echo 'import "./_gotest_"'
568 echo 'import "./_xtest_"'
570 echo 'import "testing"'
571 echo 'import "testing/internal/testdeps"'
572 if ! test -n "$testmain"; then
573 echo 'import __os__ "os"'
577 echo 'var tests = []testing.InternalTest {'
581 if test "$n" != "TestMain"; then
583 echo ' {"'$n'", '$j'},'
589 # The comment makes the multiline declaration
590 # gofmt-safe even when there are no benchmarks.
591 echo 'var benchmarks = []testing.InternalBenchmark{ //'
596 echo ' {"'$n'", '$j'},'
601 echo 'var examples = []testing.InternalExample{ //'
602 # This doesn't work because we don't pick up the output.
607 # echo ' {"'$n'", '$j', ""},'
615 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
617 if test -n "$testmain"; then
618 echo " ${testmain}(m)"
620 echo ' __os__.Exit(m.Run())'
628 if test "$trace" = "true"; then
629 echo ${GC} -g -c _testmain.go
631 ${GC} -g -c _testmain.go
633 if test "$trace" = "true"; then
634 echo ${GL} *.o
${GOLIBS}
639 if test "$bench" = ""; then
640 if test "$trace" = "true"; then
641 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
643 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
645 (sleep `expr $timeout + 10`
646 echo > gotest-timeout
647 echo "timed out in gotest" 1>&2
652 if ! test -f gotest-timeout
; then
653 sleeppid
=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
656 if test "$sleeppid" != ""; then
661 if test "$trace" = "true"; then
662 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
664 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
670 rm -rf ..
/..
/testsuite
/*.o
673 if test "$f" = "_obj" ||
test "$f" = "_test"; then
676 rm -rf ..
/..
/testsuite
/$f
678 cp $f ..
/..
/testsuite
/
680 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
686 if test "$testname" != ""; then
687 GOTESTNAME
="$testname"
690 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
691 # Useful when using make check-target-libgo
692 cat libgo.log
>> libgo-all.log
693 cat libgo.
sum >> libgo-all.
sum