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
521 s
=$
(echo $tp |
sed -e 's/\.\.z2f/%/g' |
sed -e 's/.*%//')
522 # screen out methods (X.Y.Z)
523 ndots
=$
(echo $s |
sed -e 's/\./ /g' |
wc -w)
524 if [ $ndots -ne 2 ]; then
527 if [ -z "${result}" ]; then
530 result
="${result} ${s}"
533 echo "$result" |
sed -e 's/ /\n/g'
539 # On systems using PPC64 ELF ABI v1 function symbols show up
540 # as descriptors in the data section. We assume that $goarch
541 # distinguishes v1 (ppc64) from v2 (ppc64le).
542 if test "$goos" != "aix" && test "$goarch" = "ppc64"; then
546 # test functions are named TestFoo
547 # the grep -v eliminates methods and other special names
548 # that have multiple dots.
549 pattern
='Test([^a-z].*)?'
550 # The -p option tells GNU nm not to sort.
551 # The -v option tells Solaris nm to sort by value.
552 testsyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
553 tests
=$
(symtogo
"$testsyms")
554 if [ "x$tests" = x
]; then
555 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
558 # benchmarks are named BenchmarkFoo.
559 pattern
='Benchmark([^a-z].*)?'
560 benchmarksyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
561 benchmarks
=$
(symtogo
"$benchmarksyms")
563 # examples are named ExampleFoo
564 pattern
='Example([^a-z].*)?'
565 examplesyms
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
566 examples
=$
(symtogo
"$examplesyms")
572 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
573 echo 'import "./_gotest_"'
576 echo 'import "./_xtest_"'
578 echo 'import "testing"'
579 echo 'import "testing/internal/testdeps"'
580 if ! test -n "$testmain"; then
581 echo 'import __os__ "os"'
585 echo 'var tests = []testing.InternalTest {'
589 if test "$n" != "TestMain"; then
591 echo ' {"'$n'", '$j'},'
597 # The comment makes the multiline declaration
598 # gofmt-safe even when there are no benchmarks.
599 echo 'var benchmarks = []testing.InternalBenchmark{ //'
604 echo ' {"'$n'", '$j'},'
609 echo 'var examples = []testing.InternalExample{ //'
610 # This doesn't work because we don't pick up the output.
615 # echo ' {"'$n'", '$j', ""},'
623 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
625 if test -n "$testmain"; then
626 echo " ${testmain}(m)"
628 echo ' __os__.Exit(m.Run())'
636 if test "$trace" = "true"; then
637 echo ${GC} -g -c _testmain.go
639 ${GC} -g -c _testmain.go
641 if test "$trace" = "true"; then
642 echo ${GL} *.o
${GOLIBS}
647 if test "$bench" = ""; then
648 if test "$trace" = "true"; then
649 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
651 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
653 (sleep `expr $timeout + 10`
654 echo > gotest-timeout
655 echo "timed out in gotest" 1>&2
660 if ! test -f gotest-timeout
; then
661 sleeppid
=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
664 if test "$sleeppid" != ""; then
669 if test "$trace" = "true"; then
670 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
672 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
678 rm -rf ..
/..
/testsuite
/*.o
681 if test "$f" = "_obj" ||
test "$f" = "_test"; then
684 rm -rf ..
/..
/testsuite
/$f
686 cp $f ..
/..
/testsuite
/
688 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
694 if test "$testname" != ""; then
695 GOTESTNAME
="$testname"
698 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
699 # Useful when using make check-target-libgo
700 cat libgo.log
>> libgo-all.log
701 cat libgo.
sum >> libgo-all.
sum