summaryrefslogtreecommitdiff
path: root/src/cmd/gotest
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
committerOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
commit3e45412327a2654a77944249962b3652e6142299 (patch)
treebc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/cmd/gotest
parentc533680039762cacbc37db8dc7eed074c3e497be (diff)
downloadgolang-3e45412327a2654a77944249962b3652e6142299.tar.gz
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/cmd/gotest')
-rw-r--r--src/cmd/gotest/Makefile16
-rwxr-xr-xsrc/cmd/gotest/gotest73
-rwxr-xr-xsrc/cmd/gotest/gotry167
3 files changed, 216 insertions, 40 deletions
diff --git a/src/cmd/gotest/Makefile b/src/cmd/gotest/Makefile
index b20b1daff..74054e974 100644
--- a/src/cmd/gotest/Makefile
+++ b/src/cmd/gotest/Makefile
@@ -1,14 +1,18 @@
-# Copyright 2009 The Go Authors. All rights reserved.
+# Copyright 2010 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.
-include ../../Make.conf
+include ../../Make.inc
-TARG=gotest
+TARG=install
clean:
@true
-install: $(TARG)
- ! test -f "$(GOBIN)"/$(TARG) || chmod u+w "$(GOBIN)"/$(TARG)
- cp $(TARG) "$(GOBIN)"/$(TARG)
+install: install-gotest install-gotry
+
+install-%: %
+ ! test -f "$(GOBIN)"/$* || chmod u+w "$(GOBIN)"/$*
+ sed 's`@@GOROOT@@`$(GOROOT_FINAL)`' $* >"$(GOBIN)"/$*
+ chmod +x "$(GOBIN)"/$*
+
diff --git a/src/cmd/gotest/gotest b/src/cmd/gotest/gotest
index fec2b4a4a..7572610d2 100755
--- a/src/cmd/gotest/gotest
+++ b/src/cmd/gotest/gotest
@@ -3,10 +3,10 @@
# 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
+# 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.
+# If files are named on the command line, use them instead of *_test.go.
# Makes egrep,grep work better in general if we put them
# in ordinary C mode instead of what the current language is.
@@ -14,16 +14,15 @@ unset LANG
export LC_ALL=C
export LC_CTYPE=C
-GOBIN="${GOBIN:-$HOME/bin}"
-
-_GC=$GC # Make.$GOARCH will overwrite this
+_GC=$GC # Make.inc will overwrite this
if [ ! -f [Mm]akefile ]; then
echo 'please create a Makefile for gotest; see http://golang.org/doc/code.html for details' 1>&2
exit 2
fi
-. "$GOROOT"/src/Make.$GOARCH
+export GOROOT=${GOROOT:-"@@GOROOT@@"}
+eval $(gomake -j1 --no-print-directory -f "$GOROOT"/src/Make.inc go-env)
if [ -z "$O" ]; then
echo 'missing $O - maybe no Make.$GOARCH?' 1>&2
exit 2
@@ -31,18 +30,12 @@ fi
E="$GORUN"
-# TODO(kaib): proper emulator strategy
-case x"$GOARCH" in
-xarm)
- export E=${GORUN:-qemu-arm -cpu cortex-a8}
-esac
-
# Allow overrides
-GC="$GOBIN/${_GC:-$GC} -I _test"
-GL="$GOBIN/${GL:-$LD} -L _test"
-AS="$GOBIN/$AS"
-CC="$GOBIN/$CC"
-LD="$GOBIN/$LD"
+GC="${_GC:-$GC} -I _test"
+GL="${GL:-$LD} -L _test"
+AS="$AS"
+CC="$CC"
+LD="$LD"
export GC GL O AS CC LD
gofiles=""
@@ -93,14 +86,13 @@ fi
set -e
-"$GOBIN"/gomake testpackage-clean
-"$GOBIN"/gomake testpackage "GOTESTFILES=$gofiles"
+gomake testpackage-clean
+gomake testpackage "GOTESTFILES=$gofiles"
if $havex; then
$GC -o $xofile $xgofiles
fi
# They all compile; now generate the code to call them.
-trap "rm -f _testmain.go _testmain.$O" 0 1 2 3 14 15
# Suppress output to stdout on Linux
MAKEFLAGS=
@@ -116,18 +108,18 @@ nmgrep() {
# Figure out pkg.
case "$i" in
*.a)
- pkg=$("$GOBIN"/gopack p $i __.PKGDEF | sed -n 's/^package //p' | sed 's/ .*//' | sed 1q)
+ pkg=$(gopack p $i __.PKGDEF | sed -n 's/^package //p' | sed 's/ .*//' | sed 1q)
;;
*)
pkg=$(sed -n 's/^ .* in package "\(.*\)".*/\1/p' $i | sed 1q)
;;
esac
- "$GOBIN"/6nm -s "$i" | egrep ' T .*\.'"$pat"'$' |
+ 6nm -s "$i" | egrep ' T .*\.'"$pat"'$' |
sed 's/.* //; /\..*\./d; s/""\./'"$pkg"'./g'
done
}
-importpath=$("$GOBIN"/gomake -s importpath)
+importpath=$(gomake -s importpath)
{
# test functions are named TestFoo
# the grep -v eliminates methods and other special names
@@ -155,30 +147,43 @@ importpath=$("$GOBIN"/gomake -s importpath)
echo 'import "./_xtest_"'
fi
echo 'import "testing"'
+ echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
# test array
echo
- echo 'var tests = []testing.Test {'
+ echo 'var tests = []testing.InternalTest{'
for i in $tests
do
- echo ' testing.Test{ "'$i'", '$i' },'
+ echo ' {"'$i'", '$i'},'
done
echo '}'
# benchmark array
- echo 'var benchmarks = []testing.Benchmark {'
- for i in $benchmarks
- do
- echo ' testing.Benchmark{ "'$i'", '$i' },'
- done
- echo '}'
-
+ if [ "$benchmarks" = "" ]
+ then
+ # keep the empty array gofmt-safe.
+ # (not an issue for the test array, which is never empty.)
+ echo 'var benchmarks = []testing.InternalBenchmark{}'
+ else
+ echo 'var benchmarks = []testing.InternalBenchmark{'
+ for i in $benchmarks
+ do
+ echo ' {"'$i'", '$i'},'
+ done
+ echo '}'
+ fi
# body
echo
echo 'func main() {'
- echo ' testing.Main(tests);'
- echo ' testing.RunBenchmarks(benchmarks)'
+ echo ' testing.Main(__regexp__.MatchString, tests)'
+ echo ' testing.RunBenchmarks(__regexp__.MatchString, benchmarks)'
echo '}'
}>_testmain.go
$GC _testmain.go
$GL _testmain.$O
+
+# Set dynamic linker library path, no matter what it's called,
+# to include the current directory while running $O.out,
+# so that cgo libraries can be tested without installation.
+LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH \
+DYLD_LIBRARY_PATH=.:$DYLD_LIBRARY_PATH \
$E ./$O.out "$@"
diff --git a/src/cmd/gotest/gotry b/src/cmd/gotest/gotry
new file mode 100755
index 000000000..52c5d2d58
--- /dev/null
+++ b/src/cmd/gotest/gotry
@@ -0,0 +1,167 @@
+#!/usr/bin/env bash
+# Copyright 2010 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 non-test *.go files in the named directory, write
+# out a file /tmp/$USER.try.go to evaluate the expressions on the
+# command line, perhaps to discover a function or method that
+# gives the desired results. See usage message.
+# Compile the program and run it.
+
+# Makes egrep,grep work better in general if we put them
+# in ordinary C mode instead of what the current language is.
+unset LANG
+export LC_ALL=C
+export LC_CTYPE=C
+
+export GOROOT=${GOROOT:-"@@GOROOT@@"}
+eval $(gomake -j1 --no-print-directory -f "$GOROOT"/src/Make.inc go-env)
+if [ -z "$O" ]; then
+ echo 'missing $O - maybe no Make.$GOARCH?' 1>&2
+ exit 2
+fi
+
+# Allow overrides
+GC="${_GC:-$GC} -I _test"
+GL="${GL:-$LD} -L _test"
+AS="$AS"
+CC="$CC"
+LD="$LD"
+export GC GL O AS CC LD
+
+# Macros for tab and quotes for easier readability.
+T=' '
+BQ='`'
+SQ="'"
+DQ='"'
+SD="$SQ$DQ"
+DS="$DQ$SQ"
+
+usage="usage: gotry [packagedirectory] expression ...
+Given one expression, gotry attempts to evaluate that expression.
+Given multiple expressions, gotry treats them as a list of arguments
+and result values and attempts to find a function in the package
+that, given the first few expressions as arguments, evaluates to
+the remaining expressions as results. If the first expression has
+methods, it will also search for applicable methods.
+
+If there are multiple expressions, a package directory must be
+specified. If there is a package argument, the expressions are
+evaluated in an environment that includes
+ import . ${DQ}packagedirectory${DQ}
+
+Examples:
+ gotry 3+4
+ # evaluates to 7
+ gotry strings ${SD}abc${DS} ${SD}c${DS} 7-5
+ # finds strings.Index etc.
+ gotry regexp ${SQ}MustCompile(${DQ}^[0-9]+${DQ})${SQ} ${SD}12345${DS} true
+ # finds Regexp.MatchString
+
+"
+
+function fail() {
+ echo 2>&1 "$@"
+ exit 2
+}
+
+case $# in
+ 0)
+ fail "$usage"
+ ;;
+ *)
+ case "$1" in
+ -*help|-*'?'|'?')
+ fail "$usage"
+ esac
+ if test -d "$GOROOT/src/pkg/$1"
+ then
+ pkg=$(basename $1)
+ dir=$GOROOT/src/pkg/$1
+ importdir=$1
+ shift
+ case "$pkg" in
+ os|syscall)
+ fail "gotry: cannot try packages os or syscall; they are too dangerous"
+ esac
+ fi
+ ;;
+esac
+
+spaces='[ ][ ]*'
+
+function getFunctions() {
+ if [ "$pkg" = "" ]
+ then
+ return
+ fi
+ for file in $dir/*.go
+ do
+ case $file in
+ *_test*)
+ continue
+ esac
+ grep "func$spaces[A-Z]" $file | # TODO: should be Unicode upper case
+ sed "s/func$spaces//;s/(.*//"
+ done | sort -u
+}
+
+# Generate list of public functions.
+functions=$(getFunctions)
+
+# Write file to compile
+rm -f /tmp/$USER.try.go
+(
+cat <<'!'
+package main
+
+import (
+ "os"
+ "try"
+!
+
+if [ "$pkg" != "" ]
+then
+ echo "$T" . '"'$importdir'"'
+fi
+
+cat <<!
+)
+func main() {
+ try.Main("$pkg", firstArg, functions, args)
+}
+var functions = map[string] interface{}{
+!
+
+for i in $functions
+do
+ echo "$T"'"'$i'": '$i','
+done
+echo "}"
+
+echo 'var args = []interface{}{'
+
+if [ $# = 1 ]
+then
+ echo "${T}toSlice($1)",
+else
+for i
+ do
+ echo "$T$i",
+ done
+fi
+echo "}"
+
+cat <<!
+var firstArg = $BQ$1$BQ
+var _ os.Error
+func toSlice(a ...interface{}) []interface{} { return a }
+!
+
+)>/tmp/$USER.try.go
+
+$GC -o /tmp/$USER.try.$O /tmp/$USER.try.go &&
+$GL -o /tmp/$USER.try /tmp/$USER.try.$O &&
+/tmp/$USER.try "_$@"
+rm -f /tmp/$USER.try /tmp/$USER.try.go /tmp/$USER.try.$O