addressbook tests: Run with private D-Bus session and environment.
[platform/upstream/evolution-data-server.git] / tests / tools / dbus-session.sh
1 #
2 # Helper functions to start your own D-Bus session.
3 #
4 # Refactored from with-session-bush.sh (from telepathy-glib).
5 #
6 # The canonical location of this program is the telepathy-glib tools/
7 # directory, please synchronize any changes with that copy.
8 #
9 # Copyright (C) 2007-2008,2011 Collabora Ltd. <http://www.collabora.co.uk/>
10 #
11 # Copying and distribution of this file, with or without modification,
12 # are permitted in any medium without royalty provided the copyright
13 # notice and this notice are preserved.
14
15
16 dbus_daemon_args="--print-address=5 --print-pid=6 --fork"
17 dbus_verbose=0
18 dbus_me=with-session-bus
19 dbus_sleep=0
20 dbus_with_session=""
21 dbus_config_file=""
22
23 # Params:
24 # verbose: 0 for off and 1 for on
25 #
26 dbus_init () {
27     exec 5> $dbus_me-$$.address
28     exec 6> $dbus_me-$$.pid
29     dbus_verbose=$1
30 }
31
32 dbus_usage ()
33 {
34   echo "usage: $me [options] -- program [program_options]" >&2
35   echo "Requires write access to the current directory." >&2
36   echo "" >&2
37   echo "If \$WITH_SESSION_BUS_FORK_DBUS_MONITOR is set, fork dbus-monitor" >&2
38   echo "with the arguments in \$WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT." >&2
39   echo "The output of dbus-monitor is saved in $me-<pid>.dbus-monitor-logs" >&2
40   exit 2
41 }
42
43 dbus_parse_args () {
44     while test "z$1" != "z--"; do
45         case "$1" in
46         --sleep=*)
47             sleep="$1"
48             dbus_sleep="${sleep#--sleep=}"
49             shift
50             ;;
51         --session)
52             dbus_with_session="--session"
53             shift
54             ;;
55         --config-file=*)
56             # FIXME: assumes config file doesn't contain any special characters
57             dbus_config_file="$1"
58             shift
59             ;;
60          *)
61             dbus_usage
62             ;;
63         esac
64     done
65 }
66
67 dbus_start () {
68     local args="$dbus_daemon_args $dbus_with_session $dbus_config_file "
69
70     if [ $dbus_verbose -gt 0 ] ; then
71         echo -n "dbus args $args "
72     fi
73
74     dbus-daemon $args
75
76     {
77         if [ $dbus_verbose -gt 0 ] ; then
78             echo -n "Temporary bus daemon is "; cat $dbus_me-$$.address;
79         fi
80     } >&2
81
82     {
83         if [ $dbus_verbose -gt 0 ] ; then
84             echo -n "Temporary bus daemon PID is "; head -n1 $dbus_me-$$.pid;
85         fi
86     } >&2
87
88     DBUS_SESSION_BUS_ADDRESS="`cat $dbus_me-$$.address`"
89     export DBUS_SESSION_BUS_ADDRESS
90
91     if [ -n "$WITH_SESSION_BUS_FORK_DBUS_MONITOR" ] ; then
92         if [ $dbus_verbose -gt 0 ] ; then
93             echo -n "Forking dbus-monitor " \
94                 "$WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT" >&2
95         fi
96         dbus-monitor $WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT \
97             > $dbus_me-$$.dbus-monitor-logs 2>&1 &
98     fi
99 }
100
101 #
102 # This should be called for INT, HUP and TERM signals
103 #
104 dbus_stop () {
105     pid=`head -n1 $dbus_me-$$.pid`
106     if test -n "$pid" ; then
107         if [ $dbus_verbose -gt 0 ] ; then
108             echo "Killing temporary bus daemon: $pid" >&2
109         fi
110         kill -INT "$pid"
111     fi
112     rm -f $dbus_me-$$.address
113     rm -f $dbus_me-$$.pid
114 }