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 echo 'no test files found' 1>&2
270 # Run any commands given in sources, like
271 # // gotest: $GC foo.go
272 # to build any test-only dependencies.
275 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
280 pkgbasefiles
=`ls *.go | grep -v _test.go 2>/dev/null`
283 for f
in $pkgfiles; do
284 pkgbasefiles
="$pkgbasefiles `basename $f`"
291 echo 'no source files found' 1>&2
296 # Split $gofiles into external gofiles (those in *_test packages)
297 # and internal ones (those in the main package).
298 for f
in $gofiles; do
299 package
=`grep '^package[ ]' $f | sed 1q`
302 xgofiles
="$xgofiles $f"
305 ngofiles
="$ngofiles $f"
314 if [ "x$xgofiles" != "x" ]; then
321 package
=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
326 if test -n "$pkgpath"; then
327 pkgpatharg
="-fgo-pkgpath=$pkgpath"
328 xpkgpatharg
="-fgo-pkgpath=${pkgpath}_test"
329 elif test -n "$prefix"; then
330 prefixarg
="-fgo-prefix=$prefix"
333 if test "$trace" = "true"; then
334 echo $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
336 $GC -g $pkgpatharg $prefixarg -c -I .
-fno-toplevel-reorder -o _gotest_.o
$gofiles $pkgbasefiles
339 mkdir
-p `dirname $package`
340 cp _gotest_.o
`dirname $package`/lib
`basename $package`.a
341 if test "$trace" = "true"; then
342 echo $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
344 $GC -g $xpkgpatharg -c -I .
-fno-toplevel-reorder -o $xofile $xgofiles
347 # They all compile; now generate the code to call them.
350 # The package main has been renamed to __main__ when imported.
352 echo $1 |
sed 's/^main\./__main__./'
361 symtogo
='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
363 # test functions are named TestFoo
364 # the grep -v eliminates methods and other special names
365 # that have multiple dots.
366 pattern
='Test([^a-z].*)?'
367 # The -p option tells GNU nm not to sort.
368 # The -v option tells Solaris nm to sort by value.
369 tests
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
370 if [ "x$tests" = x
]; then
371 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o
$xofile 1>&2
374 # benchmarks are named BenchmarkFoo.
375 pattern
='Benchmark([^a-z].*)?'
376 benchmarks
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $test .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
378 # examples are named ExampleFoo
379 pattern
='Example([^a-z].*)?'
380 examples
=$
($NM -p -v _gotest_.o
$xofile |
egrep " $text .*\."$pattern'$' |
grep -v '\..*\..*\.' | fgrep
-v '$' | fgrep
-v ' __go_' |
sed 's/.* //' |
$symtogo)
386 if echo "$tests" |
egrep -v '_test\.' >/dev
/null
; then
387 echo 'import "./_gotest_"'
390 echo 'import "./_xtest_"'
392 echo 'import "testing"'
393 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
396 echo 'var tests = []testing.InternalTest {'
400 echo ' {"'$i'", '$j'},'
405 # The comment makes the multiline declaration
406 # gofmt-safe even when there are no benchmarks.
407 echo 'var benchmarks = []testing.InternalBenchmark{ //'
411 echo ' {"'$i'", '$j'},'
416 echo 'var examples = []testing.InternalExample{ //'
417 # This doesn't work because we don't pick up the output.
421 # echo ' {"'$i'", '$j', ""},'
429 var matchRe *__regexp__.Regexp
431 func matchString(pat, str string) (result bool, err error) {
432 if matchRe == nil || matchPat != pat {
434 matchRe, err = __regexp__.Compile(matchPat)
439 return matchRe.MatchString(str), nil
443 testing.Main(matchString, tests, benchmarks, examples)
449 if test "$trace" = "true"; then
450 echo ${GC} -g -c _testmain.go
452 ${GC} -g -c _testmain.go
454 if test "$trace" = "true"; then
455 echo ${GL} *.o
${GOLIBS}
459 if test "$trace" = "true"; then
460 echo .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@"
462 .
/a.out
-test.short
-test.timeout
=${timeout}s
"$@" &
464 (sleep `expr $timeout + 10`
465 echo > gotest-timeout
466 echo "timed out in gotest" 1>&2
471 if ! test -f gotest-timeout
; then
477 rm -rf ..
/..
/testsuite
/*.o
480 if test "$f" = "_obj" ||
test "$f" = "_test"; then
483 rm -rf ..
/..
/testsuite
/$f
485 cp $f ..
/..
/testsuite
/
487 ln -s ..
/$DIR/test
/$f ..
/..
/testsuite
/
493 if test "$testname" != ""; then
494 GOTESTNAME
="$testname"
497 $MAKE check RUNTESTFLAGS
="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
498 # Useful when using make check-target-libgo
499 cat libgo.log
>> libgo-all.log
500 cat libgo.
sum >> libgo-all.
sum