* lto-cgraph.c (lto_output_node, input_overwrite_node): Stream
[official-gcc.git] / libgo / testsuite / gotest
blob3596d727ff1ff9bb593580c4e8dcce0c2cacedb4
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 bench=""
40 trace=false
41 while $loop; do
42 case "x$1" in
43 x--srcdir)
44 srcdir=$2
45 shift
46 shift
48 x--srcdir=*)
49 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
50 shift
52 x--basedir)
53 basedir=$2
54 shift
55 shift
57 x--basedir=*)
58 basedir=`echo $1 | sed -e 's/^--basedir=//'`
59 shift
61 x--pkgpath)
62 pkgpath=$2
63 shift
64 shift
66 x--pkgpath=*)
67 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
68 shift
70 x--prefix)
71 prefix=$2
72 shift
73 shift
75 x--prefix=*)
76 prefix=`echo $1 | sed -e 's/^--prefix=//'`
77 shift
79 x--keep)
80 keep=true
81 shift
83 x--pkgfiles)
84 pkgfiles=$2
85 shift
86 shift
88 x--pkgfiles=*)
89 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
90 shift
92 x--dejagnu)
93 dejagnu=$2
94 shift
95 shift
97 x--dejagnu=*)
98 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
99 shift
101 x--goarch)
102 GOARCH=$2
103 shift
104 shift
106 x--goarch=*)
107 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
108 shift
110 x--timeout)
111 timeout=$2
112 shift
113 shift
115 x--timeout=*)
116 timeout=`echo $1 | sed -e 's/^--timeout=//'`
117 shift
119 x--testname)
120 testname=$2
121 shift
122 shift
124 x--testname=*)
125 testname=`echo $1 | sed -e 's/^--testname=//'`
126 shift
128 x--bench)
129 bench=$2
130 shift
131 shift
133 x--bench=*)
134 bench=`echo $1 | sed -e 's/^--bench=//'`
135 shift
137 x--trace)
138 trace=true
139 shift
141 x-*)
142 loop=false
145 loop=false
148 gofiles="$gofiles $1"
149 shift
151 esac
152 done
154 DIR=gotest$$
155 rm -rf $DIR
156 mkdir $DIR
158 cd $DIR
159 mkdir test
160 cd test
162 if test $keep = false; then
163 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
164 else
165 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
168 case "$srcdir" in
172 srcdir="../../$srcdir"
174 esac
176 SRCDIR=$srcdir
177 export SRCDIR
179 case "$basedir" in
183 basedir="../../$basedir"
185 esac
187 # Link all the files/directories in srcdir into our working directory,
188 # so that the tests do not have to refer to srcdir to find test data.
189 ln -s $srcdir/* .
191 # Some tests refer to a ../testdata directory.
192 if test -e $srcdir/../testdata; then
193 rm -f ../testdata
194 abssrcdir=`cd $srcdir && pwd`
195 ln -s $abssrcdir/../testdata ../testdata
198 # Copy the .go files because io/utils_test.go expects a regular file.
199 case "x$gofiles" in
201 case "x$pkgfiles" in
203 for f in `cd $srcdir; ls *.go`; do
204 rm -f $f;
205 cp $srcdir/$f .
206 done
209 for f in $pkgfiles; do
210 if test -f $basedir/$f; then
211 b=`basename $f`
212 rm -f $b
213 cp $basedir/$f $b
214 elif test -f ../../$f; then
215 b=`basename $f`
216 rm -f $b
217 cp ../../$f $b
218 else
219 echo "file $f not found" 1>&2
220 exit 1
222 done
223 for f in `cd $srcdir; ls *_test.go`; do
224 rm -f $f
225 cp $srcdir/$f .
226 done
228 esac
231 for f in $gofiles; do
232 b=`basename $f`
233 rm -f $b
234 cp $basedir/$f $b
235 done
236 case "x$pkgfiles" in
238 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
239 rm -f $f
240 cp $srcdir/$f .
241 done
244 for f in $pkgfiles; do
245 if test -f $basedir/$f; then
246 b=`basename $f`
247 rm -f $b
248 cp $basedir/$f $b
249 elif test -f ../../$f; then
250 b=`basename $f`
251 rm -f $b
252 cp ../../$f $b
253 else
254 echo "file $f not found" 1>&2
255 exit 1
257 done
259 esac
261 esac
263 # Some tests expect the _obj directory created by the gc Makefiles.
264 mkdir _obj
266 # Some tests expect the _test directory created by the gc Makefiles.
267 mkdir _test
269 case "x$gofiles" in
271 gofiles=`ls *_test.go 2>/dev/null`
274 xgofiles=$gofiles
275 gofiles=
276 for f in $xgofiles; do
277 gofiles="$gofiles `basename $f`"
278 done
279 esac
281 case "x$gofiles" in
283 echo 'no test files found' 1>&2
284 exit 1
286 esac
288 # Run any commands given in sources, like
289 # // gotest: $GC foo.go
290 # to build any test-only dependencies.
291 holdGC="$GC"
292 GC="$GC -g -c -I ."
293 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
294 GC="$holdGC"
296 case "x$pkgfiles" in
298 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
301 for f in $pkgfiles; do
302 pkgbasefiles="$pkgbasefiles `basename $f`"
303 done
305 esac
307 case "x$pkgfiles" in
309 echo 'no source files found' 1>&2
310 exit 1
312 esac
314 # Split $gofiles into external gofiles (those in *_test packages)
315 # and internal ones (those in the main package).
316 xgofiles=
317 for f in $gofiles; do
318 package=`grep '^package[ ]' $f | sed 1q`
319 case "$package" in
320 *_test)
321 xgofiles="$xgofiles $f"
324 ngofiles="$ngofiles $f"
326 esac
327 done
328 gofiles=$ngofiles
330 # External $O file
331 xofile=""
332 havex=false
333 if [ "x$xgofiles" != "x" ]; then
334 xofile="_xtest_.o"
335 havex=true
338 testmain=
339 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then
340 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
341 testmain="${package}.TestMain"
342 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then
343 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
344 testmain="${package}.TestMain"
347 set -e
349 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
351 pkgpatharg=
352 xpkgpatharg=
353 prefixarg=
354 if test -n "$pkgpath"; then
355 pkgpatharg="-fgo-pkgpath=$pkgpath"
356 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
357 elif test -n "$prefix"; then
358 prefixarg="-fgo-prefix=$prefix"
361 if test "$trace" = "true"; then
362 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
364 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
366 if $havex; then
367 mkdir -p `dirname $package`
368 cp _gotest_.o `dirname $package`/lib`basename $package`.a
369 if test "$trace" = "true"; then
370 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
372 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
375 # They all compile; now generate the code to call them.
377 testname() {
378 # Remove the package from the name used with the -test option.
379 echo $1 | sed 's/^.*\.//'
382 localname() {
383 # The package main has been renamed to __main__ when imported.
384 # Adjust its uses.
385 echo $1 | sed 's/^main\./__main__./'
389 text="T"
390 case "$GOARCH" in
391 ppc64*) text="[TD]" ;;
392 esac
394 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
396 # test functions are named TestFoo
397 # the grep -v eliminates methods and other special names
398 # that have multiple dots.
399 pattern='Test([^a-z].*)?'
400 # The -p option tells GNU nm not to sort.
401 # The -v option tells Solaris nm to sort by value.
402 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
403 if [ "x$tests" = x ]; then
404 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
405 exit 2
407 # benchmarks are named BenchmarkFoo.
408 pattern='Benchmark([^a-z].*)?'
409 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
411 # examples are named ExampleFoo
412 pattern='Example([^a-z].*)?'
413 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
415 # package spec
416 echo 'package main'
417 echo
418 # imports
419 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
420 echo 'import "./_gotest_"'
422 if $havex; then
423 echo 'import "./_xtest_"'
425 echo 'import "testing"'
426 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
427 if ! test -n "$testmain"; then
428 echo 'import __os__ "os"'
430 # test array
431 echo
432 echo 'var tests = []testing.InternalTest {'
433 for i in $tests
435 n=$(testname $i)
436 if test "$n" != "TestMain"; then
437 j=$(localname $i)
438 echo ' {"'$n'", '$j'},'
440 done
441 echo '}'
443 # benchmark array
444 # The comment makes the multiline declaration
445 # gofmt-safe even when there are no benchmarks.
446 echo 'var benchmarks = []testing.InternalBenchmark{ //'
447 for i in $benchmarks
449 n=$(testname $i)
450 j=$(localname $i)
451 echo ' {"'$n'", '$j'},'
452 done
453 echo '}'
455 # examples array
456 echo 'var examples = []testing.InternalExample{ //'
457 # This doesn't work because we don't pick up the output.
458 #for i in $examples
460 # n=$(testname $i)
461 # j=$(localname $i)
462 # echo ' {"'$n'", '$j', ""},'
463 #done
464 echo '}'
466 # body
467 echo \
469 var matchPat string
470 var matchRe *__regexp__.Regexp
472 func matchString(pat, str string) (result bool, err error) {
473 if matchRe == nil || matchPat != pat {
474 matchPat = pat
475 matchRe, err = __regexp__.Compile(matchPat)
476 if err != nil {
477 return
480 return matchRe.MatchString(str), nil
483 func main() {
484 m := testing.MainStart(matchString, tests, benchmarks, examples)
486 if test -n "$testmain"; then
487 echo " ${testmain}(m)"
488 else
489 echo ' __os__.Exit(m.Run())'
492 echo '}'
493 }>_testmain.go
495 case "x$dejagnu" in
496 xno)
497 if test "$trace" = "true"; then
498 echo ${GC} -g -c _testmain.go
500 ${GC} -g -c _testmain.go
502 if test "$trace" = "true"; then
503 echo ${GL} *.o ${GOLIBS}
505 ${GL} *.o ${GOLIBS}
507 if test "$bench" = ""; then
508 if test "$trace" = "true"; then
509 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
511 ./a.out -test.short -test.timeout=${timeout}s "$@" &
512 pid=$!
513 (sleep `expr $timeout + 10`
514 echo > gotest-timeout
515 echo "timed out in gotest" 1>&2
516 kill -9 $pid) &
517 alarmpid=$!
518 wait $pid
519 status=$?
520 if ! test -f gotest-timeout; then
521 kill $alarmpid
523 else
524 if test "$trace" = "true"; then
525 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
527 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
528 status=$?
530 exit $status
532 xyes)
533 rm -rf ../../testsuite/*.o
534 files=`echo *`
535 for f in $files; do
536 if test "$f" = "_obj" || test "$f" = "_test"; then
537 continue
539 rm -rf ../../testsuite/$f
540 if test -f $f; then
541 cp $f ../../testsuite/
542 else
543 ln -s ../$DIR/test/$f ../../testsuite/
545 done
546 cd ../../testsuite
547 rm -rf _obj _test
548 mkdir _obj _test
549 if test "$testname" != ""; then
550 GOTESTNAME="$testname"
551 export GOTESTNAME
553 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
554 # Useful when using make check-target-libgo
555 cat libgo.log >> libgo-all.log
556 cat libgo.sum >> libgo-all.sum
557 rm -rf $files
559 esac