Almost ready for 2.11.1
[git] / t / lib-git-daemon.sh
1 # Shell library to run git-daemon in tests.  Ends the test early if
2 # GIT_TEST_GIT_DAEMON is not set.
3 #
4 # Usage:
5 #
6 #       . ./test-lib.sh
7 #       . "$TEST_DIRECTORY"/lib-git-daemon.sh
8 #       start_git_daemon
9 #
10 #       test_expect_success '...' '
11 #               ...
12 #       '
13 #
14 #       test_expect_success ...
15 #
16 #       stop_git_daemon
17 #       test_done
18
19 test_tristate GIT_TEST_GIT_DAEMON
20 if test "$GIT_TEST_GIT_DAEMON" = false
21 then
22         skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
23         test_done
24 fi
25
26 if test_have_prereq !PIPE
27 then
28         test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29 fi
30
31 LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-${this_test#t}}
32
33 GIT_DAEMON_PID=
34 GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
35 GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT
36
37 start_git_daemon() {
38         if test -n "$GIT_DAEMON_PID"
39         then
40                 error "start_git_daemon already called"
41         fi
42
43         mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
44
45         trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
46
47         say >&3 "Starting git daemon ..."
48         mkfifo git_daemon_output
49         git daemon --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
50                 --reuseaddr --verbose \
51                 --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
52                 "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
53                 >&3 2>git_daemon_output &
54         GIT_DAEMON_PID=$!
55         {
56                 read line <&7
57                 echo >&4 "$line"
58                 cat <&7 >&4 &
59         } 7<git_daemon_output &&
60
61         # Check expected output
62         if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
63         then
64                 kill "$GIT_DAEMON_PID"
65                 wait "$GIT_DAEMON_PID"
66                 trap 'die' EXIT
67                 test_skip_or_die $GIT_TEST_GIT_DAEMON \
68                         "git daemon failed to start"
69         fi
70 }
71
72 stop_git_daemon() {
73         if test -z "$GIT_DAEMON_PID"
74         then
75                 return
76         fi
77
78         trap 'die' EXIT
79
80         # kill git-daemon child of git
81         say >&3 "Stopping git daemon ..."
82         kill "$GIT_DAEMON_PID"
83         wait "$GIT_DAEMON_PID" >&3 2>&4
84         ret=$?
85         if test_match_signal 15 $?
86         then
87                 error "git daemon exited with status: $ret"
88         fi
89         GIT_DAEMON_PID=
90         rm -f git_daemon_output
91 }