OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[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         # test functions are named TestFoo
281         # the grep -v eliminates methods and other special names
282         # that have multiple dots.
283         pattern='Test([^a-z].*)?'
284         # The -p option tells GNU nm not to sort.
285         # The -v option tells Solaris nm to sort by value.
286         tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
287         if [ "x$tests" = x ]; then
288                 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
289                 exit 2
290         fi
291
292         # package spec
293         echo 'package main'
294         echo
295         # imports
296         if echo "$tests" | egrep -v '_test\.' >/dev/null; then
297                 echo 'import "./_gotest_"'
298         fi
299         if $havex; then
300                 echo 'import "./_xtest_"'
301         fi
302         if [ $package != "testing" ]; then
303                 echo 'import "testing"'
304                 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
305         fi
306         # test array
307         echo
308         echo 'var tests = []testing.InternalTest {'
309         for i in $tests
310         do
311                 echo '  { "'$i'", '$i' },'
312         done
313         echo '}'
314         # body
315         echo
316         echo 'func main() {'
317         echo '  testing.Main(__regexp__.MatchString, tests)'
318         echo '}'
319 }>_testmain.go
320
321 case "x$dejagnu" in
322 xno)
323         ${GC} -g -c _testmain.go
324         ${GL} *.o ${GOLIBS}
325         ./a.out "$@"
326         ;;
327 xyes)
328         rm -rf ../testsuite/*.o
329         files=`echo *`
330         for f in $files; do
331                 if test "$f" = "_obj" || test "$f" = "_test"; then
332                         continue
333                 fi
334                 rm -rf ../testsuite/$f
335                 if test -f $f; then
336                         cp $f ../testsuite/
337                 else
338                         ln -s ../$DIR/$f ../testsuite/
339                 fi
340         done
341         cd ../testsuite
342         rm -rf _obj _test
343         mkdir _obj _test
344         $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
345         # Useful when using make check-target-libgo
346         cat libgo.log >> libgo-all.log
347         cat libgo.sum >> libgo-all.sum
348         rm -rf $files
349         ;;
350 esac