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
294 for f
in `ls *_test.go`; do
295 tag1
=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
296 tag2
=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
297 if test x
$tag1 = x
$f; then
300 if test x
$tag2 = x
$f; then
308 aix | android | darwin | dragonfly | freebsd | hurd | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
311 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
)
320 aix | android | darwin | dragonfly | freebsd | hurd | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows
)
323 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
)
328 if test x
$tag1 != xnonmatchingtag
-a x
$tag2 != xnonmatchingtag
; then
329 tags
=`sed '/^package /q' < $f | grep '^// *+build '`
337 "+build" |
"//+build")
338 if test "$first" = "true"; then
340 elif test "$match" = "false"; then
345 $goos |
$goarch | cgo | gccgo | go1.
[0-9])
348 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!"go1.
[0-9])
352 for ctag
in `echo $tag | sed -e 's/,/ /g'`; do
354 $goos |
$goarch | cgo | gccgo | go1.
[0-9])
356 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!"go1.
[0-9])
366 if test "$cmatch" = "true"; then
376 if test "$match" = "false" -a "$first" = "false"; then
380 if test "$omatch" = "true"; then
381 gofiles
="$gofiles $f"
389 for f
in $xgofiles; do
390 gofiles
="$gofiles `basename $f`"
396 echo 'no test files found' 1>&2
403 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
406 for f
in $pkgfiles; do
407 pkgbasefiles
="$pkgbasefiles `basename $f`"
414 echo 'no source files found' 1>&2
419 # Split $gofiles into external gofiles (those in *_test packages)
420 # and internal ones (those in the main package).
423 for f
in $gofiles; do
424 package
=`grep '^package[ ]' $f | sed 1q`
427 xpackage
=`echo $package | sed -e 's/package[ ]//' -e 's/[ ]*$//'`
428 xgofiles
="$xgofiles $f"
431 ngofiles
="$ngofiles $f"
440 if [ "x$xgofiles" != "x" ]; then
446 if $havex && fgrep
'func TestMain(' $xgofiles >/dev
/null
2>&1; then
447 package
=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
448 testmain
="${package}.TestMain"
449 elif test -n "$gofiles" && fgrep
'func TestMain(' $gofiles >/dev
/null
2>&1; then
450 package
=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
451 testmain
="${package}.TestMain"
456 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
461 if test -n "$pkgpath"; then
462 pkgpatharg
="-fgo-pkgpath=$pkgpath"
463 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
464 elif test -n "$prefix"; then
465 prefixarg
="-fgo-prefix=$prefix"
468 if test "$trace" = "true"; then
469 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
471 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
474 mkdir
-p `dirname $package`
475 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
477 # Force the test version of the package to be imported first,
478 # so that its type definitions will be used, in case any new
479 # methods appear in export_test.go files.
480 echo "package $xpackage" > _first_test.go
481 echo 'import _ "'$package'"' >> _first_test.go
483 if test "$trace" = "true"; then
484 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile _first_test.go
$xgofiles
486 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile _first_test.go
$xgofiles
489 # They all compile; now generate the code to call them.
492 # Remove the package from the name used with the -test option.
493 echo $1 |
sed 's/^.*\.//'
497 # The package main has been renamed to __main__ when imported.
499 echo $1 |
sed 's/^main\./__main__./'
502 # Takes a list of tests derived from 'nm' output (whose symbols are mangled)
503 # and emits a demangled list of tests, using only the terminal package.
506 # Original symbol: foo/bar/leaf.Mumble
507 # Mangled symbol: foo..z2fbar..z2fleaf.Mumble
508 # Returned: leaf.Mumble
513 # Discard symbols with a leading dot.
514 # On AIX, this will remove function text symbols (with a leading dot).
515 # Therefore, only function descriptor symbols (without this leading dot)
516 # will be used to retrieve the go symbols, avoiding duplication.
517 if expr "$tp" : '^\.' >/dev
/null
2>&1; then
520 # Skip type descriptors. These are normally skipped because they
521 # are weak symbols, but if not using GNU nm we may see them here.
522 if expr "$tp" : '^type\.\.' >/dev
/null
2>&1; then
525 s
=$
(echo "$tp" |
sed -e 's/\.\.z2f/%/g' |
sed -e 's/.*%//')
526 # Screen out methods (X.Y.Z).
527 if ! expr "$s" : '^[^.]*\.[^.]*$' >/dev
/null
2>&1; then
532 if test x
$tname = xTestMain
; then
535 # Check that the function is defined in a test file,
536 # not an ordinary non-test file.
537 if grep "^func $tname(" $gofiles $xgofiles >/dev
/null
2>&1; then
543 # Takes an example name and puts any output into the file example.txt.
544 # It strips comment markers but does not otherwise change the output.
547 for f
in $gofiles $xgofiles; do
548 if ! grep "^func $n(" $f >/dev
/null
2>&1; then
551 # Copy the output comment, if any, into example.txt.
552 # Remove the comment markers.
553 sed -n "/^func $n(/,/^}$/ p" $f |
554 sed -n '\|// \([Uu]nordered \)\?[Oo]utput:|,$ p' |
555 sed -n '\|//| s|[ ]*// \?||p' > example.txt
556 # Check whether we found an output comment.
557 if ! sed -n '1p' < example.txt |
grep '[Oo]utput:' >/dev
/null
2>&1; then
565 # On systems using PPC64 ELF ABI v1 function symbols show up
566 # as descriptors in the data section.
569 # test functions are named TestFoo
570 # the grep -v eliminates methods and other special names
571 # that have multiple dots.
572 pattern
='Test([^a-z].*)?'
573 # The -p option tells GNU nm not to sort.
574 # The -v option tells Solaris nm to sort by value.
575 testsyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
578 testxsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
579 testsyms
="$testsyms $testxsyms"
581 tests
=$
(symtogo
"$testsyms")
582 if [ "x$tests" = x
]; then
583 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
586 # benchmarks are named BenchmarkFoo.
587 pattern
='Benchmark([^a-z].*)?'
588 benchmarksyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
590 benchmarkxsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
591 benchmarksyms
="$benchmarksyms $benchmarkxsyms"
593 benchmarks
=$
(symtogo
"$benchmarksyms")
595 # examples are named ExampleFoo
596 pattern
='Example([^a-z].*)?'
597 examplesyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
599 examplexsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
600 examplesyms
="$examplesyms $examplexsyms"
602 examples
=$
(symtogo
"$examplesyms")
608 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
609 echo 'import "./_gotest_"'
613 if test -n "$testxsyms" -o -n "$benchmarkxsyms"; then
616 # Check whether any example has output.
617 for i
in $
(symtogo
"$examplexsyms"); do
619 if test -f example.txt
; then
626 if test x
$needxtest = xtrue
; then
627 echo 'import "./_xtest_"'
629 echo 'import _ "./_xtest_"'
632 if test "$package" != "testing"; then
633 echo 'import "testing"'
635 echo 'import "testing/internal/testdeps"'
636 if ! test -n "$testmain"; then
637 echo 'import __os__ "os"'
641 echo 'var tests = []testing.InternalTest {'
645 echo ' {"'$n'", '$j'},'
650 # The comment makes the multiline declaration
651 # gofmt-safe even when there are no benchmarks.
652 echo 'var benchmarks = []testing.InternalBenchmark{'
653 for i
in $benchmarks; do
656 echo ' {"'$n'", '$j'},'
661 echo 'var examples = []testing.InternalExample{'
662 for i
in $examples; do
665 # Look for a //output comment.
670 if ! test -f example.txt
; then
673 # Check whether the output can be unordered.
675 if sed -n '1p' < example.txt |
grep -i unordered
>/dev
/null
2>&1; then
678 # Remove the output header.
680 # Quote quotation characters.
682 # Turn pairs of spaces into " \x20", because $() will
683 # drop duplicate spaces.
684 # Drop trailing spaces, and turn newlines into \n.
685 # Remove leading and trailing \n.
686 sed '1 s/\([Uu]nordered \)\?[Oo]utput:[ ]*//' < example.txt |
687 sed -e 's/\\/\\\\/g' \
693 sed -e 's/^\(\\n\)*//' \
694 -e 's/\(\\n\)*$//' > example2.txt
696 echo ' {"'$n'", '$j','
697 sed -e 's/^/ "/' -e 's/$/", /' < example2.txt
699 rm -f example.txt example2.txt
707 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
709 if test -n "$testmain"; then
710 echo " ${testmain}(m)"
712 echo ' __os__.Exit(m.Run())'
720 if test "$trace" = "true"; then
721 echo ${GC} -g -c _testmain.go
723 ${GC} -g -c _testmain.go
725 if test "$trace" = "true"; then
726 echo ${GL} *.o
${GOLIBS}
731 if test "$bench" = ""; then
732 if test "$trace" = "true"; then
733 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
735 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
737 (sleep `expr $timeout + 10`
738 echo > gotest-timeout
739 echo "timed out in gotest" 1>&2
744 if ! test -f gotest-timeout
; then
745 sleeppid
=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
748 if test "$sleeppid" != ""; then
753 if test "$trace" = "true"; then
754 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
756 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
762 rm -rf ..
/..
/testsuite
/*.o
765 if test "$f" = "_obj" ||
test "$f" = "_test"; then
768 rm -rf ..
/..
/testsuite
/$f
770 cp $f ..
/..
/testsuite
/
772 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
778 if test "$testname" != ""; then
779 GOTESTNAME
="$testname"
782 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
783 # Useful when using make check-target-libgo
784 cat libgo.log
>> libgo-all.log
785 cat libgo.
sum >> libgo-all.
sum