blob: 0c025e3b3468af023dee07a502268c0285af52dc [file]
#!/bin/bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Using all the test*.go files in the current directory, write out a file
# _testmain.go that runs all its tests. Compile everything and run the
# tests.
# If files are named on the command line, use them instead of test*.go.
O=6
GC=${GC:-${O}g}
GL=${GL:-${O}l}
export GC GL
gofiles=""
loop=true
while $loop; do
case "x$1" in
x-*)
loop=false
;;
x)
loop=false
;;
*)
gofiles="$gofiles $1"
shift
;;
esac
done
case "x$gofiles" in
x)
gofiles=$(ls *_test.go 2>/dev/null)
esac
case "x$gofiles" in
x)
echo 'no test files found' 1>&2
exit 1
esac
ofiles=$(echo $gofiles | sed 's/\.go/.6/g')
files=$(echo $gofiles | sed 's/\.go//g')
# Run any commands given in sources, like
# // gotest: $GC foo.go
# to build any test-only dependencies.
sed -n 's/^\/\/ gotest: //p' $gofiles | sh
set -e
for i in $gofiles
do
$GC $i
done
# They all compile; now generate the code to call them.
trap "rm -f _testmain.go _testmain.6" 0 1 2 3 14 15
{
# package spec
echo 'package main'
echo
# imports
for i in $files
do
echo 'import "./'$i'"'
done
echo 'import "testing"'
# test array
echo
echo 'var tests = []testing.Test {'
for ofile in $ofiles
do
# test functions are named TestFoo
# the grep -v eliminates methods and other special names
# that have multiple dots.
pattern='Test([^a-z].*)?'
tests=$(6nm -s $ofile | egrep ' T .*·'$pattern'$' | grep -v '·.*[.·]' | sed 's/.* //; s/·/./')
if [ "x$tests" = x ]; then
echo 'gotest: warning: no tests matching '$pattern' in '$ofile 1>&2
else
for i in $tests
do
echo ' testing.Test{ "'$i'", '$i' },'
done
fi
done
echo '}'
# body
echo
echo 'func main() {'
echo ' testing.Main(tests)'
echo '}'
}>_testmain.go
$GC _testmain.go
$GL _testmain.$O
./$O.out "$@"