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 | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos
)
311 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm
)
315 # File name like x_amd64_random.go, where tag1=random.
316 # Don't match based on tag2.
325 aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos
)
328 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm
)
333 if test x
$tag1 != xnonmatchingtag
-a x
$tag2 != xnonmatchingtag
; then
334 tags
=`sed '/^package /q' < $f | grep '^// *+build '`
342 "+build" |
"//+build")
343 if test "$first" = "true"; then
345 elif test "$match" = "false"; then
350 $goos |
$goarch | cgo | gccgo | goexperiment.fieldtrack | go1.
[0-9] | go1.
[0-9][0-9])
353 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!goexperiment.fieldtrack" |
"!"go1.
[0-9] |
"!"go1.1
[0-7])
357 for ctag
in `echo $tag | sed -e 's/,/ /g'`; do
359 $goos |
$goarch | cgo | gccgo | goexperiment.fieldtrack | go1.
[0-9] | go1.
[0-9][0-9])
361 "!"$goos |
"!"$goarch |
"!cgo" |
"!gccgo" |
"!goexperiment.fieldtrack" |
"!"go1.
[0-9] |
"!"go1.1
[0-7])
371 if test "$cmatch" = "true"; then
381 if test "$match" = "false" -a "$first" = "false"; then
385 if test "$omatch" = "true"; then
386 gofiles
="$gofiles $f"
394 for f
in $xgofiles; do
395 gofiles
="$gofiles `basename $f`"
401 echo 'no test files found' 1>&2
408 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
411 for f
in $pkgfiles; do
412 pkgbasefiles
="$pkgbasefiles `basename $f`"
419 echo 'no source files found' 1>&2
424 # Split $gofiles into external gofiles (those in *_test packages)
425 # and internal ones (those in the main package).
428 for f
in $gofiles; do
429 package
=`grep '^package[ ]' $f | sed 1q`
432 xpackage
=`echo $package | sed -e 's/package[ ]//' -e 's/[ ]*$//'`
433 xgofiles
="$xgofiles $f"
436 ngofiles
="$ngofiles $f"
445 if [ "x$xgofiles" != "x" ]; then
451 if $havex && fgrep
'func TestMain(' $xgofiles >/dev
/null
2>&1; then
452 package
=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
453 testmain
="${package}.TestMain"
454 elif test -n "$gofiles" && fgrep
'func TestMain(' $gofiles >/dev
/null
2>&1; then
455 package
=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
456 testmain
="${package}.TestMain"
461 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
466 if test -n "$pkgpath"; then
467 pkgpatharg
="-fgo-pkgpath=$pkgpath"
468 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
469 elif test -n "$prefix"; then
470 prefixarg
="-fgo-prefix=$prefix"
473 if test "$trace" = "true"; then
474 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
476 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
479 mkdir
-p `dirname $package`
480 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
482 # Force the test version of the package to be imported first,
483 # so that its type definitions will be used, in case any new
484 # methods appear in export_test.go files.
485 echo "package $xpackage" > _first_test.go
486 echo 'import _ "'$package'"' >> _first_test.go
488 if test "$trace" = "true"; then
489 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile _first_test.go
$xgofiles
491 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile _first_test.go
$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 # Also demangle underscores.
505 echo $1 |
sed 's/^main\./__main__./' |
sed 's/__/_/'
508 # Takes a list of tests derived from 'nm' output (whose symbols are mangled)
509 # and emits a demangled list of tests, using only the terminal package.
512 # Original symbol: foo/bar/leaf.Mumble
513 # Mangled symbol: foo_1fbar_1leaf.Mumble
514 # Returned: leaf.Mumble
519 # Discard symbols with a leading dot.
520 # On AIX, this will remove function text symbols (with a leading dot).
521 # Therefore, only function descriptor symbols (without this leading dot)
522 # will be used to retrieve the go symbols, avoiding duplication.
523 if expr "$tp" : '^\.' >/dev
/null
2>&1; then
526 # Skip type descriptors. These are normally skipped because they
527 # are weak symbols, but if not using GNU nm we may see them here.
528 if expr "$tp" : '^type\.\.' >/dev
/null
2>&1; then
531 s
=$
(echo "$tp" |
sed -e 's/_1/%/g' |
sed -e 's/.*%//')
532 # Screen out methods (X.Y.Z).
533 if ! expr "$s" : '^[^.]*\.[^.]*$' >/dev
/null
2>&1; then
538 if test x
$tname = xTestMain
; then
541 # Check that the function is defined in a test file,
542 # not an ordinary non-test file.
543 if grep "^func $tname(" $gofiles $xgofiles >/dev
/null
2>&1; then
549 # Takes an example name and puts any output into the file example.txt.
550 # It strips comment markers but does not otherwise change the output.
553 for f
in $gofiles $xgofiles; do
554 if ! grep "^func $n(" $f >/dev
/null
2>&1; then
557 # Copy the output comment, if any, into example.txt.
558 # Remove the comment markers.
559 sed -n "/^func $n(/,/^}$/ p" $f |
560 sed -n '\|// \([Uu]nordered \)\?[Oo]utput:|,$ p' |
561 sed -n '\|//| s|[ ]*// \?||p' > example.txt
562 # Check whether we found an output comment.
563 if ! sed -n '1p' < example.txt |
grep '[Oo]utput:' >/dev
/null
2>&1; then
571 # On systems using PPC64 ELF ABI v1 function symbols show up
572 # as descriptors in the data section.
575 # test functions are named TestFoo
576 # the grep -v eliminates methods and other special names
577 # that have multiple dots.
578 pattern
='Test([^a-z].*)?'
579 # The -p option tells GNU nm not to sort.
580 # The -v option tells Solaris nm to sort by value.
581 testsyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
584 testxsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
585 testsyms
="$testsyms $testxsyms"
587 tests
=$
(symtogo
"$testsyms")
588 if [ "x$tests" = x
]; then
589 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
592 # benchmarks are named BenchmarkFoo.
593 pattern
='Benchmark([^a-z].*)?'
594 benchmarksyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
596 benchmarkxsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
597 benchmarksyms
="$benchmarksyms $benchmarkxsyms"
599 benchmarks
=$
(symtogo
"$benchmarksyms")
601 # examples are named ExampleFoo
602 pattern
='Example([^a-z].*)?'
603 examplesyms
=$
($NM -p -v _gotest_.o |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
605 examplexsyms
=$
($NM -p -v $xofile |
egrep " $text .*\."$pattern'$' | fgrep
-v ' __go_' |
egrep -v '\.\.\w+$' |
sed 's/.* //')
606 examplesyms
="$examplesyms $examplexsyms"
608 examples
=$
(symtogo
"$examplesyms")
614 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
615 echo 'import "./_gotest_"'
619 if test -n "$testxsyms" -o -n "$benchmarkxsyms"; then
622 # Check whether any example has output.
623 for i
in $
(symtogo
"$examplexsyms"); do
625 if test -f example.txt
; then
632 if test x
$needxtest = xtrue
; then
633 echo 'import "./_xtest_"'
635 echo 'import _ "./_xtest_"'
638 if test "$package" != "testing"; then
639 echo 'import "testing"'
641 echo 'import "testing/internal/testdeps"'
642 if ! test -n "$testmain"; then
643 echo 'import __os__ "os"'
647 echo 'var tests = []testing.InternalTest {'
651 echo ' {"'$n'", '$j'},'
656 # The comment makes the multiline declaration
657 # gofmt-safe even when there are no benchmarks.
658 echo 'var benchmarks = []testing.InternalBenchmark{'
659 for i
in $benchmarks; do
662 echo ' {"'$n'", '$j'},'
667 echo 'var examples = []testing.InternalExample{'
668 for i
in $examples; do
671 # Look for a //output comment.
676 if ! test -f example.txt
; then
679 # Check whether the output can be unordered.
681 if sed -n '1p' < example.txt |
grep -i unordered
>/dev
/null
2>&1; then
684 # Remove the output header.
686 # Quote quotation characters.
688 # Turn pairs of spaces into " \x20", because $() will
689 # drop duplicate spaces.
690 # Drop trailing spaces, and turn newlines into \n.
691 # Remove leading and trailing \n.
692 sed '1 s/\([Uu]nordered \)\?[Oo]utput:[ ]*//' < example.txt |
693 sed -e 's/\\/\\\\/g' \
699 sed -e 's/^\(\\n\)*//' \
700 -e 's/\(\\n\)*$//' > example2.txt
702 echo ' {"'$n'", '$j','
703 sed -e 's/^/ "/' -e 's/$/", /' < example2.txt
705 rm -f example.txt example2.txt
713 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
715 if test -n "$testmain"; then
716 echo " ${testmain}(m)"
718 echo ' __os__.Exit(m.Run())'
726 if test "$trace" = "true"; then
727 echo ${GC} -g -c _testmain.go
729 ${GC} -g -c _testmain.go
731 if test "$trace" = "true"; then
732 echo ${GL} *.o
${GOLIBS}
737 if test "$bench" = ""; then
738 if test "$trace" = "true"; then
739 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
741 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
743 (sleep `expr $timeout + 10`
744 echo > gotest-timeout
745 echo "timed out in gotest" 1>&2
750 if ! test -f gotest-timeout
; then
751 sleeppid
=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
754 if test "$sleeppid" != ""; then
759 if test "$trace" = "true"; then
760 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
762 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
768 rm -rf ..
/..
/testsuite
/*.o
771 if test "$f" = "_obj" ||
test "$f" = "_test"; then
774 rm -rf ..
/..
/testsuite
/$f
776 cp $f ..
/..
/testsuite
/
778 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
784 if test "$testname" != ""; then
785 GOTESTNAME
="$testname"
788 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
789 # Useful when using make check-target-libgo
790 cat libgo.log
>> libgo-all.log
791 cat libgo.
sum >> libgo-all.
sum