OSDN Git Service

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