In libobjc/: 2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libgo / testsuite / gotest
blob13211d0e34c8640adaecbe810cd9bc7c73204e98
1 #!/bin/sh
2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # Using all the *_test.go files in the current directory, write out a file
7 # _testmain.go that runs all its tests. Compile everything and run the
8 # tests.
9 # If files are named on the command line, use them instead of *_test.go.
11 # Makes egrep,grep work better in general if we put them
12 # in ordinary C mode instead of what the current language is.
13 unset LANG
14 export LC_ALL=C
15 export LC_CTYPE=C
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=
20 export GC GL GOLIBS
22 # srcdir is where the source files are found. basedir is where the
23 # source file paths are relative to.
24 # gofiles are the test files. pkgfiles are the source files.
25 srcdir=.
26 basedir=.
27 gofiles=""
28 pkgfiles=""
29 loop=true
30 keep=false
31 prefix=
32 dejagnu=no
33 while $loop; do
34 case "x$1" in
35 x--srcdir)
36 srcdir=$2
37 shift
38 shift
40 x--srcdir=*)
41 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
42 shift
44 x--basedir)
45 basedir=$2
46 shift
47 shift
49 x--basedir=*)
50 basedir=`echo $1 | sed -e 's/^--basedir=//'`
51 shift
53 x--prefix)
54 prefix=$2
55 shift
56 shift
58 x--prefix=*)
59 prefix=`echo $1 | sed -e 's/^--prefix=//'`
60 shift
62 x--keep)
63 keep=true
64 shift
66 x--pkgfiles)
67 pkgfiles=$2
68 shift
69 shift
71 x--pkgfiles=*)
72 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
73 shift
75 x--dejagnu)
76 dejagnu=$2
77 shift
78 shift
80 x--dejagnu=*)
81 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
82 shift
84 x-*)
85 loop=false
88 loop=false
91 gofiles="$gofiles $1"
92 shift
94 esac
95 done
97 DIR=gotest$$
98 rm -rf $DIR
99 mkdir $DIR
101 cd $DIR
103 if test $keep = false; then
104 trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
105 else
106 trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
109 case "$srcdir" in
113 srcdir="../$srcdir"
115 esac
117 SRCDIR=$srcdir
118 export SRCDIR
120 case "$basedir" in
124 basedir="../$basedir"
126 esac
128 # Link all the files/directories in srcdir into our working directory,
129 # so that the tests do not have to refer to srcdir to find test data.
130 ln -s $srcdir/* .
132 # Copy the .go files because io/utils_test.go expects a regular file.
133 case "x$gofiles" in
135 case "x$pkgfiles" in
137 for f in `cd $srcdir; ls *.go`; do
138 rm -f $f;
139 cp $srcdir/$f .
140 done
143 for f in $pkgfiles; do
144 if test -f $basedir/$f; then
145 b=`basename $f`
146 rm -f $b
147 cp $basedir/$f $b
148 elif test -f ../$f; then
149 b=`basename $f`
150 rm -f $b
151 cp ../$f $b
152 else
153 echo "file $f not found" 1>&2
154 exit 1
156 done
157 for f in `cd $srcdir; ls *_test.go`; do
158 rm -f $f
159 cp $srcdir/$f .
160 done
162 esac
165 for f in $gofiles; do
166 b=`basename $f`
167 rm -f $b
168 cp $basedir/$f $b
169 done
170 case "x$pkgfiles" in
172 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
173 rm -f $f
174 cp $srcdir/$f .
175 done
178 for f in $pkgfiles; do
179 if test -f $basedir/$f; then
180 b=`basename $f`
181 rm -f $b
182 cp $basedir/$f $b
183 elif test -f ../$f; then
184 b=`basename $f`
185 rm -f $b
186 cp ../$f $b
187 else
188 echo "file $f not found" 1>&2
189 exit 1
191 done
193 esac
195 esac
197 # Some tests expect the _obj directory created by the gc Makefiles.
198 mkdir _obj
200 # Some tests expect the _test directory created by the gc Makefiles.
201 mkdir _test
203 case "x$gofiles" in
205 gofiles=$(echo -n $(ls *_test.go 2>/dev/null))
206 esac
208 case "x$gofiles" in
210 echo 'no test files found' 1>&2
211 exit 1
212 esac
214 # Run any commands given in sources, like
215 # // gotest: $GC foo.go
216 # to build any test-only dependencies.
217 holdGC="$GC"
218 GC="$GC -g -c -I ."
219 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
220 GC="$holdGC"
222 case "x$pkgfiles" in
224 pkgbasefiles=$(echo -n $(ls *.go | grep -v _test.go 2>/dev/null))
227 for f in $pkgfiles; do
228 pkgbasefiles="$pkgbasefiles `basename $f`"
229 done
231 esac
233 case "x$pkgfiles" in
235 echo 'no source files found' 1>&2
236 exit 1
238 esac
240 # Split $gofiles into external gofiles (those in *_test packages)
241 # and internal ones (those in the main package).
242 xgofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
243 gofiles=$(echo $(grep '^package[ ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
245 # External $O file
246 xofile=""
247 havex=false
248 if [ "x$xgofiles" != "x" ]; then
249 xofile="_xtest_.o"
250 havex=true
253 set -e
255 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
257 prefixarg=
258 if test -n "$prefix"; then
259 prefixarg="-fgo-prefix=$prefix"
262 $GC -g $prefixarg -c -I . -o _gotest_.o $gofiles $pkgbasefiles
263 if $havex; then
264 mkdir -p `dirname $package`
265 cp _gotest_.o `dirname $package`/lib`basename $package`.a
266 $GC -g -c -I . -o $xofile $xgofiles
269 # They all compile; now generate the code to call them.
271 # test functions are named TestFoo
272 # the grep -v eliminates methods and other special names
273 # that have multiple dots.
274 pattern='Test([^a-z].*)?'
275 tests=$(nm -s _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
276 if [ "x$tests" = x ]; then
277 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
278 exit 2
281 # package spec
282 echo 'package main'
283 echo
284 # imports
285 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
286 echo 'import "./_gotest_"'
288 if $havex; then
289 echo 'import "./_xtest_"'
291 if [ $package != "testing" ]; then
292 echo 'import "testing"'
293 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
295 # test array
296 echo
297 echo 'var tests = []testing.InternalTest {'
298 for i in $tests
300 echo ' { "'$i'", '$i' },'
301 done
302 echo '}'
303 # body
304 echo
305 echo 'func main() {'
306 echo ' testing.Main(__regexp__.MatchString, tests)'
307 echo '}'
308 }>_testmain.go
310 case "x$dejagnu" in
311 xno)
312 ${GC} -g -c _testmain.go
313 ${GL} *.o ${GOLIBS}
314 ./a.out "$@"
316 xyes)
317 # This is the only file which is optionally made.
318 # All others are overwritten on copying/building,
319 # but this may remain and cause conflicts if not
320 # deleted.
321 rm -rf ../testsuite/_xtest_.o
322 cp *.o _testmain.go ../testsuite
323 cd ../testsuite
324 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
325 # Useful when using make check-target-libgo
326 cat libgo.log >> libgo-all.log
327 cat libgo.sum >> libgo-all.sum
329 esac