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.
46 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
55 basedir=`echo $1 | sed -e 's/^--basedir=//'`
64 prefix=`echo $1 | sed -e 's/^--prefix=//'`
77 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
86 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
95 timeout=`echo $1 | sed -e 's/^--timeout=//'`
104 testname=`echo $1 | sed -e 's/^--testname=//'`
118 gofiles="$gofiles $1"
130 if test $keep = false; then
131 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
133 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
151 basedir="../$basedir"
155 # Link all the files/directories in srcdir into our working directory,
156 # so that the tests do not have to refer to srcdir to find test data.
159 # Some tests refer to a ../testdata directory.
160 if test -e $srcdir/../testdata; then
162 abssrcdir=`cd $srcdir && pwd`
163 ln -s $abssrcdir/../testdata ../testdata
166 # Copy the .go files because io/utils_test.go expects a regular file.
171 for f in `cd $srcdir; ls *.go`; do
177 for f in $pkgfiles; do
178 if test -f $basedir/$f; then
182 elif test -f ../$f; then
187 echo "file $f not found" 1>&2
191 for f in `cd $srcdir; ls *_test.go`; do
199 for f in $gofiles; do
206 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
212 for f in $pkgfiles; do
213 if test -f $basedir/$f; then
217 elif test -f ../$f; then
222 echo "file $f not found" 1>&2
231 # Some tests expect the _obj directory created by the gc Makefiles.
234 # Some tests expect the _test directory created by the gc Makefiles.
239 gofiles=`ls *_test.go 2>/dev/null`
244 echo 'no test files found' 1>&2
248 # Run any commands given in sources, like
249 # // gotest: $GC foo.go
250 # to build any test-only dependencies.
253 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
258 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
261 for f in $pkgfiles; do
262 pkgbasefiles="$pkgbasefiles `basename $f`"
269 echo 'no source files found' 1>&2
274 # Split $gofiles into external gofiles (those in *_test packages)
275 # and internal ones (those in the main package).
276 xgofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
277 gofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
282 if [ "x$xgofiles" != "x" ]; then
289 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
292 if test -n "$prefix"; then
293 prefixarg="-fgo-prefix=$prefix"
296 if test "$trace" = "true"; then
297 echo $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
299 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
302 mkdir -p `dirname $package`
303 cp _gotest_.o `dirname $package`/lib`basename $package`.a
304 if test "$trace" = "true"; then
305 echo $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
307 $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
310 # They all compile; now generate the code to call them.
313 # The package main has been renamed to __main__ when imported.
315 echo $1 | sed 's/^main\./__main__./'
319 # test functions are named TestFoo
320 # the grep -v eliminates methods and other special names
321 # that have multiple dots.
322 pattern='Test([^a-z].*)?'
323 # The -p option tells GNU nm not to sort.
324 # The -v option tells Solaris nm to sort by value.
325 tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
326 if [ "x$tests" = x ]; then
327 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
330 # benchmarks are named BenchmarkFoo.
331 pattern='Benchmark([^a-z].*)?'
332 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
338 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
339 echo 'import "./_gotest_"'
342 echo 'import "./_xtest_"'
344 echo 'import "testing"'
345 echo 'import __os__ "os"' # rename in case tested package is called os
346 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
349 echo 'var tests = []testing.InternalTest {'
353 echo ' {"'$i'", '$j'},'
357 # The comment makes the multiline declaration
358 # gofmt-safe even when there are no benchmarks.
359 echo 'var benchmarks = []testing.InternalBenchmark{ //'
363 echo ' {"'$i'", '$j'},'
370 var matchRe *__regexp__.Regexp
372 func matchString(pat, str string) (result bool, err __os__.Error) {
373 if matchRe == nil || matchPat != pat {
375 matchRe, err = __regexp__.Compile(matchPat)
380 return matchRe.MatchString(str), nil
384 testing.Main(matchString, tests, benchmarks)
390 if test "$trace" = "true"; then
391 echo ${GC} -g -c _testmain.go
393 ${GC} -g -c _testmain.go
395 if test "$trace" = "true"; then
396 echo ${GL} *.o ${GOLIBS}
400 if test "$trace" = "true"; then
401 echo ./a.out -test.short -test.timeout=$timeout "$@"
403 ./a.out -test.short -test.timeout=$timeout "$@" &
405 (sleep `expr $timeout + 10`
406 echo > gotest-timeout
407 echo "timed out in gotest" 1>&2
412 if ! test -f gotest-timeout; then
418 rm -rf ../testsuite/*.o
421 if test "$f" = "_obj" || test "$f" = "_test"; then
424 rm -rf ../testsuite/$f
428 ln -s ../$DIR/$f ../testsuite/
434 if test "$testname" != ""; then
435 GOTESTNAME="$testname"
438 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
439 # Useful when using make check-target-libgo
440 cat libgo.log >> libgo-all.log
441 cat libgo.sum >> libgo-all.sum