| #!/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. |
| |
| _GC=$GC # Make.$GOARCH will overwrite this |
| |
| . $GOROOT/src/Make.$GOARCH |
| if [ -z "$O" ]; then |
| echo 'missing $O - maybe no Make.$GOARCH?' 1>&2 |
| exit 1 |
| fi |
| |
| # Allow overrides |
| GC=${_GC:-$GC} |
| GL=${GL:-$LD} |
| GC="$GC -I _obj" |
| GL="$GL -L _obj" |
| 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/.'$O'/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.$O" 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 "$@" |