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.
280 # test functions are named TestFoo
281 # the grep -v eliminates methods and other special names
282 # that have multiple dots.
283 pattern='Test([^a-z].*)?'
284 # The -p option tells GNU nm not to sort.
285 # The -v option tells Solaris nm to sort by value.
286 tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
287 if [ "x$tests" = x ]; then
288 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
296 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
297 echo 'import "./_gotest_"'
300 echo 'import "./_xtest_"'
302 if [ $package != "testing" ]; then
303 echo 'import "testing"'
304 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
308 echo 'var tests = []testing.InternalTest {'
311 echo ' { "'$i'", '$i' },'
317 echo ' testing.Main(__regexp__.MatchString, tests)'
323 ${GC} -g -c _testmain.go
328 rm -rf ../testsuite/*.o
331 if test "$f" = "_obj" || test "$f" = "_test"; then
334 rm -rf ../testsuite/$f
338 ln -s ../$DIR/$f ../testsuite/
344 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
345 # Useful when using make check-target-libgo
346 cat libgo.log >> libgo-all.log
347 cat libgo.sum >> libgo-all.sum