OSDN Git Service

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