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.
45 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
54 basedir=`echo $1 | sed -e 's/^--basedir=//'`
63 prefix=`echo $1 | sed -e 's/^--prefix=//'`
76 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
85 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
94 timeout=`echo $1 | sed -e 's/^--timeout=//'`
103 testname=`echo $1 | sed -e 's/^--testname=//'`
113 gofiles="$gofiles $1"
125 if test $keep = false; then
126 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
128 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
146 basedir="../$basedir"
150 # Link all the files/directories in srcdir into our working directory,
151 # so that the tests do not have to refer to srcdir to find test data.
154 # Some tests refer to a ../testdata directory.
155 if test -e $srcdir/../testdata; then
157 abssrcdir=`cd $srcdir && pwd`
158 ln -s $abssrcdir/../testdata ../testdata
161 # Copy the .go files because io/utils_test.go expects a regular file.
166 for f in `cd $srcdir; ls *.go`; do
172 for f in $pkgfiles; do
173 if test -f $basedir/$f; then
177 elif test -f ../$f; then
182 echo "file $f not found" 1>&2
186 for f in `cd $srcdir; ls *_test.go`; do
194 for f in $gofiles; do
201 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
207 for f in $pkgfiles; do
208 if test -f $basedir/$f; then
212 elif test -f ../$f; then
217 echo "file $f not found" 1>&2
226 # Some tests expect the _obj directory created by the gc Makefiles.
229 # Some tests expect the _test directory created by the gc Makefiles.
234 gofiles=`ls *_test.go 2>/dev/null`
239 echo 'no test files found' 1>&2
243 # Run any commands given in sources, like
244 # // gotest: $GC foo.go
245 # to build any test-only dependencies.
248 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
253 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
256 for f in $pkgfiles; do
257 pkgbasefiles="$pkgbasefiles `basename $f`"
264 echo 'no source files found' 1>&2
269 # Split $gofiles into external gofiles (those in *_test packages)
270 # and internal ones (those in the main package).
271 xgofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
272 gofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
277 if [ "x$xgofiles" != "x" ]; then
284 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
287 if test -n "$prefix"; then
288 prefixarg="-fgo-prefix=$prefix"
291 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
293 mkdir -p `dirname $package`
294 cp _gotest_.o `dirname $package`/lib`basename $package`.a
295 $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
298 # They all compile; now generate the code to call them.
301 # The package main has been renamed to __main__ when imported.
303 echo $1 | sed 's/^main\./__main__./'
307 # test functions are named TestFoo
308 # the grep -v eliminates methods and other special names
309 # that have multiple dots.
310 pattern='Test([^a-z].*)?'
311 # The -p option tells GNU nm not to sort.
312 # The -v option tells Solaris nm to sort by value.
313 tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
314 if [ "x$tests" = x ]; then
315 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
318 # benchmarks are named BenchmarkFoo.
319 pattern='Benchmark([^a-z].*)?'
320 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
326 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
327 echo 'import "./_gotest_"'
330 echo 'import "./_xtest_"'
332 echo 'import "testing"'
333 echo 'import __os__ "os"' # rename in case tested package is called os
334 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
337 echo 'var tests = []testing.InternalTest {'
341 echo ' {"'$i'", '$j'},'
345 # The comment makes the multiline declaration
346 # gofmt-safe even when there are no benchmarks.
347 echo 'var benchmarks = []testing.InternalBenchmark{ //'
351 echo ' {"'$i'", '$j'},'
358 var matchRe *__regexp__.Regexp
360 func matchString(pat, str string) (result bool, err __os__.Error) {
361 if matchRe == nil || matchPat != pat {
363 matchRe, err = __regexp__.Compile(matchPat)
368 return matchRe.MatchString(str), nil
372 testing.Main(matchString, tests, benchmarks)
378 ${GC} -g -c _testmain.go
381 ./a.out -test.short -test.timeout=$timeout "$@" &
383 (sleep `expr $timeout + 10`
384 echo > gotest-timeout
385 echo "timed out in gotest" 1>&2
390 if ! test -f gotest-timeout; then
396 rm -rf ../testsuite/*.o
399 if test "$f" = "_obj" || test "$f" = "_test"; then
402 rm -rf ../testsuite/$f
406 ln -s ../$DIR/$f ../testsuite/
412 if test "$testname" != ""; then
413 GOTESTNAME="$testname"
416 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
417 # Useful when using make check-target-libgo
418 cat libgo.log >> libgo-all.log
419 cat libgo.sum >> libgo-all.sum