summaryrefslogtreecommitdiff
path: root/src/cmd/gotest/gotest
blob: 7572610d2e3cba22bb62aa0df404dc3d7d7ea507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env 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.

# 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

_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

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

E="$GORUN"

# 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

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=$(echo -n $(ls *_test.go 2>/dev/null))
esac

case "x$gofiles" in
x)
	echo 'no test files found (*_test.go)' 1>&2
	exit 2
esac

# Run any commands given in sources, like
#   // gotest: $GC foo.go
# to build any test-only dependencies.
sed -n 's/^\/\/ gotest: //p' $gofiles | sh -e || exit 1

# Split $gofiles into external gofiles (those in *_test packages)
# and internal ones (those in the main package).
xgofiles=$(echo $(grep '^package[ 	]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
gofiles=$(echo $(grep '^package[ 	]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))

# External $O file
xofile=""
havex=false
if [ "x$xgofiles" != "x" ]; then
	xofile="_xtest_.$O"
	havex=true
fi

set -e

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.

# Suppress output to stdout on Linux
MAKEFLAGS=
MAKELEVEL=

# usage: nmgrep pattern file...
nmgrep() {
	pat="$1"
	shift
	for i
	do
		# Package symbol "".Foo is pkg.Foo when imported in Go.
		# Figure out pkg.
		case "$i" in
		*.a)
			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
		6nm -s "$i" | egrep ' T .*\.'"$pat"'$' |
		sed 's/.* //; /\..*\./d; s/""\./'"$pkg"'./g'
	done
}

importpath=$(gomake -s importpath)
{
	# test functions are named TestFoo
	# the grep -v eliminates methods and other special names
	# that have multiple dots.
	pattern='Test([^a-z].*)?'
	tests=$(nmgrep $pattern _test/$importpath.a $xofile)
	if [ "x$tests" = x ]; then
		echo 'gotest: error: no tests matching '$pattern in _test/$importpath.a $xofile 1>&2
		exit 2
	fi
	# benchmarks are named BenchmarkFoo.
	pattern='Benchmark([^a-z].*)?'
	benchmarks=$(nmgrep $pattern _test/$importpath.a $xofile)

	# package spec
	echo 'package main'
	echo
	# imports
	if echo "$tests" | egrep -v '_test\.' >/dev/null; then
		if [ "$importpath" != "testing" ]; then
			echo 'import "'$importpath'"'
		fi
	fi
	if $havex; then
		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.InternalTest{'
	for i in $tests
	do
		echo '	{"'$i'", '$i'},'
	done
	echo '}'
	# benchmark array
	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(__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 "$@"