-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
funs.sh
155 lines (135 loc) · 3.51 KB
/
funs.sh
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
#!/bin/bash
GO=8g
LD=8l
prepare() {
[[ "x$1" == "x" ]] && (echo "'prepare' requires a argument!" && exit -1)
[[ -d $1 ]] || mkdir -p $1
}
# usage: nmgrep pattern file...
# NOTE: copied from 'gotest'
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
}
print_testmain.go()
{
importpath=$1
# test functions are named TestFoo
# the grep -v eliminates methods and other special names
# that have multiple dots.
#pattern='Test([^a-z].*)?'
pattern='Test([A-Z0-9_].*)?'
tests=$(nmgrep $pattern _test/$importpath.a)
if [ "x$tests" = x ]; then
echo 'gotest: error: no tests matching '$pattern in _test/$importpath.a 1>&2
exit 2
fi
# benchmarks are named BenchmarkFoo.
#pattern='Benchmark([^a-z].*)?'
pattern='Benchmark([A-Z0-9_].*)?'
benchmarks=$(nmgrep $pattern _test/$importpath.a)
# 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
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 '}'
}
_build()
{
local type=$1
local name=$2
if [[ "x$type" == "x" ]] ; then
echo "build type unspecified!"
return 1
fi
if [[ "x$name" == "x" ]] ; then
echo "usage: build_$type name"
return 2
fi
if [[ "x$go_files" == "x" ]] ; then
echo "build_$type: variable go_files is empty, pack will not be built"
return 3
fi
(prepare _obj) \
&& $GO $go_incs -o _obj/$name.8 $go_files \
&& {
[[ "$type" == "pack" ]] && gopack grc _obj/$name.a _obj/$name.8
[[ "$type" == "exe" ]] && {
prepare _bin && $LD $go_libs -o _bin/$name _obj/$name.8
}
}
if [[ "x$go_tests" == "x" ]] ; then
#echo "build_$type: variable go_tests is empty, test pack will not be built."
return 100
fi
(prepare _test)\
&& $GO -o _test/$name.8 $go_files $go_tests \
&& gopack grc _test/$name.a _test/$name.8
}
build_pack()
{
_build pack $@
}
build_exe()
{
_build exe $@
}
build_testmain()
{
local name=$1
local testmain=_test/main.go
#print_testmain.go $name > $testmain || return 1
(prepare _test) \
&& ( print_testmain.go $name > $testmain ) \
&& $GO -I_test -o _test/main.8 $testmain \
&& $LD -L_test -o _bin/test _test/main.8 \
}