1#!/usr/bin/env bash
2# testrunner.sh
3#
4# Copyright (C) 2006-2008  Jürg Billeter
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19#
20# Author:
21# 	Jürg Billeter <j@bitron.ch>
22
23set -e
24
25export G_DEBUG=fatal-warnings
26
27EXTRA_ENVIRONMENT_FILE=tests-extra-environment.sh
28
29testfile=$1
30testdirname="$(dirname $testfile)"
31testfile=${testfile#$srcdir/}
32testpath=${testfile/.*/}
33testpath=${testpath//\//_}
34testpath=${testpath//-/_}
35if test -f $testdirname/$EXTRA_ENVIRONMENT_FILE; then
36	source $testdirname/$EXTRA_ENVIRONMENT_FILE
37fi
38
39vapidir=$abs_top_srcdir/vapi
40run_prefix=""
41
42VALAC=$abs_top_builddir/compiler/valac$EXEEXT
43VALAFLAGS="$VALAFLAGS \
44	--vapidir $vapidir \
45	--enable-checking \
46	--disable-warnings \
47	--save-temps \
48	--cc $CC \
49	-X -g \
50	-X -O0 \
51	-X -pipe \
52	-X -lm \
53	-X -DGETTEXT_PACKAGE=\"valac\""
54VAPIGEN=$abs_top_builddir/vapigen/vapigen$EXEEXT
55VAPIGENFLAGS="--vapidir $vapidir"
56
57# Incorporate the TEST_CFLAGS.
58for cflag in ${TEST_CFLAGS}; do
59    VALAFLAGS="${VALAFLAGS} -X ${cflag}"
60done
61
62# Incorporate the user's CFLAGS. Matters if the user decided to insert
63# -m32 in CFLAGS, for example.
64for cflag in ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}; do
65	if [[ ! $cflag =~ ^\-O[0-9]$ ]]; then
66		VALAFLAGS="${VALAFLAGS} -X ${cflag}"
67	fi
68done
69
70function testheader() {
71	if [ "$1" = "Packages:" ]; then
72		shift
73		PACKAGES="$PACKAGES $@"
74	elif [ "$*" = "Invalid Code" ]; then
75		INVALIDCODE=1
76		INHEADER=0
77		SOURCEFILE=${testpath}_invalid.vala
78	elif [ "$1" = "D-Bus" ]; then
79		DBUSTEST=1
80		run_prefix="dbus-run-session -- $run_prefix"
81	elif [ "$1" = "GIR" ]; then
82		GIRTEST=1
83	fi
84}
85
86function sourceheader() {
87	if [ "$1" = "Program:" ]; then
88		if [ "$2" = "server" ]; then
89			ISSERVER=1
90		fi
91		ns=$testpath/$2
92		ns=${ns//\//_}
93		ns=${ns//-/_}
94		SOURCEFILE=$ns.vala
95		SOURCEFILES="$SOURCEFILES $SOURCEFILE"
96	elif [ $GIRTEST -eq 1 ]; then
97		if [ "$1" = "Input:" ]; then
98			ns=$testpath
99			SOURCEFILE=$ns.gir
100			cat <<EOF > $SOURCEFILE
101<?xml version="1.0"?>
102<repository version="1.2"
103			xmlns="http://www.gtk.org/introspection/core/1.0"
104			xmlns:c="http://www.gtk.org/introspection/c/1.0"
105			xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
106  <include name="GLib" version="2.0"/>
107  <include name="GObject" version="2.0"/>
108  <include name="Gio" version="2.0"/>
109  <c:include name="test.h"/>
110  <namespace name="Test"
111			 version="1.2"
112			 c:identifier-prefixes="Test"
113			 c:symbol-prefixes="test">
114EOF
115		elif [ "$1" = "Output:" ]; then
116			SOURCEFILE=$testpath.vapi.ref
117		fi
118	fi
119}
120
121function sourceend() {
122	if [ $INVALIDCODE -eq 1 ]; then
123		PACKAGEFLAGS=$([ -z "$PACKAGES" ] || echo $PACKAGES | xargs -n 1 echo -n " --pkg")
124		echo '' > prepare
125		echo "$VALAC $VALAFLAGS $PACKAGEFLAGS -C $SOURCEFILE" > check
126		echo "RET=\$?" >> check
127		echo "if [ \$RET -ne 1 ]; then exit 1; fi" >> check
128		echo "exit 0" >> check
129	elif [ $GIRTEST -eq 1 ]; then
130		if [ $PART -eq 1 ]; then
131			echo "  </namespace>" >> $SOURCEFILE
132			echo "</repository>" >> $SOURCEFILE
133		fi
134		PACKAGEFLAGS=$([ -z "$PACKAGES" ] || echo $PACKAGES | xargs -n 1 echo -n " --pkg")
135		echo "$VAPIGEN $VAPIGENFLAGS $PACKAGEFLAGS --library $ns $ns.gir && tail -n +5 $ns.vapi|sed '\$d'|diff -wu $ns.vapi.ref -" > check
136	else
137		PACKAGEFLAGS=$([ -z "$PACKAGES" ] || echo $PACKAGES | xargs -n 1 echo -n " --pkg")
138		echo "$VALAC $VALAFLAGS $PACKAGEFLAGS -o $ns$EXEEXT $SOURCEFILE" >> prepare
139		if [ $DBUSTEST -eq 1 ]; then
140			if [ $ISSERVER -eq 1 ]; then
141				echo "./$ns$EXEEXT" >> check
142			fi
143		else
144			echo "./$ns$EXEEXT" >> check
145		fi
146	fi
147}
148
149PACKAGES=${PACKAGES:-gio-2.0}
150
151unset SOURCEFILE
152
153testdir=_test.$$
154rm -rf ./$testdir
155mkdir $testdir
156cd $testdir
157
158case "$testfile" in
159*.gs)
160	SOURCEFILE=$testpath.gs
161	;&
162*.vala)
163	SOURCEFILE=${SOURCEFILE:-$testpath.vala}
164	cat "$abs_srcdir/$testfile" > ./$SOURCEFILE
165	PACKAGEFLAGS=$([ -z "$PACKAGES" ] || echo $PACKAGES | xargs -n 1 echo -n " --pkg")
166	$VALAC $VALAFLAGS $PACKAGEFLAGS -o $testpath$EXEEXT $SOURCEFILE
167	./$testpath$EXEEXT
168	;;
169*.test)
170	rm -f prepare check
171	echo 'set -e' >> prepare
172	run_prefix=""
173	PART=0
174	INHEADER=1
175	INVALIDCODE=0
176	GIRTEST=0
177	DBUSTEST=0
178	ISSERVER=0
179	while IFS="" read -r line; do
180		if [ $PART -eq 0 ]; then
181			if [ -n "$line" ]; then
182				testheader $line
183			else
184				PART=1
185			fi
186		else
187			if [ $INHEADER -eq 1 ]; then
188				if [ -n "$line" ]; then
189					sourceheader $line
190				else
191					INHEADER=0
192				fi
193			else
194				if echo "$line" | grep -q "^[A-Za-z]\+:"; then
195					sourceend
196					PART=$(($PART + 1))
197					INHEADER=1
198					sourceheader $line
199				else
200					echo "$line" >> $SOURCEFILE
201				fi
202			fi
203		fi
204	done < "$abs_srcdir/$testfile"
205	sourceend
206	cat prepare check > $ns.check
207	$run_prefix bash $ns.check
208	;;
209esac
210
211cd ..
212rm -rf ./$testdir
213