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
340 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
345 if test -n "$pkgpath"; then
346 pkgpatharg
="-fgo-pkgpath=$pkgpath"
347 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
348 elif test -n "$prefix"; then
349 prefixarg
="-fgo-prefix=$prefix"
352 if test "$trace" = "true"; then
353 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
355 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
358 mkdir
-p `dirname $package`
359 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
360 if test "$trace" = "true"; then
361 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
363 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
366 # They all compile; now generate the code to call them.
369 # Remove the package from the name used with the -test option.
370 echo $1 |
sed 's/^.*\.//'
374 # The package main has been renamed to __main__ when imported.
376 echo $1 |
sed 's/^main\./__main__./'
382 ppc64
*) text
="[TD]" ;;
385 symtogo
='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
387 # test functions are named TestFoo
388 # the grep -v eliminates methods and other special names
389 # that have multiple dots.
390 pattern
='Test([^a-z].*)?'
391 # The -p option tells GNU nm not to sort.
392 # The -v option tells Solaris nm to sort by value.
393 tests
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
394 if [ "x$tests" = x
]; then
395 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
398 # benchmarks are named BenchmarkFoo.
399 pattern
='Benchmark([^a-z].*)?'
400 benchmarks
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
402 # examples are named ExampleFoo
403 pattern
='Example([^a-z].*)?'
404 examples
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
410 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
411 echo 'import "./_gotest_"'
414 echo 'import "./_xtest_"'
416 echo 'import "testing"'
417 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
420 echo 'var tests = []testing.InternalTest {'
425 echo ' {"'$n'", '$j'},'
430 # The comment makes the multiline declaration
431 # gofmt-safe even when there are no benchmarks.
432 echo 'var benchmarks = []testing.InternalBenchmark{ //'
437 echo ' {"'$n'", '$j'},'
442 echo 'var examples = []testing.InternalExample{ //'
443 # This doesn't work because we don't pick up the output.
448 # echo ' {"'$n'", '$j', ""},'
456 var matchRe *__regexp__.Regexp
458 func matchString(pat, str string) (result bool, err error) {
459 if matchRe == nil || matchPat != pat {
461 matchRe, err = __regexp__.Compile(matchPat)
466 return matchRe.MatchString(str), nil
470 testing.Main(matchString, tests, benchmarks, examples)
476 if test "$trace" = "true"; then
477 echo ${GC} -g -c _testmain.go
479 ${GC} -g -c _testmain.go
481 if test "$trace" = "true"; then
482 echo ${GL} *.o
${GOLIBS}
486 if test "$bench" = ""; then
487 if test "$trace" = "true"; then
488 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
490 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
492 (sleep `expr $timeout + 10`
493 echo > gotest-timeout
494 echo "timed out in gotest" 1>&2
499 if ! test -f gotest-timeout
; then
503 if test "$trace" = "true"; then
504 echo .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
506 .
/a.out
-test.run
=^\$
-test.bench
="${bench}" "$@"
512 rm -rf ..
/..
/testsuite
/*.o
515 if test "$f" = "_obj" ||
test "$f" = "_test"; then
518 rm -rf ..
/..
/testsuite
/$f
520 cp $f ..
/..
/testsuite
/
522 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
528 if test "$testname" != ""; then
529 GOTESTNAME
="$testname"
532 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
533 # Useful when using make check-target-libgo
534 cat libgo.log
>> libgo-all.log
535 cat libgo.
sum >> libgo-all.
sum