PR c++/61198
[official-gcc.git] / libgo / testsuite / gotest
blob1bcd4b9d31305956614e39d829ebc58caf9423eb
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 set -e
340 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
342 pkgpatharg=
343 xpkgpatharg=
344 prefixarg=
345 if test -n "$pkgpath"; then
346 pkgpatharg="-fgo-pkgpath=$pkgpath"
347 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
348 elif test -n "$prefix"; then
349 prefixarg="-fgo-prefix=$prefix"
352 if test "$trace" = "true"; then
353 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
355 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
357 if $havex; then
358 mkdir -p `dirname $package`
359 cp _gotest_.o `dirname $package`/lib`basename $package`.a
360 if test "$trace" = "true"; then
361 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
363 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
366 # They all compile; now generate the code to call them.
368 testname() {
369 # Remove the package from the name used with the -test option.
370 echo $1 | sed 's/^.*\.//'
373 localname() {
374 # The package main has been renamed to __main__ when imported.
375 # Adjust its uses.
376 echo $1 | sed 's/^main\./__main__./'
380 text="T"
381 case "$GOARCH" in
382 ppc64*) text="[TD]" ;;
383 esac
385 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
387 # test functions are named TestFoo
388 # the grep -v eliminates methods and other special names
389 # that have multiple dots.
390 pattern='Test([^a-z].*)?'
391 # The -p option tells GNU nm not to sort.
392 # The -v option tells Solaris nm to sort by value.
393 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
394 if [ "x$tests" = x ]; then
395 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
396 exit 2
398 # benchmarks are named BenchmarkFoo.
399 pattern='Benchmark([^a-z].*)?'
400 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
402 # examples are named ExampleFoo
403 pattern='Example([^a-z].*)?'
404 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
406 # package spec
407 echo 'package main'
408 echo
409 # imports
410 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
411 echo 'import "./_gotest_"'
413 if $havex; then
414 echo 'import "./_xtest_"'
416 echo 'import "testing"'
417 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
418 # test array
419 echo
420 echo 'var tests = []testing.InternalTest {'
421 for i in $tests
423 n=$(testname $i)
424 j=$(localname $i)
425 echo ' {"'$n'", '$j'},'
426 done
427 echo '}'
429 # benchmark array
430 # The comment makes the multiline declaration
431 # gofmt-safe even when there are no benchmarks.
432 echo 'var benchmarks = []testing.InternalBenchmark{ //'
433 for i in $benchmarks
435 n=$(testname $i)
436 j=$(localname $i)
437 echo ' {"'$n'", '$j'},'
438 done
439 echo '}'
441 # examples array
442 echo 'var examples = []testing.InternalExample{ //'
443 # This doesn't work because we don't pick up the output.
444 #for i in $examples
446 # n=$(testname $i)
447 # j=$(localname $i)
448 # echo ' {"'$n'", '$j', ""},'
449 #done
450 echo '}'
452 # body
453 echo \
455 var matchPat string
456 var matchRe *__regexp__.Regexp
458 func matchString(pat, str string) (result bool, err error) {
459 if matchRe == nil || matchPat != pat {
460 matchPat = pat
461 matchRe, err = __regexp__.Compile(matchPat)
462 if err != nil {
463 return
466 return matchRe.MatchString(str), nil
469 func main() {
470 testing.Main(matchString, tests, benchmarks, examples)
472 }>_testmain.go
474 case "x$dejagnu" in
475 xno)
476 if test "$trace" = "true"; then
477 echo ${GC} -g -c _testmain.go
479 ${GC} -g -c _testmain.go
481 if test "$trace" = "true"; then
482 echo ${GL} *.o ${GOLIBS}
484 ${GL} *.o ${GOLIBS}
486 if test "$bench" = ""; then
487 if test "$trace" = "true"; then
488 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
490 ./a.out -test.short -test.timeout=${timeout}s "$@" &
491 pid=$!
492 (sleep `expr $timeout + 10`
493 echo > gotest-timeout
494 echo "timed out in gotest" 1>&2
495 kill -9 $pid) &
496 alarmpid=$!
497 wait $pid
498 status=$?
499 if ! test -f gotest-timeout; then
500 kill $alarmpid
502 else
503 if test "$trace" = "true"; then
504 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
506 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
507 status=$?
509 exit $status
511 xyes)
512 rm -rf ../../testsuite/*.o
513 files=`echo *`
514 for f in $files; do
515 if test "$f" = "_obj" || test "$f" = "_test"; then
516 continue
518 rm -rf ../../testsuite/$f
519 if test -f $f; then
520 cp $f ../../testsuite/
521 else
522 ln -s ../$DIR/test/$f ../../testsuite/
524 done
525 cd ../../testsuite
526 rm -rf _obj _test
527 mkdir _obj _test
528 if test "$testname" != ""; then
529 GOTESTNAME="$testname"
530 export GOTESTNAME
532 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
533 # Useful when using make check-target-libgo
534 cat libgo.log >> libgo-all.log
535 cat libgo.sum >> libgo-all.sum
536 rm -rf $files
538 esac