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.
44 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
53 basedir=`echo $1 | sed -e 's/^--basedir=//'`
62 prefix=`echo $1 | sed -e 's/^--prefix=//'`
75 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
84 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
93 timeout=`echo $1 | sed -e 's/^--timeout=//'`
103 gofiles="$gofiles $1"
115 if test $keep = false; then
116 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
118 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
136 basedir="../$basedir"
140 # Link all the files/directories in srcdir into our working directory,
141 # so that the tests do not have to refer to srcdir to find test data.
144 # Some tests refer to a ../testdata directory.
145 if test -e $srcdir/../testdata; then
147 abssrcdir=`cd $srcdir && pwd`
148 ln -s $abssrcdir/../testdata ../testdata
151 # Copy the .go files because io/utils_test.go expects a regular file.
156 for f in `cd $srcdir; ls *.go`; do
162 for f in $pkgfiles; do
163 if test -f $basedir/$f; then
167 elif test -f ../$f; then
172 echo "file $f not found" 1>&2
176 for f in `cd $srcdir; ls *_test.go`; do
184 for f in $gofiles; do
191 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
197 for f in $pkgfiles; do
198 if test -f $basedir/$f; then
202 elif test -f ../$f; then
207 echo "file $f not found" 1>&2
216 # Some tests expect the _obj directory created by the gc Makefiles.
219 # Some tests expect the _test directory created by the gc Makefiles.
224 gofiles=$(echo -n $(ls *_test.go 2>/dev/null))
229 echo 'no test files found' 1>&2
233 # Run any commands given in sources, like
234 # // gotest: $GC foo.go
235 # to build any test-only dependencies.
238 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
243 pkgbasefiles=$(echo -n $(ls *.go | grep -v _test.go 2>/dev/null))
246 for f in $pkgfiles; do
247 pkgbasefiles="$pkgbasefiles `basename $f`"
254 echo 'no source files found' 1>&2
259 # Split $gofiles into external gofiles (those in *_test packages)
260 # and internal ones (those in the main package).
261 xgofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
262 gofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
267 if [ "x$xgofiles" != "x" ]; then
274 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
277 if test -n "$prefix"; then
278 prefixarg="-fgo-prefix=$prefix"
281 $GC -g $prefixarg -c -I . -o _gotest_.o $gofiles $pkgbasefiles
283 mkdir -p `dirname $package`
284 cp _gotest_.o `dirname $package`/lib`basename $package`.a
285 $GC -g -c -I . -o $xofile $xgofiles
288 # They all compile; now generate the code to call them.
291 # The package main has been renamed to __main__ when imported.
293 echo $1 | sed 's/^main\./__main__./'
297 # test functions are named TestFoo
298 # the grep -v eliminates methods and other special names
299 # that have multiple dots.
300 pattern='Test([^a-z].*)?'
301 # The -p option tells GNU nm not to sort.
302 # The -v option tells Solaris nm to sort by value.
303 tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
304 if [ "x$tests" = x ]; then
305 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
308 # benchmarks are named BenchmarkFoo.
309 pattern='Benchmark([^a-z].*)?'
310 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
316 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
317 echo 'import "./_gotest_"'
320 echo 'import "./_xtest_"'
322 echo 'import "testing"'
323 echo 'import __os__ "os"' # rename in case tested package is called os
324 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
327 echo 'var tests = []testing.InternalTest {'
331 echo ' {"'$i'", '$j'},'
335 # The comment makes the multiline declaration
336 # gofmt-safe even when there are no benchmarks.
337 echo 'var benchmarks = []testing.InternalBenchmark{ //'
341 echo ' {"'$i'", '$j'},'
348 var matchRe *__regexp__.Regexp
350 func matchString(pat, str string) (result bool, err __os__.Error) {
351 if matchRe == nil || matchPat != pat {
353 matchRe, err = __regexp__.Compile(matchPat)
358 return matchRe.MatchString(str), nil
362 testing.Main(matchString, tests, benchmarks)
368 ${GC} -g -c _testmain.go
370 ./a.out -test.short -test.timeout=$timeout "$@"
373 rm -rf ../testsuite/*.o
376 if test "$f" = "_obj" || test "$f" = "_test"; then
379 rm -rf ../testsuite/$f
383 ln -s ../$DIR/$f ../testsuite/
389 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
390 # Useful when using make check-target-libgo
391 cat libgo.log >> libgo-all.log
392 cat libgo.sum >> libgo-all.sum