OSDN Git Service

gotest: Remove race in use of ../testdata.
[pf3gnuchains/gcc-fork.git] / libgo / testsuite / gotest
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.
5
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.
10
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
16
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=${GOLIBS:-}
20 export GC GL GOLIBS
21
22 NM=${NM:-nm}
23
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 prefix=
34 dejagnu=no
35 GOARCH=""
36 timeout=240
37 testname=""
38 trace=false
39 while $loop; do
40         case "x$1" in
41         x--srcdir)
42                 srcdir=$2
43                 shift
44                 shift
45                 ;;
46         x--srcdir=*)
47                 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
48                 shift
49                 ;;
50         x--basedir)
51                 basedir=$2
52                 shift
53                 shift
54                 ;;
55         x--basedir=*)
56                 basedir=`echo $1 | sed -e 's/^--basedir=//'`
57                 shift
58                 ;;
59         x--prefix)
60                 prefix=$2
61                 shift
62                 shift
63                 ;;
64         x--prefix=*)
65                 prefix=`echo $1 | sed -e 's/^--prefix=//'`
66                 shift
67                 ;;
68         x--keep)
69                 keep=true
70                 shift
71                 ;;
72         x--pkgfiles)
73                 pkgfiles=$2
74                 shift
75                 shift
76                 ;;
77         x--pkgfiles=*)
78                 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
79                 shift
80                 ;;
81         x--dejagnu)
82                 dejagnu=$2
83                 shift
84                 shift
85                 ;;
86         x--dejagnu=*)
87                 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
88                 shift
89                 ;;
90         x--goarch)
91                 GOARCH=$2
92                 shift
93                 shift
94                 ;;
95         x--goarch=*)
96                 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
97                 shift
98                 ;;
99         x--timeout)
100                 timeout=$2
101                 shift
102                 shift
103                 ;;
104         x--timeout=*)
105                 timeout=`echo $1 | sed -e 's/^--timeout=//'`
106                 shift
107                 ;;
108         x--testname)
109                 testname=$2
110                 shift
111                 shift
112                 ;;
113         x--testname=*)
114                 testname=`echo $1 | sed -e 's/^--testname=//'`
115                 shift
116                 ;;
117         x--trace)
118                 trace=true
119                 shift
120                 ;;
121         x-*)
122                 loop=false
123                 ;;
124         x)
125                 loop=false
126                 ;;
127         *)
128                 gofiles="$gofiles $1"
129                 shift
130                 ;;
131         esac
132 done
133
134 DIR=gotest$$
135 rm -rf $DIR
136 mkdir $DIR
137
138 cd $DIR
139 mkdir test
140 cd test
141
142 if test $keep = false; then
143   trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
144 else
145   trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
146 fi
147
148 case "$srcdir" in
149         /*)
150                 ;;
151         *)
152                 srcdir="../../$srcdir"
153                 ;;
154 esac
155
156 SRCDIR=$srcdir
157 export SRCDIR
158
159 case "$basedir" in
160         /*)
161                 ;;
162         *)
163                 basedir="../../$basedir"
164                 ;;
165 esac
166
167 # Link all the files/directories in srcdir into our working directory,
168 # so that the tests do not have to refer to srcdir to find test data.
169 ln -s $srcdir/* .
170
171 # Some tests refer to a ../testdata directory.
172 if test -e $srcdir/../testdata; then
173   rm -f ../testdata
174   abssrcdir=`cd $srcdir && pwd`
175   ln -s $abssrcdir/../testdata ../testdata
176 fi
177
178 # Copy the .go files because io/utils_test.go expects a regular file.
179 case "x$gofiles" in
180 x)
181         case "x$pkgfiles" in
182         x)
183                 for f in `cd $srcdir; ls *.go`; do
184                     rm -f $f;
185                     cp $srcdir/$f .
186                 done
187                 ;;
188         *)
189                 for f in $pkgfiles; do
190                     if test -f $basedir/$f; then
191                         b=`basename $f`
192                         rm -f $b
193                         cp $basedir/$f $b
194                     elif test -f ../../$f; then
195                         b=`basename $f`
196                         rm -f $b
197                         cp ../../$f $b
198                     else
199                         echo "file $f not found" 1>&2
200                         exit 1
201                     fi
202                 done
203                 for f in `cd $srcdir; ls *_test.go`; do
204                     rm -f $f
205                     cp $srcdir/$f .
206                 done
207                 ;;
208         esac
209         ;;
210 *)
211         for f in $gofiles; do
212             b=`basename $f`
213             rm -f $b
214             cp $basedir/$f $b
215         done
216         case "x$pkgfiles" in
217         x)
218                 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
219                     rm -f $f
220                     cp $srcdir/$f .
221                 done
222                 ;;
223         *)
224                 for f in $pkgfiles; do
225                     if test -f $basedir/$f; then
226                         b=`basename $f`
227                         rm -f $b
228                         cp $basedir/$f $b
229                     elif test -f ../../$f; then
230                         b=`basename $f`
231                         rm -f $b
232                         cp ../../$f $b
233                     else
234                         echo "file $f not found" 1>&2
235                         exit 1
236                     fi
237                 done
238                 ;;
239         esac
240         ;;
241 esac
242
243 # Some tests expect the _obj directory created by the gc Makefiles.
244 mkdir _obj
245
246 # Some tests expect the _test directory created by the gc Makefiles.
247 mkdir _test
248
249 case "x$gofiles" in
250 x)
251         gofiles=`ls *_test.go 2>/dev/null`
252 esac
253
254 case "x$gofiles" in
255 x)
256         echo 'no test files found' 1>&2
257         exit 1
258 esac
259
260 # Run any commands given in sources, like
261 #   // gotest: $GC foo.go
262 # to build any test-only dependencies.
263 holdGC="$GC"
264 GC="$GC -g -c -I ."
265 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
266 GC="$holdGC"
267
268 case "x$pkgfiles" in
269 x)
270         pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
271         ;;
272 *)
273         for f in $pkgfiles; do
274             pkgbasefiles="$pkgbasefiles `basename $f`"
275         done
276         ;;
277 esac
278
279 case "x$pkgfiles" in
280 x)
281         echo 'no source files found' 1>&2
282         exit 1
283         ;;
284 esac
285
286 # Split $gofiles into external gofiles (those in *_test packages)
287 # and internal ones (those in the main package).
288 for f in $gofiles; do
289     package=`grep '^package[    ]' $f | sed 1q`
290     case "$package" in
291     *_test)
292         xgofiles="$xgofiles $f"
293         ;;
294     *)
295         ngofiles="$ngofiles $f"
296         ;;
297     esac
298 done
299 gofiles=$ngofiles
300
301 # External $O file
302 xofile=""
303 havex=false
304 if [ "x$xgofiles" != "x" ]; then
305         xofile="_xtest_.o"
306         havex=true
307 fi
308
309 set -e
310
311 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
312
313 prefixarg=
314 if test -n "$prefix"; then
315         prefixarg="-fgo-prefix=$prefix"
316 fi
317
318 if test "$trace" = "true"; then
319   echo $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
320 fi
321 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
322
323 if $havex; then
324         mkdir -p `dirname $package`
325         cp _gotest_.o `dirname $package`/lib`basename $package`.a
326         if test "$trace" = "true"; then
327             echo $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
328         fi
329         $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
330 fi
331
332 # They all compile; now generate the code to call them.
333
334 localname() {
335         # The package main has been renamed to __main__ when imported.
336         # Adjust its uses.
337         echo $1 | sed 's/^main\./__main__./'
338 }
339
340 {
341         text="T"
342         case "$GOARCH" in
343         ppc*) text="D" ;;
344         esac
345
346         # test functions are named TestFoo
347         # the grep -v eliminates methods and other special names
348         # that have multiple dots.
349         pattern='Test([^a-z].*)?'
350         # The -p option tells GNU nm not to sort.
351         # The -v option tells Solaris nm to sort by value.
352         tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
353         if [ "x$tests" = x ]; then
354                 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
355                 exit 2
356         fi
357         # benchmarks are named BenchmarkFoo.
358         pattern='Benchmark([^a-z].*)?'
359         benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $test .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
360
361         # examples are named ExampleFoo
362         pattern='Example([^a-z].*)?'
363         examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
364
365         # package spec
366         echo 'package main'
367         echo
368         # imports
369         if echo "$tests" | egrep -v '_test\.' >/dev/null; then
370                 echo 'import "./_gotest_"'
371         fi
372         if $havex; then
373                 echo 'import "./_xtest_"'
374         fi
375         echo 'import "testing"'
376         echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
377         # test array
378         echo
379         echo 'var tests = []testing.InternalTest {'
380         for i in $tests
381         do
382                 j=$(localname $i)
383                 echo '  {"'$i'", '$j'},'
384         done
385         echo '}'
386
387         # benchmark array
388         # The comment makes the multiline declaration
389         # gofmt-safe even when there are no benchmarks.
390         echo 'var benchmarks = []testing.InternalBenchmark{ //'
391         for i in $benchmarks
392         do
393                 j=$(localname $i)
394                 echo '  {"'$i'", '$j'},'
395         done
396         echo '}'
397
398         # examples array
399         echo 'var examples = []testing.InternalExample{ //'
400         # This doesn't work because we don't pick up the output.
401         #for i in $examples
402         #do
403         #       j=$(localname $i)
404         #       echo '  {"'$i'", '$j', ""},'
405         #done
406         echo '}'
407
408         # body
409         echo \
410 '
411 var matchPat string
412 var matchRe *__regexp__.Regexp
413
414 func matchString(pat, str string) (result bool, err error) {
415         if matchRe == nil || matchPat != pat {
416                 matchPat = pat
417                 matchRe, err = __regexp__.Compile(matchPat)
418                 if err != nil {
419                         return
420                 }
421         }
422         return matchRe.MatchString(str), nil
423 }
424
425 func main() {
426         testing.Main(matchString, tests, benchmarks, examples)
427 }'
428 }>_testmain.go
429
430 case "x$dejagnu" in
431 xno)
432         if test "$trace" = "true"; then
433             echo ${GC} -g -c _testmain.go
434         fi
435         ${GC} -g -c _testmain.go
436
437         if test "$trace" = "true"; then
438             echo ${GL} *.o ${GOLIBS}
439         fi
440         ${GL} *.o ${GOLIBS}
441
442         if test "$trace" = "true"; then
443             echo ./a.out -test.short -test.timeout=${timeout}s "$@"
444         fi
445         ./a.out -test.short -test.timeout=${timeout}s "$@" &
446         pid=$!
447         (sleep `expr $timeout + 10`
448             echo > gotest-timeout
449             echo "timed out in gotest" 1>&2
450             kill -9 $pid) &
451         alarmpid=$!
452         wait $pid
453         status=$?
454         if ! test -f gotest-timeout; then
455             kill $alarmpid
456         fi
457         exit $status
458         ;;
459 xyes)
460         rm -rf ../../testsuite/*.o
461         files=`echo *`
462         for f in $files; do
463                 if test "$f" = "_obj" || test "$f" = "_test"; then
464                         continue
465                 fi
466                 rm -rf ../../testsuite/$f
467                 if test -f $f; then
468                         cp $f ../../testsuite/
469                 else
470                         ln -s ../$DIR/test/$f ../../testsuite/
471                 fi
472         done
473         cd ../../testsuite
474         rm -rf _obj _test
475         mkdir _obj _test
476         if test "$testname" != ""; then
477             GOTESTNAME="$testname"
478             export GOTESTNAME
479         fi
480         $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
481         # Useful when using make check-target-libgo
482         cat libgo.log >> libgo-all.log
483         cat libgo.sum >> libgo-all.sum
484         rm -rf $files
485         ;;
486 esac