Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 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 |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 18 | |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame^] | 19 | test_tristate GIT_TEST_GIT_DAEMON |
| 20 | if test "$GIT_TEST_GIT_DAEMON" = false |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 21 | then |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame^] | 22 | skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)" |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 23 | test_done |
| 24 | fi |
| 25 | |
| 26 | LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-'8121'} |
| 27 | |
| 28 | GIT_DAEMON_PID= |
| 29 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
| 30 | GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT |
| 31 | |
| 32 | start_git_daemon() { |
| 33 | if test -n "$GIT_DAEMON_PID" |
| 34 | then |
| 35 | error "start_git_daemon already called" |
| 36 | fi |
| 37 | |
| 38 | mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH" |
| 39 | |
| 40 | trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT |
| 41 | |
| 42 | say >&3 "Starting git daemon ..." |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 43 | mkfifo git_daemon_output |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 44 | git daemon --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ |
| 45 | --reuseaddr --verbose \ |
| 46 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
| 47 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 48 | >&3 2>git_daemon_output & |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 49 | GIT_DAEMON_PID=$! |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 50 | { |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 51 | read line <&7 |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 52 | echo >&4 "$line" |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 53 | cat <&7 >&4 & |
| 54 | } 7<git_daemon_output && |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 55 | |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 56 | # Check expected output |
| 57 | if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble" |
| 58 | then |
| 59 | kill "$GIT_DAEMON_PID" |
| 60 | wait "$GIT_DAEMON_PID" |
| 61 | trap 'die' EXIT |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame^] | 62 | test_skip_or_die $GIT_TEST_GIT_DAEMON \ |
| 63 | "git daemon failed to start" |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 64 | fi |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | stop_git_daemon() { |
| 68 | if test -z "$GIT_DAEMON_PID" |
| 69 | then |
| 70 | return |
| 71 | fi |
| 72 | |
| 73 | trap 'die' EXIT |
| 74 | |
| 75 | # kill git-daemon child of git |
| 76 | say >&3 "Stopping git daemon ..." |
| 77 | kill "$GIT_DAEMON_PID" |
| 78 | wait "$GIT_DAEMON_PID" >&3 2>&4 |
| 79 | ret=$? |
| 80 | # expect exit with status 143 = 128+15 for signal TERM=15 |
| 81 | if test $ret -ne 143 |
| 82 | then |
| 83 | error "git daemon exited with status: $ret" |
| 84 | fi |
| 85 | GIT_DAEMON_PID= |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 86 | rm -f git_daemon_output |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 87 | } |