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.
49 srcdir
=`echo $1 | sed -e 's/^--srcdir=//'`
58 basedir
=`echo $1 | sed -e 's/^--basedir=//'`
67 pkgpath
=`echo $1 | sed -e 's/^--pkgpath=//'`
76 prefix
=`echo $1 | sed -e 's/^--prefix=//'`
89 pkgfiles
=`echo $1 | sed -e 's/^--pkgfiles=//'`
98 dejagnu
=`echo $1 | sed -e 's/^--dejagnu=//'`
107 GOARCH
=`echo $1 | sed -e 's/^--goarch=//'`
116 timeout
=`echo $1 | sed -e 's/^--timeout=//'`
125 testname
=`echo $1 | sed -e 's/^--testname=//'`
134 bench
=`echo $1 | sed -e 's/^--bench=//'`
148 gofiles
="$gofiles $1"
162 if test $keep = false
; then
163 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
165 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
172 srcdir
="../../$srcdir"
183 basedir
="../../$basedir"
187 # Link all the files/directories in srcdir into our working directory,
188 # so that the tests do not have to refer to srcdir to find test data.
191 # Some tests refer to a ../testdata directory.
192 if test -e $srcdir/..
/testdata
; then
194 abssrcdir
=`cd $srcdir && pwd`
195 ln -s $abssrcdir/..
/testdata ..
/testdata
198 # Copy the .go files because io/utils_test.go expects a regular file.
203 for f
in `cd $srcdir; ls *.go`; do
209 for f
in $pkgfiles; do
210 if test -f $basedir/$f; then
214 elif test -f ..
/..
/$f; then
219 echo "file $f not found" 1>&2
223 for f
in `cd $srcdir; ls *_test.go`; do
231 for f
in $gofiles; do
238 for f
in `cd $srcdir; ls *.go | grep -v *_test.go`; do
244 for f
in $pkgfiles; do
245 if test -f $basedir/$f; then
249 elif test -f ..
/..
/$f; then
254 echo "file $f not found" 1>&2
263 # Some tests expect the _obj directory created by the gc Makefiles.
266 # Some tests expect the _test directory created by the gc Makefiles.
271 gofiles
=`ls *_test.go 2>/dev/null`
276 for f
in $xgofiles; do
277 gofiles
="$gofiles `basename $f`"
283 echo 'no test files found' 1>&2
288 # Run any commands given in sources, like
289 # // gotest: $GC foo.go
290 # to build any test-only dependencies.
293 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
298 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
301 for f
in $pkgfiles; do
302 pkgbasefiles
="$pkgbasefiles `basename $f`"
309 echo 'no source files found' 1>&2
314 # Split $gofiles into external gofiles (those in *_test packages)
315 # and internal ones (those in the main package).
317 for f
in $gofiles; do
318 package
=`grep '^package[ ]' $f | sed 1q`
321 xgofiles
="$xgofiles $f"
324 ngofiles
="$ngofiles $f"
333 if [ "x$xgofiles" != "x" ]; then
339 if $havex && fgrep
'func TestMain(' $xgofiles >/dev
/null
2>&1; then
340 package
=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
341 testmain
="${package}.TestMain"
342 elif test -n "$gofiles" && fgrep
'func TestMain(' $gofiles >/dev
/null
2>&1; then
343 package
=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
344 testmain
="${package}.TestMain"
349 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
354 if test -n "$pkgpath"; then
355 pkgpatharg
="-fgo-pkgpath=$pkgpath"
356 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
357 elif test -n "$prefix"; then
358 prefixarg
="-fgo-prefix=$prefix"
361 if test "$trace" = "true"; then
362 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
364 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
367 mkdir
-p `dirname $package`
368 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
369 if test "$trace" = "true"; then
370 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
372 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
375 # They all compile; now generate the code to call them.
378 # Remove the package from the name used with the -test option.
379 echo $1 |
sed 's/^.*\.//'
383 # The package main has been renamed to __main__ when imported.
385 echo $1 |
sed 's/^main\./__main__./'
391 ppc64
*) text
="[TD]" ;;
394 symtogo
='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
396 # test functions are named TestFoo
397 # the grep -v eliminates methods and other special names
398 # that have multiple dots.
399 pattern
='Test([^a-z].*)?'
400 # The -p option tells GNU nm not to sort.
401 # The -v option tells Solaris nm to sort by value.
402 tests
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
403 if [ "x$tests" = x
]; then
404 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
407 # benchmarks are named BenchmarkFoo.
408 pattern
='Benchmark([^a-z].*)?'
409 benchmarks
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
411 # examples are named ExampleFoo
412 pattern
='Example([^a-z].*)?'
413 examples
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
419 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
420 echo 'import "./_gotest_"'
423 echo 'import "./_xtest_"'
425 echo 'import "testing"'
426 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
427 if ! test -n "$testmain"; then
428 echo 'import __os__ "os"'
432 echo 'var tests = []testing.InternalTest {'
436 if test "$n" != "TestMain"; then
438 echo ' {"'$n'", '$j'},'
444 # The comment makes the multiline declaration
445 # gofmt-safe even when there are no benchmarks.
446 echo 'var benchmarks = []testing.InternalBenchmark{ //'
451 echo ' {"'$n'", '$j'},'
456 echo 'var examples = []testing.InternalExample{ //'
457 # This doesn't work because we don't pick up the output.
462 # echo ' {"'$n'", '$j', ""},'
470 var matchRe *__regexp__.Regexp
472 func matchString(pat, str string) (result bool, err error) {
473 if matchRe == nil || matchPat != pat {
475 matchRe, err = __regexp__.Compile(matchPat)
480 return matchRe.MatchString(str), nil
484 m := testing.MainStart(matchString, tests, benchmarks, examples)
486 if test -n "$testmain"; then
487 echo " ${testmain}(m)"
489 echo ' __os__.Exit(m.Run())'
497 if test "$trace" = "true"; then
498 echo ${GC} -g -c _testmain.go
500 ${GC} -g -c _testmain.go
502 if test "$trace" = "true"; then
503 echo ${GL} *.o
${GOLIBS}
507 if test "$bench" = ""; then
508 if test "$trace" = "true"; then
509 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
511 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
513 (sleep `expr $timeout + 10`
514 echo > gotest-timeout
515 echo "timed out in gotest" 1>&2
520 if ! test -f gotest-timeout
; then
524 if test "$trace" = "true"; then
525 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
527 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
533 rm -rf ..
/..
/testsuite
/*.o
536 if test "$f" = "_obj" ||
test "$f" = "_test"; then
539 rm -rf ..
/..
/testsuite
/$f
541 cp $f ..
/..
/testsuite
/
543 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
549 if test "$testname" != ""; then
550 GOTESTNAME
="$testname"
553 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
554 # Useful when using make check-target-libgo
555 cat libgo.log
>> libgo-all.log
556 cat libgo.
sum >> libgo-all.
sum