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.
48 srcdir
=`echo $1 | sed -e 's/^--srcdir=//'`
57 basedir
=`echo $1 | sed -e 's/^--basedir=//'`
66 pkgpath
=`echo $1 | sed -e 's/^--pkgpath=//'`
75 prefix
=`echo $1 | sed -e 's/^--prefix=//'`
88 pkgfiles
=`echo $1 | sed -e 's/^--pkgfiles=//'`
97 dejagnu
=`echo $1 | sed -e 's/^--dejagnu=//'`
106 GOARCH
=`echo $1 | sed -e 's/^--goarch=//'`
115 timeout
=`echo $1 | sed -e 's/^--timeout=//'`
124 testname
=`echo $1 | sed -e 's/^--testname=//'`
138 gofiles
="$gofiles $1"
152 if test $keep = false
; then
153 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
155 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
162 srcdir
="../../$srcdir"
173 basedir
="../../$basedir"
177 # Link all the files/directories in srcdir into our working directory,
178 # so that the tests do not have to refer to srcdir to find test data.
181 # Some tests refer to a ../testdata directory.
182 if test -e $srcdir/..
/testdata
; then
184 abssrcdir
=`cd $srcdir && pwd`
185 ln -s $abssrcdir/..
/testdata ..
/testdata
188 # Copy the .go files because io/utils_test.go expects a regular file.
193 for f
in `cd $srcdir; ls *.go`; do
199 for f
in $pkgfiles; do
200 if test -f $basedir/$f; then
204 elif test -f ..
/..
/$f; then
209 echo "file $f not found" 1>&2
213 for f
in `cd $srcdir; ls *_test.go`; do
221 for f
in $gofiles; do
228 for f
in `cd $srcdir; ls *.go | grep -v *_test.go`; do
234 for f
in $pkgfiles; do
235 if test -f $basedir/$f; then
239 elif test -f ..
/..
/$f; then
244 echo "file $f not found" 1>&2
253 # Some tests expect the _obj directory created by the gc Makefiles.
256 # Some tests expect the _test directory created by the gc Makefiles.
261 gofiles
=`ls *_test.go 2>/dev/null`
266 for f
in $xgofiles; do
267 gofiles
="$gofiles `basename $f`"
273 echo 'no test files found' 1>&2
278 # Run any commands given in sources, like
279 # // gotest: $GC foo.go
280 # to build any test-only dependencies.
283 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
288 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
291 for f
in $pkgfiles; do
292 pkgbasefiles
="$pkgbasefiles `basename $f`"
299 echo 'no source files found' 1>&2
304 # Split $gofiles into external gofiles (those in *_test packages)
305 # and internal ones (those in the main package).
307 for f
in $gofiles; do
308 package
=`grep '^package[ ]' $f | sed 1q`
311 xgofiles
="$xgofiles $f"
314 ngofiles
="$ngofiles $f"
323 if [ "x$xgofiles" != "x" ]; then
330 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
335 if test -n "$pkgpath"; then
336 pkgpatharg
="-fgo-pkgpath=$pkgpath"
337 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
338 elif test -n "$prefix"; then
339 prefixarg
="-fgo-prefix=$prefix"
342 if test "$trace" = "true"; then
343 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
345 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
348 mkdir
-p `dirname $package`
349 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
350 if test "$trace" = "true"; then
351 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
353 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
356 # They all compile; now generate the code to call them.
359 # Remove the package from the name used with the -test option.
360 echo $1 |
sed 's/^.*\.//'
364 # The package main has been renamed to __main__ when imported.
366 echo $1 |
sed 's/^main\./__main__./'
372 ppc64
) text
="[TD]" ;;
375 symtogo
='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
377 # test functions are named TestFoo
378 # the grep -v eliminates methods and other special names
379 # that have multiple dots.
380 pattern
='Test([^a-z].*)?'
381 # The -p option tells GNU nm not to sort.
382 # The -v option tells Solaris nm to sort by value.
383 tests
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
384 if [ "x$tests" = x
]; then
385 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
388 # benchmarks are named BenchmarkFoo.
389 pattern
='Benchmark([^a-z].*)?'
390 benchmarks
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
392 # examples are named ExampleFoo
393 pattern
='Example([^a-z].*)?'
394 examples
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
400 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
401 echo 'import "./_gotest_"'
404 echo 'import "./_xtest_"'
406 echo 'import "testing"'
407 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
410 echo 'var tests = []testing.InternalTest {'
415 echo ' {"'$n'", '$j'},'
420 # The comment makes the multiline declaration
421 # gofmt-safe even when there are no benchmarks.
422 echo 'var benchmarks = []testing.InternalBenchmark{ //'
427 echo ' {"'$n'", '$j'},'
432 echo 'var examples = []testing.InternalExample{ //'
433 # This doesn't work because we don't pick up the output.
438 # echo ' {"'$n'", '$j', ""},'
446 var matchRe *__regexp__.Regexp
448 func matchString(pat, str string) (result bool, err error) {
449 if matchRe == nil || matchPat != pat {
451 matchRe, err = __regexp__.Compile(matchPat)
456 return matchRe.MatchString(str), nil
460 testing.Main(matchString, tests, benchmarks, examples)
466 if test "$trace" = "true"; then
467 echo ${GC} -g -c _testmain.go
469 ${GC} -g -c _testmain.go
471 if test "$trace" = "true"; then
472 echo ${GL} *.o
${GOLIBS}
476 if test "$trace" = "true"; then
477 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
479 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
481 (sleep `expr $timeout + 10`
482 echo > gotest-timeout
483 echo "timed out in gotest" 1>&2
488 if ! test -f gotest-timeout
; then
494 rm -rf ..
/..
/testsuite
/*.o
497 if test "$f" = "_obj" ||
test "$f" = "_test"; then
500 rm -rf ..
/..
/testsuite
/$f
502 cp $f ..
/..
/testsuite
/
504 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
510 if test "$testname" != ""; then
511 GOTESTNAME
="$testname"
514 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
515 # Useful when using make check-target-libgo
516 cat libgo.log
>> libgo-all.log
517 cat libgo.
sum >> libgo-all.
sum