2016-12-07 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / testsuite / gotest
blob980e9483a6f589007abc508922bfaf0e99ac3fa3
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 LANG=C
14 LC_ALL=C
15 LC_CTYPE=C
16 export LANG LC_ALL LC_CTYPE
18 GC=${GC:-gccgo}
19 GL=${GL:-${GC-gccgo}}
20 GOLIBS=${GOLIBS:-}
21 export GC GL GOLIBS
23 NM=${NM:-nm}
25 # srcdir is where the source files are found. basedir is where the
26 # source file paths are relative to.
27 # gofiles are the test files. pkgfiles are the source files.
28 srcdir=.
29 basedir=.
30 goarch=""
31 gofiles=""
32 goos=""
33 pkgfiles=""
34 loop=true
35 keep=false
36 pkgpath=
37 prefix=
38 dejagnu=no
39 timeout=240
40 testname=""
41 bench=""
42 trace=false
43 while $loop; do
44 case "x$1" in
45 x--srcdir)
46 srcdir=$2
47 shift
48 shift
50 x--srcdir=*)
51 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
52 shift
54 x--basedir)
55 basedir=$2
56 shift
57 shift
59 x--basedir=*)
60 basedir=`echo $1 | sed -e 's/^--basedir=//'`
61 shift
63 x--goarch)
64 goarch=$2
65 shift
66 shift
68 x--goarch=*)
69 goarch=`echo $1 | sed -e 's/^--goarch=//'`
70 shift
72 x--goos)
73 goos=$2
74 shift
75 shift
77 x--goos=*)
78 goos=`echo $1 | sed -e 's/^--goos=//'`
79 shift
81 x--pkgpath)
82 pkgpath=$2
83 shift
84 shift
86 x--pkgpath=*)
87 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
88 shift
90 x--prefix)
91 prefix=$2
92 shift
93 shift
95 x--prefix=*)
96 prefix=`echo $1 | sed -e 's/^--prefix=//'`
97 shift
99 x--keep)
100 keep=true
101 shift
103 x--pkgfiles)
104 pkgfiles=$2
105 shift
106 shift
108 x--pkgfiles=*)
109 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
110 shift
112 x--dejagnu)
113 dejagnu=$2
114 shift
115 shift
117 x--dejagnu=*)
118 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
119 shift
121 x--timeout)
122 timeout=$2
123 shift
124 shift
126 x--timeout=*)
127 timeout=`echo $1 | sed -e 's/^--timeout=//'`
128 shift
130 x--testname)
131 testname=$2
132 shift
133 shift
135 x--testname=*)
136 testname=`echo $1 | sed -e 's/^--testname=//'`
137 shift
139 x--bench)
140 bench=$2
141 shift
142 shift
144 x--bench=*)
145 bench=`echo $1 | sed -e 's/^--bench=//'`
146 shift
148 x--trace)
149 trace=true
150 shift
152 x-*)
153 loop=false
156 loop=false
159 gofiles="$gofiles $1"
160 shift
162 esac
163 done
165 DIR=gotest$$
166 rm -rf $DIR
167 mkdir $DIR
169 cd $DIR
170 mkdir test
171 cd test
173 if test $keep = false; then
174 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
175 else
176 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
179 case "$srcdir" in
183 srcdir="../../$srcdir"
185 esac
187 SRCDIR=$srcdir
188 export SRCDIR
190 case "$basedir" in
194 basedir="../../$basedir"
196 esac
198 # Link all the files/directories in srcdir into our working directory,
199 # so that the tests do not have to refer to srcdir to find test data.
200 ln -s $srcdir/* .
202 # Some tests refer to a ../testdata directory.
203 if test -e $srcdir/../testdata; then
204 rm -f ../testdata
205 abssrcdir=`cd $srcdir && pwd`
206 ln -s $abssrcdir/../testdata ../testdata
209 # Copy the .go files because io/utils_test.go expects a regular file.
210 case "x$gofiles" in
212 case "x$pkgfiles" in
214 for f in `cd $srcdir; ls *.go`; do
215 rm -f $f;
216 cp $srcdir/$f .
217 done
220 for f in $pkgfiles; do
221 case $f in
223 b=`basename $f`
224 rm -f $b
225 cp $f $b
228 if test -f $basedir/$f; then
229 b=`basename $f`
230 rm -f $b
231 cp $basedir/$f $b
232 elif test -f ../../$f; then
233 b=`basename $f`
234 rm -f $b
235 cp ../../$f $b
236 else
237 echo "file $f not found" 1>&2
238 exit 1
241 esac
242 done
243 for f in `cd $srcdir; ls *_test.go`; do
244 rm -f $f
245 cp $srcdir/$f .
246 done
248 esac
251 for f in $gofiles; do
252 b=`basename $f`
253 rm -f $b
254 cp $basedir/$f $b
255 done
256 case "x$pkgfiles" in
258 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
259 rm -f $f
260 cp $srcdir/$f .
261 done
264 for f in $pkgfiles; do
265 case $f in
267 b=`basename $f`
268 rm -f $b
269 cp $f $b
272 if test -f $basedir/$f; then
273 b=`basename $f`
274 rm -f $b
275 cp $basedir/$f $b
276 elif test -f ../../$f; then
277 b=`basename $f`
278 rm -f $b
279 cp ../../$f $b
280 else
281 echo "file $f not found" 1>&2
282 exit 1
285 esac
286 done
288 esac
290 esac
292 # Some tests expect the _obj directory created by the gc Makefiles.
293 mkdir _obj
295 # Some tests expect the _test directory created by the gc Makefiles.
296 mkdir _test
298 case "x$gofiles" in
300 for f in `ls *_test.go`; do
301 tag1=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
302 tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
303 if test x$tag1 = x$f; then
304 tag1=
306 if test x$tag2 = x$f; then
307 tag2=
310 case "$tag1" in
311 "") ;;
312 $goarch) ;;
313 $goos) ;;
314 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
315 tag1=nonmatchingtag
317 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64)
318 tag1=nonmatchingtag
320 esac
322 case "$tag2" in
323 "") ;;
324 $goarch) ;;
325 $goos) ;;
326 android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
327 tag2=nonmatchingtag
329 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | mipso32 | mipsn32 | mipsn64 | mipso64 | ppc | s390 | s390x | sparc | sparc64)
330 tag2=nonmatchingtag
332 esac
334 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
335 tags=`sed '/^package /q' < $f | grep '^// +build '`
336 omatch=true
337 first=true
338 match=false
339 for tag in $tags; do
340 case $tag in
341 "//")
343 "+build")
344 if test "$first" = "true"; then
345 first=false
346 elif test "$match" = "false"; then
347 omatch=false
349 match=false
351 $goos | $goarch | cgo)
352 match=true
354 "!"$goos | "!"$goarch | "!cgo")
356 *,*)
357 cmatch=true
358 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
359 case $ctag in
360 $goos | $goarch | cgo)
362 "!"$goos | "!"$goarch | "!cgo")
363 cmatch=false
365 "!"*)
368 cmatch=false
370 esac
371 done
372 if test "$cmatch" = "true"; then
373 match=true
376 "!"*)
377 match=true
379 esac
380 done
382 if test "$match" = "false" -a "$first" = "false"; then
383 omatch=false
386 if test "$omatch" = "true"; then
387 gofiles="$gofiles $f"
390 done
393 xgofiles=$gofiles
394 gofiles=
395 for f in $xgofiles; do
396 gofiles="$gofiles `basename $f`"
397 done
398 esac
400 case "x$gofiles" in
402 echo 'no test files found' 1>&2
403 exit 1
405 esac
407 # Run any commands given in sources, like
408 # // gotest: $GC foo.go
409 # to build any test-only dependencies.
410 holdGC="$GC"
411 GC="$GC -g -c -I ."
412 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
413 GC="$holdGC"
415 case "x$pkgfiles" in
417 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
420 for f in $pkgfiles; do
421 pkgbasefiles="$pkgbasefiles `basename $f`"
422 done
424 esac
426 case "x$pkgfiles" in
428 echo 'no source files found' 1>&2
429 exit 1
431 esac
433 # Split $gofiles into external gofiles (those in *_test packages)
434 # and internal ones (those in the main package).
435 xgofiles=
436 for f in $gofiles; do
437 package=`grep '^package[ ]' $f | sed 1q`
438 case "$package" in
439 *_test)
440 xgofiles="$xgofiles $f"
443 ngofiles="$ngofiles $f"
445 esac
446 done
447 gofiles=$ngofiles
449 # External $O file
450 xofile=""
451 havex=false
452 if [ "x$xgofiles" != "x" ]; then
453 xofile="_xtest_.o"
454 havex=true
457 testmain=
458 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then
459 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
460 testmain="${package}.TestMain"
461 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then
462 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
463 testmain="${package}.TestMain"
466 set -e
468 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
470 pkgpatharg=
471 xpkgpatharg=
472 prefixarg=
473 if test -n "$pkgpath"; then
474 pkgpatharg="-fgo-pkgpath=$pkgpath"
475 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
476 elif test -n "$prefix"; then
477 prefixarg="-fgo-prefix=$prefix"
480 if test "$trace" = "true"; then
481 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
483 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
485 if $havex; then
486 mkdir -p `dirname $package`
487 cp _gotest_.o `dirname $package`/lib`basename $package`.a
488 if test "$trace" = "true"; then
489 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
491 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
494 # They all compile; now generate the code to call them.
496 testname() {
497 # Remove the package from the name used with the -test option.
498 echo $1 | sed 's/^.*\.//'
501 localname() {
502 # The package main has been renamed to __main__ when imported.
503 # Adjust its uses.
504 echo $1 | sed 's/^main\./__main__./'
508 text="T"
509 case "$goarch" in
510 ppc64*) text="[TD]" ;;
511 esac
513 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
515 # test functions are named TestFoo
516 # the grep -v eliminates methods and other special names
517 # that have multiple dots.
518 pattern='Test([^a-z].*)?'
519 # The -p option tells GNU nm not to sort.
520 # The -v option tells Solaris nm to sort by value.
521 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
522 if [ "x$tests" = x ]; then
523 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
524 exit 2
526 # benchmarks are named BenchmarkFoo.
527 pattern='Benchmark([^a-z].*)?'
528 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
530 # examples are named ExampleFoo
531 pattern='Example([^a-z].*)?'
532 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
534 # package spec
535 echo 'package main'
536 echo
537 # imports
538 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
539 echo 'import "./_gotest_"'
541 if $havex; then
542 echo 'import "./_xtest_"'
544 echo 'import "testing"'
545 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
546 if ! test -n "$testmain"; then
547 echo 'import __os__ "os"'
549 # test array
550 echo
551 echo 'var tests = []testing.InternalTest {'
552 for i in $tests
554 n=$(testname $i)
555 if test "$n" != "TestMain"; then
556 j=$(localname $i)
557 echo ' {"'$n'", '$j'},'
559 done
560 echo '}'
562 # benchmark array
563 # The comment makes the multiline declaration
564 # gofmt-safe even when there are no benchmarks.
565 echo 'var benchmarks = []testing.InternalBenchmark{ //'
566 for i in $benchmarks
568 n=$(testname $i)
569 j=$(localname $i)
570 echo ' {"'$n'", '$j'},'
571 done
572 echo '}'
574 # examples array
575 echo 'var examples = []testing.InternalExample{ //'
576 # This doesn't work because we don't pick up the output.
577 #for i in $examples
579 # n=$(testname $i)
580 # j=$(localname $i)
581 # echo ' {"'$n'", '$j', ""},'
582 #done
583 echo '}'
585 # body
586 echo \
588 var matchPat string
589 var matchRe *__regexp__.Regexp
591 func matchString(pat, str string) (result bool, err error) {
592 if matchRe == nil || matchPat != pat {
593 matchPat = pat
594 matchRe, err = __regexp__.Compile(matchPat)
595 if err != nil {
596 return
599 return matchRe.MatchString(str), nil
602 func main() {
603 m := testing.MainStart(matchString, tests, benchmarks, examples)
605 if test -n "$testmain"; then
606 echo " ${testmain}(m)"
607 else
608 echo ' __os__.Exit(m.Run())'
611 echo '}'
612 }>_testmain.go
614 case "x$dejagnu" in
615 xno)
616 if test "$trace" = "true"; then
617 echo ${GC} -g -c _testmain.go
619 ${GC} -g -c _testmain.go
621 if test "$trace" = "true"; then
622 echo ${GL} *.o ${GOLIBS}
624 ${GL} *.o ${GOLIBS}
626 set +e
627 if test "$bench" = ""; then
628 if test "$trace" = "true"; then
629 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
631 ./a.out -test.short -test.timeout=${timeout}s "$@" &
632 pid=$!
633 (sleep `expr $timeout + 10`
634 echo > gotest-timeout
635 echo "timed out in gotest" 1>&2
636 kill -9 $pid) &
637 alarmpid=$!
638 wait $pid
639 status=$?
640 if ! test -f gotest-timeout; then
641 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
642 kill $alarmpid
643 wait $alarmpid
644 if test "$sleeppid" != ""; then
645 kill $sleeppid
648 else
649 if test "$trace" = "true"; then
650 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
652 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
653 status=$?
655 exit $status
657 xyes)
658 rm -rf ../../testsuite/*.o
659 files=`echo *`
660 for f in $files; do
661 if test "$f" = "_obj" || test "$f" = "_test"; then
662 continue
664 rm -rf ../../testsuite/$f
665 if test -f $f; then
666 cp $f ../../testsuite/
667 else
668 ln -s ../$DIR/test/$f ../../testsuite/
670 done
671 cd ../../testsuite
672 rm -rf _obj _test
673 mkdir _obj _test
674 if test "$testname" != ""; then
675 GOTESTNAME="$testname"
676 export GOTESTNAME
678 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
679 # Useful when using make check-target-libgo
680 cat libgo.log >> libgo-all.log
681 cat libgo.sum >> libgo-all.sum
682 rm -rf $files
684 esac