2014-01-16 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / testsuite / gotest
blob155c7a8619ae1788d3aa2e75da3182048f9d0469
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=${GOLIBS:-}
20 export GC GL GOLIBS
22 NM=${NM:-nm}
24 # srcdir is where the source files are found. basedir is where the
25 # source file paths are relative to.
26 # gofiles are the test files. pkgfiles are the source files.
27 srcdir=.
28 basedir=.
29 gofiles=""
30 pkgfiles=""
31 loop=true
32 keep=false
33 pkgpath=
34 prefix=
35 dejagnu=no
36 GOARCH=""
37 timeout=240
38 testname=""
39 trace=false
40 while $loop; do
41 case "x$1" in
42 x--srcdir)
43 srcdir=$2
44 shift
45 shift
47 x--srcdir=*)
48 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
49 shift
51 x--basedir)
52 basedir=$2
53 shift
54 shift
56 x--basedir=*)
57 basedir=`echo $1 | sed -e 's/^--basedir=//'`
58 shift
60 x--pkgpath)
61 pkgpath=$2
62 shift
63 shift
65 x--pkgpath=*)
66 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
67 shift
69 x--prefix)
70 prefix=$2
71 shift
72 shift
74 x--prefix=*)
75 prefix=`echo $1 | sed -e 's/^--prefix=//'`
76 shift
78 x--keep)
79 keep=true
80 shift
82 x--pkgfiles)
83 pkgfiles=$2
84 shift
85 shift
87 x--pkgfiles=*)
88 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
89 shift
91 x--dejagnu)
92 dejagnu=$2
93 shift
94 shift
96 x--dejagnu=*)
97 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
98 shift
100 x--goarch)
101 GOARCH=$2
102 shift
103 shift
105 x--goarch=*)
106 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
107 shift
109 x--timeout)
110 timeout=$2
111 shift
112 shift
114 x--timeout=*)
115 timeout=`echo $1 | sed -e 's/^--timeout=//'`
116 shift
118 x--testname)
119 testname=$2
120 shift
121 shift
123 x--testname=*)
124 testname=`echo $1 | sed -e 's/^--testname=//'`
125 shift
127 x--trace)
128 trace=true
129 shift
131 x-*)
132 loop=false
135 loop=false
138 gofiles="$gofiles $1"
139 shift
141 esac
142 done
144 DIR=gotest$$
145 rm -rf $DIR
146 mkdir $DIR
148 cd $DIR
149 mkdir test
150 cd test
152 if test $keep = false; then
153 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
154 else
155 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
158 case "$srcdir" in
162 srcdir="../../$srcdir"
164 esac
166 SRCDIR=$srcdir
167 export SRCDIR
169 case "$basedir" in
173 basedir="../../$basedir"
175 esac
177 # Link all the files/directories in srcdir into our working directory,
178 # so that the tests do not have to refer to srcdir to find test data.
179 ln -s $srcdir/* .
181 # Some tests refer to a ../testdata directory.
182 if test -e $srcdir/../testdata; then
183 rm -f ../testdata
184 abssrcdir=`cd $srcdir && pwd`
185 ln -s $abssrcdir/../testdata ../testdata
188 # Copy the .go files because io/utils_test.go expects a regular file.
189 case "x$gofiles" in
191 case "x$pkgfiles" in
193 for f in `cd $srcdir; ls *.go`; do
194 rm -f $f;
195 cp $srcdir/$f .
196 done
199 for f in $pkgfiles; do
200 if test -f $basedir/$f; then
201 b=`basename $f`
202 rm -f $b
203 cp $basedir/$f $b
204 elif test -f ../../$f; then
205 b=`basename $f`
206 rm -f $b
207 cp ../../$f $b
208 else
209 echo "file $f not found" 1>&2
210 exit 1
212 done
213 for f in `cd $srcdir; ls *_test.go`; do
214 rm -f $f
215 cp $srcdir/$f .
216 done
218 esac
221 for f in $gofiles; do
222 b=`basename $f`
223 rm -f $b
224 cp $basedir/$f $b
225 done
226 case "x$pkgfiles" in
228 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
229 rm -f $f
230 cp $srcdir/$f .
231 done
234 for f in $pkgfiles; do
235 if test -f $basedir/$f; then
236 b=`basename $f`
237 rm -f $b
238 cp $basedir/$f $b
239 elif test -f ../../$f; then
240 b=`basename $f`
241 rm -f $b
242 cp ../../$f $b
243 else
244 echo "file $f not found" 1>&2
245 exit 1
247 done
249 esac
251 esac
253 # Some tests expect the _obj directory created by the gc Makefiles.
254 mkdir _obj
256 # Some tests expect the _test directory created by the gc Makefiles.
257 mkdir _test
259 case "x$gofiles" in
261 gofiles=`ls *_test.go 2>/dev/null`
264 xgofiles=$gofiles
265 gofiles=
266 for f in $xgofiles; do
267 gofiles="$gofiles `basename $f`"
268 done
269 esac
271 case "x$gofiles" in
273 echo 'no test files found' 1>&2
274 exit 1
276 esac
278 # Run any commands given in sources, like
279 # // gotest: $GC foo.go
280 # to build any test-only dependencies.
281 holdGC="$GC"
282 GC="$GC -g -c -I ."
283 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
284 GC="$holdGC"
286 case "x$pkgfiles" in
288 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
291 for f in $pkgfiles; do
292 pkgbasefiles="$pkgbasefiles `basename $f`"
293 done
295 esac
297 case "x$pkgfiles" in
299 echo 'no source files found' 1>&2
300 exit 1
302 esac
304 # Split $gofiles into external gofiles (those in *_test packages)
305 # and internal ones (those in the main package).
306 xgofiles=
307 for f in $gofiles; do
308 package=`grep '^package[ ]' $f | sed 1q`
309 case "$package" in
310 *_test)
311 xgofiles="$xgofiles $f"
314 ngofiles="$ngofiles $f"
316 esac
317 done
318 gofiles=$ngofiles
320 # External $O file
321 xofile=""
322 havex=false
323 if [ "x$xgofiles" != "x" ]; then
324 xofile="_xtest_.o"
325 havex=true
328 set -e
330 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
332 pkgpatharg=
333 xpkgpatharg=
334 prefixarg=
335 if test -n "$pkgpath"; then
336 pkgpatharg="-fgo-pkgpath=$pkgpath"
337 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
338 elif test -n "$prefix"; then
339 prefixarg="-fgo-prefix=$prefix"
342 if test "$trace" = "true"; then
343 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
345 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
347 if $havex; then
348 mkdir -p `dirname $package`
349 cp _gotest_.o `dirname $package`/lib`basename $package`.a
350 if test "$trace" = "true"; then
351 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
353 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
356 # They all compile; now generate the code to call them.
358 testname() {
359 # Remove the package from the name used with the -test option.
360 echo $1 | sed 's/^.*\.//'
363 localname() {
364 # The package main has been renamed to __main__ when imported.
365 # Adjust its uses.
366 echo $1 | sed 's/^main\./__main__./'
370 text="T"
371 case "$GOARCH" in
372 ppc64) text="[TD]" ;;
373 esac
375 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
377 # test functions are named TestFoo
378 # the grep -v eliminates methods and other special names
379 # that have multiple dots.
380 pattern='Test([^a-z].*)?'
381 # The -p option tells GNU nm not to sort.
382 # The -v option tells Solaris nm to sort by value.
383 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
384 if [ "x$tests" = x ]; then
385 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
386 exit 2
388 # benchmarks are named BenchmarkFoo.
389 pattern='Benchmark([^a-z].*)?'
390 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
392 # examples are named ExampleFoo
393 pattern='Example([^a-z].*)?'
394 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
396 # package spec
397 echo 'package main'
398 echo
399 # imports
400 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
401 echo 'import "./_gotest_"'
403 if $havex; then
404 echo 'import "./_xtest_"'
406 echo 'import "testing"'
407 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
408 # test array
409 echo
410 echo 'var tests = []testing.InternalTest {'
411 for i in $tests
413 n=$(testname $i)
414 j=$(localname $i)
415 echo ' {"'$n'", '$j'},'
416 done
417 echo '}'
419 # benchmark array
420 # The comment makes the multiline declaration
421 # gofmt-safe even when there are no benchmarks.
422 echo 'var benchmarks = []testing.InternalBenchmark{ //'
423 for i in $benchmarks
425 n=$(testname $i)
426 j=$(localname $i)
427 echo ' {"'$n'", '$j'},'
428 done
429 echo '}'
431 # examples array
432 echo 'var examples = []testing.InternalExample{ //'
433 # This doesn't work because we don't pick up the output.
434 #for i in $examples
436 # n=$(testname $i)
437 # j=$(localname $i)
438 # echo ' {"'$n'", '$j', ""},'
439 #done
440 echo '}'
442 # body
443 echo \
445 var matchPat string
446 var matchRe *__regexp__.Regexp
448 func matchString(pat, str string) (result bool, err error) {
449 if matchRe == nil || matchPat != pat {
450 matchPat = pat
451 matchRe, err = __regexp__.Compile(matchPat)
452 if err != nil {
453 return
456 return matchRe.MatchString(str), nil
459 func main() {
460 testing.Main(matchString, tests, benchmarks, examples)
462 }>_testmain.go
464 case "x$dejagnu" in
465 xno)
466 if test "$trace" = "true"; then
467 echo ${GC} -g -c _testmain.go
469 ${GC} -g -c _testmain.go
471 if test "$trace" = "true"; then
472 echo ${GL} *.o ${GOLIBS}
474 ${GL} *.o ${GOLIBS}
476 if test "$trace" = "true"; then
477 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
479 ./a.out -test.short -test.timeout=${timeout}s "$@" &
480 pid=$!
481 (sleep `expr $timeout + 10`
482 echo > gotest-timeout
483 echo "timed out in gotest" 1>&2
484 kill -9 $pid) &
485 alarmpid=$!
486 wait $pid
487 status=$?
488 if ! test -f gotest-timeout; then
489 kill $alarmpid
491 exit $status
493 xyes)
494 rm -rf ../../testsuite/*.o
495 files=`echo *`
496 for f in $files; do
497 if test "$f" = "_obj" || test "$f" = "_test"; then
498 continue
500 rm -rf ../../testsuite/$f
501 if test -f $f; then
502 cp $f ../../testsuite/
503 else
504 ln -s ../$DIR/test/$f ../../testsuite/
506 done
507 cd ../../testsuite
508 rm -rf _obj _test
509 mkdir _obj _test
510 if test "$testname" != ""; then
511 GOTESTNAME="$testname"
512 export GOTESTNAME
514 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
515 # Useful when using make check-target-libgo
516 cat libgo.log >> libgo-all.log
517 cat libgo.sum >> libgo-all.sum
518 rm -rf $files
520 esac