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.
47 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
56 basedir=`echo $1 | sed -e 's/^--basedir=//'`
65 prefix=`echo $1 | sed -e 's/^--prefix=//'`
78 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
87 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
96 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
105 timeout=`echo $1 | sed -e 's/^--timeout=//'`
114 testname=`echo $1 | sed -e 's/^--testname=//'`
128 gofiles="$gofiles $1"
140 if test $keep = false; then
141 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
143 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
161 basedir="../$basedir"
165 # Link all the files/directories in srcdir into our working directory,
166 # so that the tests do not have to refer to srcdir to find test data.
169 # Some tests refer to a ../testdata directory.
170 if test -e $srcdir/../testdata; then
172 abssrcdir=`cd $srcdir && pwd`
173 ln -s $abssrcdir/../testdata ../testdata
176 # Copy the .go files because io/utils_test.go expects a regular file.
181 for f in `cd $srcdir; ls *.go`; do
187 for f in $pkgfiles; do
188 if test -f $basedir/$f; then
192 elif test -f ../$f; then
197 echo "file $f not found" 1>&2
201 for f in `cd $srcdir; ls *_test.go`; do
209 for f in $gofiles; do
216 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
222 for f in $pkgfiles; do
223 if test -f $basedir/$f; then
227 elif test -f ../$f; then
232 echo "file $f not found" 1>&2
241 # Some tests expect the _obj directory created by the gc Makefiles.
244 # Some tests expect the _test directory created by the gc Makefiles.
249 gofiles=`ls *_test.go 2>/dev/null`
254 echo 'no test files found' 1>&2
258 # Run any commands given in sources, like
259 # // gotest: $GC foo.go
260 # to build any test-only dependencies.
263 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
268 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
271 for f in $pkgfiles; do
272 pkgbasefiles="$pkgbasefiles `basename $f`"
279 echo 'no source files found' 1>&2
284 # Split $gofiles into external gofiles (those in *_test packages)
285 # and internal ones (those in the main package).
286 for f in $gofiles; do
287 package=`grep '^package[ ]' $f | sed 1q`
290 xgofiles="$xgofiles $f"
293 ngofiles="$ngofiles $f"
302 if [ "x$xgofiles" != "x" ]; then
309 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
312 if test -n "$prefix"; then
313 prefixarg="-fgo-prefix=$prefix"
316 if test "$trace" = "true"; then
317 echo $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
319 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
322 mkdir -p `dirname $package`
323 cp _gotest_.o `dirname $package`/lib`basename $package`.a
324 if test "$trace" = "true"; then
325 echo $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
327 $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
330 # They all compile; now generate the code to call them.
333 # The package main has been renamed to __main__ when imported.
335 echo $1 | sed 's/^main\./__main__./'
344 # test functions are named TestFoo
345 # the grep -v eliminates methods and other special names
346 # that have multiple dots.
347 pattern='Test([^a-z].*)?'
348 # The -p option tells GNU nm not to sort.
349 # The -v option tells Solaris nm to sort by value.
350 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
351 if [ "x$tests" = x ]; then
352 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
355 # benchmarks are named BenchmarkFoo.
356 pattern='Benchmark([^a-z].*)?'
357 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
359 # examples are named ExampleFoo
360 pattern='Example([^a-z].*)?'
361 examples=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
367 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
368 echo 'import "./_gotest_"'
371 echo 'import "./_xtest_"'
373 echo 'import "testing"'
374 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
377 echo 'var tests = []testing.InternalTest {'
381 echo ' {"'$i'", '$j'},'
386 # The comment makes the multiline declaration
387 # gofmt-safe even when there are no benchmarks.
388 echo 'var benchmarks = []testing.InternalBenchmark{ //'
392 echo ' {"'$i'", '$j'},'
397 echo 'var examples = []testing.InternalExample{ //'
398 # This doesn't work because we don't pick up the output.
402 # echo ' {"'$i'", '$j', ""},'
410 var matchRe *__regexp__.Regexp
412 func matchString(pat, str string) (result bool, err error) {
413 if matchRe == nil || matchPat != pat {
415 matchRe, err = __regexp__.Compile(matchPat)
420 return matchRe.MatchString(str), nil
424 testing.Main(matchString, tests, benchmarks, examples)
430 if test "$trace" = "true"; then
431 echo ${GC} -g -c _testmain.go
433 ${GC} -g -c _testmain.go
435 if test "$trace" = "true"; then
436 echo ${GL} *.o ${GOLIBS}
440 if test "$trace" = "true"; then
441 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
443 ./a.out -test.short -test.timeout=${timeout}s "$@" &
445 (sleep `expr $timeout + 10`
446 echo > gotest-timeout
447 echo "timed out in gotest" 1>&2
452 if ! test -f gotest-timeout; then
458 rm -rf ../testsuite/*.o
461 if test "$f" = "_obj" || test "$f" = "_test"; then
464 rm -rf ../testsuite/$f
468 ln -s ../$DIR/$f ../testsuite/
474 if test "$testname" != ""; then
475 GOTESTNAME="$testname"
478 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
479 # Useful when using make check-target-libgo
480 cat libgo.log >> libgo-all.log
481 cat libgo.sum >> libgo-all.sum