libcpp/ChangeLog.pph
[official-gcc.git] / libgo / testsuite / gotest
blob93db3462e8ac09ffb13fc901e95889b4e754bf09
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=
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 prefix=
34 dejagnu=no
35 while $loop; do
36 case "x$1" in
37 x--srcdir)
38 srcdir=$2
39 shift
40 shift
42 x--srcdir=*)
43 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
44 shift
46 x--basedir)
47 basedir=$2
48 shift
49 shift
51 x--basedir=*)
52 basedir=`echo $1 | sed -e 's/^--basedir=//'`
53 shift
55 x--prefix)
56 prefix=$2
57 shift
58 shift
60 x--prefix=*)
61 prefix=`echo $1 | sed -e 's/^--prefix=//'`
62 shift
64 x--keep)
65 keep=true
66 shift
68 x--pkgfiles)
69 pkgfiles=$2
70 shift
71 shift
73 x--pkgfiles=*)
74 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
75 shift
77 x--dejagnu)
78 dejagnu=$2
79 shift
80 shift
82 x--dejagnu=*)
83 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
84 shift
86 x-*)
87 loop=false
90 loop=false
93 gofiles="$gofiles $1"
94 shift
96 esac
97 done
99 DIR=gotest$$
100 rm -rf $DIR
101 mkdir $DIR
103 cd $DIR
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
111 case "$srcdir" in
115 srcdir="../$srcdir"
117 esac
119 SRCDIR=$srcdir
120 export SRCDIR
122 case "$basedir" in
126 basedir="../$basedir"
128 esac
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/* .
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
141 # Copy the .go files because io/utils_test.go expects a regular file.
142 case "x$gofiles" in
144 case "x$pkgfiles" in
146 for f in `cd $srcdir; ls *.go`; do
147 rm -f $f;
148 cp $srcdir/$f .
149 done
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
165 done
166 for f in `cd $srcdir; ls *_test.go`; do
167 rm -f $f
168 cp $srcdir/$f .
169 done
171 esac
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
181 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
182 rm -f $f
183 cp $srcdir/$f .
184 done
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
200 done
202 esac
204 esac
206 # Some tests expect the _obj directory created by the gc Makefiles.
207 mkdir _obj
209 # Some tests expect the _test directory created by the gc Makefiles.
210 mkdir _test
212 case "x$gofiles" in
214 gofiles=$(echo -n $(ls *_test.go 2>/dev/null))
215 esac
217 case "x$gofiles" in
219 echo 'no test files found' 1>&2
220 exit 1
221 esac
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"
231 case "x$pkgfiles" in
233 pkgbasefiles=$(echo -n $(ls *.go | grep -v _test.go 2>/dev/null))
236 for f in $pkgfiles; do
237 pkgbasefiles="$pkgbasefiles `basename $f`"
238 done
240 esac
242 case "x$pkgfiles" in
244 echo 'no source files found' 1>&2
245 exit 1
247 esac
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/:.*//'))
254 # External $O file
255 xofile=""
256 havex=false
257 if [ "x$xgofiles" != "x" ]; then
258 xofile="_xtest_.o"
259 havex=true
262 set -e
264 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
266 prefixarg=
267 if test -n "$prefix"; then
268 prefixarg="-fgo-prefix=$prefix"
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
278 # They all compile; now generate the code to call them.
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
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_"'
299 if $havex; then
300 echo 'import "./_xtest_"'
302 if [ $package != "testing" ]; then
303 echo 'import "testing"'
304 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
306 # test array
307 echo
308 echo 'var tests = []testing.InternalTest {'
309 for i in $tests
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
321 case "x$dejagnu" in
322 xno)
323 ${GC} -g -c _testmain.go
324 ${GL} *.o ${GOLIBS}
325 ./a.out "$@"
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
334 rm -rf ../testsuite/$f
335 if test -f $f; then
336 cp $f ../testsuite/
337 else
338 ln -s ../$DIR/$f ../testsuite/
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
350 esac