2012-10-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / testsuite / gotest
blob208cbaf8af31e318b3b7aa1dd41c6809fbe26cb5
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 trace=false
40 while $loop; do
41 case "x$1" in
42 x--srcdir)
43 srcdir=$2
44 shift
45 shift
47 x--srcdir=*)
48 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
49 shift
51 x--basedir)
52 basedir=$2
53 shift
54 shift
56 x--basedir=*)
57 basedir=`echo $1 | sed -e 's/^--basedir=//'`
58 shift
60 x--pkgpath)
61 pkgpath=$2
62 shift
63 shift
65 x--pkgpath=*)
66 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
67 shift
69 x--prefix)
70 prefix=$2
71 shift
72 shift
74 x--prefix=*)
75 prefix=`echo $1 | sed -e 's/^--prefix=//'`
76 shift
78 x--keep)
79 keep=true
80 shift
82 x--pkgfiles)
83 pkgfiles=$2
84 shift
85 shift
87 x--pkgfiles=*)
88 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
89 shift
91 x--dejagnu)
92 dejagnu=$2
93 shift
94 shift
96 x--dejagnu=*)
97 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
98 shift
100 x--goarch)
101 GOARCH=$2
102 shift
103 shift
105 x--goarch=*)
106 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
107 shift
109 x--timeout)
110 timeout=$2
111 shift
112 shift
114 x--timeout=*)
115 timeout=`echo $1 | sed -e 's/^--timeout=//'`
116 shift
118 x--testname)
119 testname=$2
120 shift
121 shift
123 x--testname=*)
124 testname=`echo $1 | sed -e 's/^--testname=//'`
125 shift
127 x--trace)
128 trace=true
129 shift
131 x-*)
132 loop=false
135 loop=false
138 gofiles="$gofiles $1"
139 shift
141 esac
142 done
144 DIR=gotest$$
145 rm -rf $DIR
146 mkdir $DIR
148 cd $DIR
149 mkdir test
150 cd test
152 if test $keep = false; then
153 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
154 else
155 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
158 case "$srcdir" in
162 srcdir="../../$srcdir"
164 esac
166 SRCDIR=$srcdir
167 export SRCDIR
169 case "$basedir" in
173 basedir="../../$basedir"
175 esac
177 # Link all the files/directories in srcdir into our working directory,
178 # so that the tests do not have to refer to srcdir to find test data.
179 ln -s $srcdir/* .
181 # Some tests refer to a ../testdata directory.
182 if test -e $srcdir/../testdata; then
183 rm -f ../testdata
184 abssrcdir=`cd $srcdir && pwd`
185 ln -s $abssrcdir/../testdata ../testdata
188 # Copy the .go files because io/utils_test.go expects a regular file.
189 case "x$gofiles" in
191 case "x$pkgfiles" in
193 for f in `cd $srcdir; ls *.go`; do
194 rm -f $f;
195 cp $srcdir/$f .
196 done
199 for f in $pkgfiles; do
200 if test -f $basedir/$f; then
201 b=`basename $f`
202 rm -f $b
203 cp $basedir/$f $b
204 elif test -f ../../$f; then
205 b=`basename $f`
206 rm -f $b
207 cp ../../$f $b
208 else
209 echo "file $f not found" 1>&2
210 exit 1
212 done
213 for f in `cd $srcdir; ls *_test.go`; do
214 rm -f $f
215 cp $srcdir/$f .
216 done
218 esac
221 for f in $gofiles; do
222 b=`basename $f`
223 rm -f $b
224 cp $basedir/$f $b
225 done
226 case "x$pkgfiles" in
228 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
229 rm -f $f
230 cp $srcdir/$f .
231 done
234 for f in $pkgfiles; do
235 if test -f $basedir/$f; then
236 b=`basename $f`
237 rm -f $b
238 cp $basedir/$f $b
239 elif test -f ../../$f; then
240 b=`basename $f`
241 rm -f $b
242 cp ../../$f $b
243 else
244 echo "file $f not found" 1>&2
245 exit 1
247 done
249 esac
251 esac
253 # Some tests expect the _obj directory created by the gc Makefiles.
254 mkdir _obj
256 # Some tests expect the _test directory created by the gc Makefiles.
257 mkdir _test
259 case "x$gofiles" in
261 gofiles=`ls *_test.go 2>/dev/null`
262 esac
264 case "x$gofiles" in
266 echo 'no test files found' 1>&2
267 exit 1
268 esac
270 # Run any commands given in sources, like
271 # // gotest: $GC foo.go
272 # to build any test-only dependencies.
273 holdGC="$GC"
274 GC="$GC -g -c -I ."
275 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
276 GC="$holdGC"
278 case "x$pkgfiles" in
280 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
283 for f in $pkgfiles; do
284 pkgbasefiles="$pkgbasefiles `basename $f`"
285 done
287 esac
289 case "x$pkgfiles" in
291 echo 'no source files found' 1>&2
292 exit 1
294 esac
296 # Split $gofiles into external gofiles (those in *_test packages)
297 # and internal ones (those in the main package).
298 for f in $gofiles; do
299 package=`grep '^package[ ]' $f | sed 1q`
300 case "$package" in
301 *_test)
302 xgofiles="$xgofiles $f"
305 ngofiles="$ngofiles $f"
307 esac
308 done
309 gofiles=$ngofiles
311 # External $O file
312 xofile=""
313 havex=false
314 if [ "x$xgofiles" != "x" ]; then
315 xofile="_xtest_.o"
316 havex=true
319 set -e
321 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
323 pkgpatharg=
324 xpkgpatharg=
325 prefixarg=
326 if test -n "$pkgpath"; then
327 pkgpatharg="-fgo-pkgpath=$pkgpath"
328 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
329 elif test -n "$prefix"; then
330 prefixarg="-fgo-prefix=$prefix"
333 if test "$trace" = "true"; then
334 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
336 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
338 if $havex; then
339 mkdir -p `dirname $package`
340 cp _gotest_.o `dirname $package`/lib`basename $package`.a
341 if test "$trace" = "true"; then
342 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
344 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
347 # They all compile; now generate the code to call them.
349 localname() {
350 # The package main has been renamed to __main__ when imported.
351 # Adjust its uses.
352 echo $1 | sed 's/^main\./__main__./'
356 text="T"
357 case "$GOARCH" in
358 ppc64) text="D" ;;
359 esac
361 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
363 # test functions are named TestFoo
364 # the grep -v eliminates methods and other special names
365 # that have multiple dots.
366 pattern='Test([^a-z].*)?'
367 # The -p option tells GNU nm not to sort.
368 # The -v option tells Solaris nm to sort by value.
369 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
370 if [ "x$tests" = x ]; then
371 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
372 exit 2
374 # benchmarks are named BenchmarkFoo.
375 pattern='Benchmark([^a-z].*)?'
376 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $test .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
378 # examples are named ExampleFoo
379 pattern='Example([^a-z].*)?'
380 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
382 # package spec
383 echo 'package main'
384 echo
385 # imports
386 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
387 echo 'import "./_gotest_"'
389 if $havex; then
390 echo 'import "./_xtest_"'
392 echo 'import "testing"'
393 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
394 # test array
395 echo
396 echo 'var tests = []testing.InternalTest {'
397 for i in $tests
399 j=$(localname $i)
400 echo ' {"'$i'", '$j'},'
401 done
402 echo '}'
404 # benchmark array
405 # The comment makes the multiline declaration
406 # gofmt-safe even when there are no benchmarks.
407 echo 'var benchmarks = []testing.InternalBenchmark{ //'
408 for i in $benchmarks
410 j=$(localname $i)
411 echo ' {"'$i'", '$j'},'
412 done
413 echo '}'
415 # examples array
416 echo 'var examples = []testing.InternalExample{ //'
417 # This doesn't work because we don't pick up the output.
418 #for i in $examples
420 # j=$(localname $i)
421 # echo ' {"'$i'", '$j', ""},'
422 #done
423 echo '}'
425 # body
426 echo \
428 var matchPat string
429 var matchRe *__regexp__.Regexp
431 func matchString(pat, str string) (result bool, err error) {
432 if matchRe == nil || matchPat != pat {
433 matchPat = pat
434 matchRe, err = __regexp__.Compile(matchPat)
435 if err != nil {
436 return
439 return matchRe.MatchString(str), nil
442 func main() {
443 testing.Main(matchString, tests, benchmarks, examples)
445 }>_testmain.go
447 case "x$dejagnu" in
448 xno)
449 if test "$trace" = "true"; then
450 echo ${GC} -g -c _testmain.go
452 ${GC} -g -c _testmain.go
454 if test "$trace" = "true"; then
455 echo ${GL} *.o ${GOLIBS}
457 ${GL} *.o ${GOLIBS}
459 if test "$trace" = "true"; then
460 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
462 ./a.out -test.short -test.timeout=${timeout}s "$@" &
463 pid=$!
464 (sleep `expr $timeout + 10`
465 echo > gotest-timeout
466 echo "timed out in gotest" 1>&2
467 kill -9 $pid) &
468 alarmpid=$!
469 wait $pid
470 status=$?
471 if ! test -f gotest-timeout; then
472 kill $alarmpid
474 exit $status
476 xyes)
477 rm -rf ../../testsuite/*.o
478 files=`echo *`
479 for f in $files; do
480 if test "$f" = "_obj" || test "$f" = "_test"; then
481 continue
483 rm -rf ../../testsuite/$f
484 if test -f $f; then
485 cp $f ../../testsuite/
486 else
487 ln -s ../$DIR/test/$f ../../testsuite/
489 done
490 cd ../../testsuite
491 rm -rf _obj _test
492 mkdir _obj _test
493 if test "$testname" != ""; then
494 GOTESTNAME="$testname"
495 export GOTESTNAME
497 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
498 # Useful when using make check-target-libgo
499 cat libgo.log >> libgo-all.log
500 cat libgo.sum >> libgo-all.sum
501 rm -rf $files
503 esac