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.
43 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
52 basedir=`echo $1 | sed -e 's/^--basedir=//'`
61 prefix=`echo $1 | sed -e 's/^--prefix=//'`
74 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
83 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
105 if test $keep = false; then
106 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
108 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
126 basedir="../$basedir"
130 # Link all the files/directories in srcdir into our working directory,
131 # so that the tests do not have to refer to srcdir to find test data.
134 # Some tests refer to a ../testdata directory.
135 if test -e $srcdir/../testdata; then
137 abssrcdir=`cd $srcdir && pwd`
138 ln -s $abssrcdir/../testdata ../testdata
141 # Copy the .go files because io/utils_test.go expects a regular file.
146 for f in `cd $srcdir; ls *.go`; do
152 for f in $pkgfiles; do
153 if test -f $basedir/$f; then
157 elif test -f ../$f; then
162 echo "file $f not found" 1>&2
166 for f in `cd $srcdir; ls *_test.go`; do
174 for f in $gofiles; do
181 for f in `cd $srcdir; ls *.go | grep -v *_test.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
206 # Some tests expect the _obj directory created by the gc Makefiles.
209 # Some tests expect the _test directory created by the gc Makefiles.
214 gofiles=$(echo -n $(ls *_test.go 2>/dev/null))
219 echo 'no test files found' 1>&2
223 # Run any commands given in sources, like
224 # // gotest: $GC foo.go
225 # to build any test-only dependencies.
228 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
233 pkgbasefiles=$(echo -n $(ls *.go | grep -v _test.go 2>/dev/null))
236 for f in $pkgfiles; do
237 pkgbasefiles="$pkgbasefiles `basename $f`"
244 echo 'no source files found' 1>&2
249 # Split $gofiles into external gofiles (those in *_test packages)
250 # and internal ones (those in the main package).
251 xgofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
252 gofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
257 if [ "x$xgofiles" != "x" ]; then
264 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
267 if test -n "$prefix"; then
268 prefixarg="-fgo-prefix=$prefix"
271 $GC -g $prefixarg -c -I . -o _gotest_.o $gofiles $pkgbasefiles
273 mkdir -p `dirname $package`
274 cp _gotest_.o `dirname $package`/lib`basename $package`.a
275 $GC -g -c -I . -o $xofile $xgofiles
278 # They all compile; now generate the code to call them.
281 # The package main has been renamed to __main__ when imported.
283 echo $1 | sed 's/^main\./__main__./'
287 # test functions are named TestFoo
288 # the grep -v eliminates methods and other special names
289 # that have multiple dots.
290 pattern='Test([^a-z].*)?'
291 # The -p option tells GNU nm not to sort.
292 # The -v option tells Solaris nm to sort by value.
293 tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
294 if [ "x$tests" = x ]; then
295 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
298 # benchmarks are named BenchmarkFoo.
299 pattern='Benchmark([^a-z].*)?'
300 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
306 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
307 echo 'import "./_gotest_"'
310 echo 'import "./_xtest_"'
312 echo 'import "testing"'
313 echo 'import __os__ "os"' # rename in case tested package is called os
314 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
317 echo 'var tests = []testing.InternalTest {'
321 echo ' {"'$i'", '$j'},'
325 # The comment makes the multiline declaration
326 # gofmt-safe even when there are no benchmarks.
327 echo 'var benchmarks = []testing.InternalBenchmark{ //'
331 echo ' {"'$i'", '$j'},'
338 var matchRe *__regexp__.Regexp
340 func matchString(pat, str string) (result bool, err __os__.Error) {
341 if matchRe == nil || matchPat != pat {
343 matchRe, err = __regexp__.Compile(matchPat)
348 return matchRe.MatchString(str), nil
352 testing.Main(matchString, tests, benchmarks)
358 ${GC} -g -c _testmain.go
363 rm -rf ../testsuite/*.o
366 if test "$f" = "_obj" || test "$f" = "_test"; then
369 rm -rf ../testsuite/$f
373 ln -s ../$DIR/$f ../testsuite/
379 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
380 # Useful when using make check-target-libgo
381 cat libgo.log >> libgo-all.log
382 cat libgo.sum >> libgo-all.sum