Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / libgo / testsuite / gotest
blobdba7408a07cfa563106f6e83dde697500e8ec285
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 aix | 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 | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le·| nios2 | ppc | ppc64 | ppc64le | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
318 tag1=nonmatchingtag
320 esac
322 case "$tag2" in
323 "") ;;
324 $goarch) ;;
325 $goos) ;;
326 aix | 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 | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le·| nios2 | ppc | ppc64 | ppc64le | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
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 | gccgo | go1.[0-9])
352 match=true
354 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!"go1.[0-9])
356 *,*)
357 cmatch=true
358 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
359 case $ctag in
360 $goos | $goarch | cgo | gccgo | go1.[0-9])
362 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!"go1.[0-9])
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__./'
507 # Takes a list of tests derived from 'nm' output (whose symbols are mangled)
508 # and emits a demangled list of tests, using only the terminal package.
509 # Example:
511 # Original symbol: foo/bar/leaf.Mumble
512 # Mangled symbol: foo..z2fbar..z2fleaf.Mumble
513 # Returned: leaf.Mumble
515 symtogo() {
516 result=""
517 for tp in $*
519 s=$(echo "$tp" | sed -e 's/\.\.z2f/%/g' | sed -e 's/.*%//')
520 # screen out methods (X.Y.Z)
521 if ! expr "$s" : '^[^.]*\.[^.]*$' >/dev/null 2>&1; then
522 continue
524 echo "$s"
525 done
529 text="T"
531 # On systems using PPC64 ELF ABI v1 function symbols show up
532 # as descriptors in the data section. We assume that $goarch
533 # distinguishes v1 (ppc64) from v2 (ppc64le).
534 if test "$goos" != "aix" && test "$goarch" = "ppc64"; then
535 text="[TD]"
538 # test functions are named TestFoo
539 # the grep -v eliminates methods and other special names
540 # that have multiple dots.
541 pattern='Test([^a-z].*)?'
542 # The -p option tells GNU nm not to sort.
543 # The -v option tells Solaris nm to sort by value.
544 testsyms=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
545 tests=$(symtogo "$testsyms")
546 if [ "x$tests" = x ]; then
547 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
548 exit 2
550 # benchmarks are named BenchmarkFoo.
551 pattern='Benchmark([^a-z].*)?'
552 benchmarksyms=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
553 benchmarks=$(symtogo "$benchmarksyms")
555 # examples are named ExampleFoo
556 pattern='Example([^a-z].*)?'
557 examplesyms=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
558 examples=$(symtogo "$examplesyms")
560 # package spec
561 echo 'package main'
562 echo
563 # imports
564 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
565 echo 'import "./_gotest_"'
567 if $havex; then
568 echo 'import "./_xtest_"'
570 echo 'import "testing"'
571 echo 'import "testing/internal/testdeps"'
572 if ! test -n "$testmain"; then
573 echo 'import __os__ "os"'
575 # test array
576 echo
577 echo 'var tests = []testing.InternalTest {'
578 for i in $tests
580 n=$(testname $i)
581 if test "$n" != "TestMain"; then
582 j=$(localname $i)
583 echo ' {"'$n'", '$j'},'
585 done
586 echo '}'
588 # benchmark array
589 # The comment makes the multiline declaration
590 # gofmt-safe even when there are no benchmarks.
591 echo 'var benchmarks = []testing.InternalBenchmark{ //'
592 for i in $benchmarks
594 n=$(testname $i)
595 j=$(localname $i)
596 echo ' {"'$n'", '$j'},'
597 done
598 echo '}'
600 # examples array
601 echo 'var examples = []testing.InternalExample{ //'
602 # This doesn't work because we don't pick up the output.
603 #for i in $examples
605 # n=$(testname $i)
606 # j=$(localname $i)
607 # echo ' {"'$n'", '$j', ""},'
608 #done
609 echo '}'
611 # body
612 echo \
614 func main() {
615 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
617 if test -n "$testmain"; then
618 echo " ${testmain}(m)"
619 else
620 echo ' __os__.Exit(m.Run())'
623 echo '}'
624 }>_testmain.go
626 case "x$dejagnu" in
627 xno)
628 if test "$trace" = "true"; then
629 echo ${GC} -g -c _testmain.go
631 ${GC} -g -c _testmain.go
633 if test "$trace" = "true"; then
634 echo ${GL} *.o ${GOLIBS}
636 ${GL} *.o ${GOLIBS}
638 set +e
639 if test "$bench" = ""; then
640 if test "$trace" = "true"; then
641 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
643 ./a.out -test.short -test.timeout=${timeout}s "$@" &
644 pid=$!
645 (sleep `expr $timeout + 10`
646 echo > gotest-timeout
647 echo "timed out in gotest" 1>&2
648 kill -9 $pid) &
649 alarmpid=$!
650 wait $pid
651 status=$?
652 if ! test -f gotest-timeout; then
653 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
654 kill $alarmpid
655 wait $alarmpid
656 if test "$sleeppid" != ""; then
657 kill $sleeppid
660 else
661 if test "$trace" = "true"; then
662 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
664 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
665 status=$?
667 exit $status
669 xyes)
670 rm -rf ../../testsuite/*.o
671 files=`echo *`
672 for f in $files; do
673 if test "$f" = "_obj" || test "$f" = "_test"; then
674 continue
676 rm -rf ../../testsuite/$f
677 if test -f $f; then
678 cp $f ../../testsuite/
679 else
680 ln -s ../$DIR/test/$f ../../testsuite/
682 done
683 cd ../../testsuite
684 rm -rf _obj _test
685 mkdir _obj _test
686 if test "$testname" != ""; then
687 GOTESTNAME="$testname"
688 export GOTESTNAME
690 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
691 # Useful when using make check-target-libgo
692 cat libgo.log >> libgo-all.log
693 cat libgo.sum >> libgo-all.sum
694 rm -rf $files
696 esac