Daily bump.
[official-gcc.git] / libgo / testsuite / gotest
blobe0dd16610db6769758d5ae9b1f7c23c6716f0e5a
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=600
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 case "x$gofiles" in
294 for f in `ls *_test.go`; do
295 tag1=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
296 tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
297 if test x$tag1 = x$f; then
298 tag1=
300 if test x$tag2 = x$f; then
301 tag2=
304 case "$tag1" in
305 "") ;;
306 $goarch) ;;
307 $goos) ;;
308 aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
309 tag1=nonmatchingtag
311 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
312 tag1=nonmatchingtag
315 # File name like x_amd64_random.go, where tag1=random.
316 # Don't match based on tag2.
317 tag2=
319 esac
321 case "$tag2" in
322 "") ;;
323 $goarch) ;;
324 $goos) ;;
325 aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
326 tag2=nonmatchingtag
328 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
329 tag2=nonmatchingtag
331 esac
333 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
334 tags=`sed '/^package /q' < $f | grep '^// *+build '`
335 omatch=true
336 first=true
337 match=false
338 for tag in $tags; do
339 case $tag in
340 "//")
342 "+build" | "//+build")
343 if test "$first" = "true"; then
344 first=false
345 elif test "$match" = "false"; then
346 omatch=false
348 match=false
350 $goos | $goarch | cgo | gccgo | goexperiment.fieldtrack | go1.[0-9] | go1.[0-9][0-9])
351 match=true
353 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
355 *,*)
356 cmatch=true
357 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
358 case $ctag in
359 $goos | $goarch | cgo | gccgo | goexperiment.fieldtrack | go1.[0-9] | go1.[0-9][0-9])
361 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
362 cmatch=false
364 "!"*)
367 cmatch=false
369 esac
370 done
371 if test "$cmatch" = "true"; then
372 match=true
375 "!"*)
376 match=true
378 esac
379 done
381 if test "$match" = "false" -a "$first" = "false"; then
382 omatch=false
385 if test "$omatch" = "true"; then
386 gofiles="$gofiles $f"
389 done
392 xgofiles=$gofiles
393 gofiles=
394 for f in $xgofiles; do
395 gofiles="$gofiles `basename $f`"
396 done
397 esac
399 case "x$gofiles" in
401 echo 'no test files found' 1>&2
402 exit 1
404 esac
406 case "x$pkgfiles" in
408 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
411 for f in $pkgfiles; do
412 pkgbasefiles="$pkgbasefiles `basename $f`"
413 done
415 esac
417 case "x$pkgfiles" in
419 echo 'no source files found' 1>&2
420 exit 1
422 esac
424 # Split $gofiles into external gofiles (those in *_test packages)
425 # and internal ones (those in the main package).
426 xgofiles=
427 xpackage=
428 for f in $gofiles; do
429 package=`grep '^package[ ]' $f | sed 1q`
430 case "$package" in
431 *_test)
432 xpackage=`echo $package | sed -e 's/package[ ]//' -e 's/[ ]*$//'`
433 xgofiles="$xgofiles $f"
436 ngofiles="$ngofiles $f"
438 esac
439 done
440 gofiles=$ngofiles
442 # External $O file
443 xofile=""
444 havex=false
445 if [ "x$xgofiles" != "x" ]; then
446 xofile="_xtest_.o"
447 havex=true
450 testmain=
451 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then
452 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
453 testmain="${package}.TestMain"
454 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then
455 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
456 testmain="${package}.TestMain"
459 set -e
461 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
463 pkgpatharg=
464 xpkgpatharg=
465 prefixarg=
466 if test -n "$pkgpath"; then
467 pkgpatharg="-fgo-pkgpath=$pkgpath"
468 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
469 elif test -n "$prefix"; then
470 prefixarg="-fgo-prefix=$prefix"
473 if test "$trace" = "true"; then
474 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
476 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
478 if $havex; then
479 mkdir -p `dirname $package`
480 cp _gotest_.o `dirname $package`/lib`basename $package`.a
482 # Force the test version of the package to be imported first,
483 # so that its type definitions will be used, in case any new
484 # methods appear in export_test.go files.
485 echo "package $xpackage" > _first_test.go
486 echo 'import _ "'$package'"' >> _first_test.go
488 if test "$trace" = "true"; then
489 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile _first_test.go $xgofiles
491 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile _first_test.go $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 # Also demangle underscores.
505 echo $1 | sed 's/^main\./__main__./' | sed 's/__/_/'
508 # Takes a list of tests derived from 'nm' output (whose symbols are mangled)
509 # and emits a demangled list of tests, using only the terminal package.
510 # Example:
512 # Original symbol: foo/bar/leaf.Mumble
513 # Mangled symbol: foo_1fbar_1leaf.Mumble
514 # Returned: leaf.Mumble
516 symtogo() {
517 result=""
518 for tp in $*; do
519 # Discard symbols with a leading dot.
520 # On AIX, this will remove function text symbols (with a leading dot).
521 # Therefore, only function descriptor symbols (without this leading dot)
522 # will be used to retrieve the go symbols, avoiding duplication.
523 if expr "$tp" : '^\.' >/dev/null 2>&1; then
524 continue
526 # Skip type descriptors. These are normally skipped because they
527 # are weak symbols, but if not using GNU nm we may see them here.
528 if expr "$tp" : '^type\.\.' >/dev/null 2>&1; then
529 continue
531 s=$(echo "$tp" | sed -e 's/_1/%/g' | sed -e 's/.*%//')
532 # Screen out methods (X.Y.Z).
533 if ! expr "$s" : '^[^.]*\.[^.]*$' >/dev/null 2>&1; then
534 continue
536 tname=$(testname $s)
537 # Skip TestMain.
538 if test x$tname = xTestMain; then
539 continue
541 # Check that the function is defined in a test file,
542 # not an ordinary non-test file.
543 if grep "^func $tname(" $gofiles $xgofiles >/dev/null 2>&1; then
544 echo "$s"
546 done
549 # Takes an example name and puts any output into the file example.txt.
550 # It strips comment markers but does not otherwise change the output.
551 exampleoutput() {
552 n=$(testname $1)
553 for f in $gofiles $xgofiles; do
554 if ! grep "^func $n(" $f >/dev/null 2>&1; then
555 continue
557 # Copy the output comment, if any, into example.txt.
558 # Remove the comment markers.
559 sed -n "/^func $n(/,/^}$/ p" $f |
560 sed -n '\|// \([Uu]nordered \)\?[Oo]utput:|,$ p' |
561 sed -n '\|//| s|[ ]*// \?||p' > example.txt
562 # Check whether we found an output comment.
563 if ! sed -n '1p' < example.txt | grep '[Oo]utput:' >/dev/null 2>&1; then
564 rm -f example.txt
566 return
567 done
571 # On systems using PPC64 ELF ABI v1 function symbols show up
572 # as descriptors in the data section.
573 text="[TD]"
575 # test functions are named TestFoo
576 # the grep -v eliminates methods and other special names
577 # that have multiple dots.
578 pattern='Test([^a-z].*)?'
579 # The -p option tells GNU nm not to sort.
580 # The -v option tells Solaris nm to sort by value.
581 testsyms=$($NM -p -v _gotest_.o | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
582 testxsyms=
583 if $havex; then
584 testxsyms=$($NM -p -v $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
585 testsyms="$testsyms $testxsyms"
587 tests=$(symtogo "$testsyms")
588 if [ "x$tests" = x ]; then
589 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
590 exit 2
592 # benchmarks are named BenchmarkFoo.
593 pattern='Benchmark([^a-z].*)?'
594 benchmarksyms=$($NM -p -v _gotest_.o | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
595 if $havex; then
596 benchmarkxsyms=$($NM -p -v $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
597 benchmarksyms="$benchmarksyms $benchmarkxsyms"
599 benchmarks=$(symtogo "$benchmarksyms")
601 # examples are named ExampleFoo
602 pattern='Example([^a-z].*)?'
603 examplesyms=$($NM -p -v _gotest_.o | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
604 if $havex; then
605 examplexsyms=$($NM -p -v $xofile | egrep " $text .*\."$pattern'$' | fgrep -v ' __go_' | egrep -v '\.\.\w+$' | sed 's/.* //')
606 examplesyms="$examplesyms $examplexsyms"
608 examples=$(symtogo "$examplesyms")
610 # package spec
611 echo 'package main'
612 echo
613 # imports
614 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
615 echo 'import "./_gotest_"'
617 if $havex; then
618 needxtest=false
619 if test -n "$testxsyms" -o -n "$benchmarkxsyms"; then
620 needxtest=true
621 else
622 # Check whether any example has output.
623 for i in $(symtogo "$examplexsyms"); do
624 exampleoutput $i
625 if test -f example.txt; then
626 rm -f example.txt
627 needxtest=true
628 break
630 done
632 if test x$needxtest = xtrue; then
633 echo 'import "./_xtest_"'
634 else
635 echo 'import _ "./_xtest_"'
638 if test "$package" != "testing"; then
639 echo 'import "testing"'
641 echo 'import "testing/internal/testdeps"'
642 if ! test -n "$testmain"; then
643 echo 'import __os__ "os"'
645 # test array
646 echo
647 echo 'var tests = []testing.InternalTest {'
648 for i in $tests; do
649 n=$(testname $i)
650 j=$(localname $i)
651 echo ' {"'$n'", '$j'},'
652 done
653 echo '}'
655 # benchmark array
656 # The comment makes the multiline declaration
657 # gofmt-safe even when there are no benchmarks.
658 echo 'var benchmarks = []testing.InternalBenchmark{'
659 for i in $benchmarks; do
660 n=$(testname $i)
661 j=$(localname $i)
662 echo ' {"'$n'", '$j'},'
663 done
664 echo '}'
666 # examples array
667 echo 'var examples = []testing.InternalExample{'
668 for i in $examples; do
669 n=$(testname $i)
670 j=$(localname $i)
671 # Look for a //output comment.
672 hasoutput=false
673 unordered=false
674 output=
675 exampleoutput $i
676 if ! test -f example.txt; then
677 continue
679 # Check whether the output can be unordered.
680 unordered=false
681 if sed -n '1p' < example.txt | grep -i unordered >/dev/null 2>&1; then
682 unordered=true
684 # Remove the output header.
685 # Quote backslashes.
686 # Quote quotation characters.
687 # Turn tab into \t.
688 # Turn pairs of spaces into " \x20", because $() will
689 # drop duplicate spaces.
690 # Drop trailing spaces, and turn newlines into \n.
691 # Remove leading and trailing \n.
692 sed '1 s/\([Uu]nordered \)\?[Oo]utput:[ ]*//' < example.txt |
693 sed -e 's/\\/\\\\/g' \
694 -e 's/"/\\"/g' \
695 -e 's/ /\\t/g' \
696 -e 's/ / \\x20/g' \
697 -e 's/[ ]*$/\\n/g' |
698 tr -d '\n' |
699 sed -e 's/^\(\\n\)*//' \
700 -e 's/\(\\n\)*$//' > example2.txt
701 hasoutput=true
702 echo ' {"'$n'", '$j','
703 sed -e 's/^/ "/' -e 's/$/", /' < example2.txt
704 echo $unordered'},'
705 rm -f example.txt example2.txt
706 done
707 echo '}'
709 # body
710 echo \
712 func main() {
713 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
715 if test -n "$testmain"; then
716 echo " ${testmain}(m)"
717 else
718 echo ' __os__.Exit(m.Run())'
721 echo '}'
722 }>_testmain.go
724 case "x$dejagnu" in
725 xno)
726 if test "$trace" = "true"; then
727 echo ${GC} -g -c _testmain.go
729 ${GC} -g -c _testmain.go
731 if test "$trace" = "true"; then
732 echo ${GL} *.o ${GOLIBS}
734 ${GL} *.o ${GOLIBS}
736 set +e
737 if test "$bench" = ""; then
738 if test "$trace" = "true"; then
739 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
741 ./a.out -test.short -test.timeout=${timeout}s "$@" &
742 pid=$!
743 (sleep `expr $timeout + 10`
744 echo > gotest-timeout
745 echo "timed out in gotest" 1>&2
746 kill -9 $pid) &
747 alarmpid=$!
748 wait $pid
749 status=$?
750 if ! test -f gotest-timeout; then
751 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
752 kill $alarmpid
753 wait $alarmpid
754 if test "$sleeppid" != ""; then
755 kill $sleeppid
758 else
759 if test "$trace" = "true"; then
760 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
762 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
763 status=$?
765 exit $status
767 xyes)
768 rm -rf ../../testsuite/*.o
769 files=`echo *`
770 for f in $files; do
771 if test "$f" = "_obj" || test "$f" = "_test"; then
772 continue
774 rm -rf ../../testsuite/$f
775 if test -f $f; then
776 cp $f ../../testsuite/
777 else
778 ln -s ../$DIR/test/$f ../../testsuite/
780 done
781 cd ../../testsuite
782 rm -rf _obj _test
783 mkdir _obj _test
784 if test "$testname" != ""; then
785 GOTESTNAME="$testname"
786 export GOTESTNAME
788 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
789 # Useful when using make check-target-libgo
790 cat libgo.log >> libgo-all.log
791 cat libgo.sum >> libgo-all.sum
792 rm -rf $files
794 esac