9612cccefb1c9dd37e61857a88ea92d86ddf854d
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
19test_tristate GIT_TEST_GIT_DAEMON
20if test "$GIT_TEST_GIT_DAEMON" = false
21then
22 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
23 test_done
24fi
25
26if test_have_prereq !PIPE
27then
28 test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29fi
30
31LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-${this_test#t}}
32
33GIT_DAEMON_PID=
34GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
35GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT
36
37start_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 ${LIB_GIT_DAEMON_COMMAND:-git daemon} \
50 --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
51 --reuseaddr --verbose \
52 --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
53 "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
54 >&3 2>git_daemon_output &
55 GIT_DAEMON_PID=$!
56 >daemon.log
57 {
58 read -r line <&7
59 printf "%s\n" "$line"
60 printf >&4 "%s\n" "$line"
61 (
62 while read -r line <&7
63 do
64 printf "%s\n" "$line"
65 printf >&4 "%s\n" "$line"
66 done
67 ) &
68 } 7<git_daemon_output >>"$TRASH_DIRECTORY/daemon.log" &&
69
70 # Check expected output
71 if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
72 then
73 kill "$GIT_DAEMON_PID"
74 wait "$GIT_DAEMON_PID"
75 trap 'die' EXIT
76 test_skip_or_die $GIT_TEST_GIT_DAEMON \
77 "git daemon failed to start"
78 fi
79}
80
81stop_git_daemon() {
82 if test -z "$GIT_DAEMON_PID"
83 then
84 return
85 fi
86
87 trap 'die' EXIT
88
89 # kill git-daemon child of git
90 say >&3 "Stopping git daemon ..."
91 kill "$GIT_DAEMON_PID"
92 wait "$GIT_DAEMON_PID" >&3 2>&4
93 ret=$?
94 if test_match_signal 15 $?
95 then
96 error "git daemon exited with status: $ret"
97 fi
98 GIT_DAEMON_PID=
99 rm -f git_daemon_output
100}