t / lib-git-daemon.shon commit Merge branch 'jk/t5531-prepare-to-default-to-non-matching' (e8a2f5f)
   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
  19if test -z "$GIT_TEST_GIT_DAEMON"
  20then
  21        skip_all="git-daemon testing disabled (define GIT_TEST_GIT_DAEMON to enable)"
  22        test_done
  23fi
  24
  25LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-'8121'}
  26
  27GIT_DAEMON_PID=
  28GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
  29GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT
  30
  31start_git_daemon() {
  32        if test -n "$GIT_DAEMON_PID"
  33        then
  34                error "start_git_daemon already called"
  35        fi
  36
  37        mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
  38
  39        trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
  40
  41        say >&3 "Starting git daemon ..."
  42        mkfifo git_daemon_output
  43        git daemon --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
  44                --reuseaddr --verbose \
  45                --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
  46                "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
  47                >&3 2>git_daemon_output &
  48        GIT_DAEMON_PID=$!
  49        {
  50                read line <&7
  51                echo >&4 "$line"
  52                cat <&7 >&4 &
  53        } 7<git_daemon_output &&
  54
  55        # Check expected output
  56        if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
  57        then
  58                kill "$GIT_DAEMON_PID"
  59                wait "$GIT_DAEMON_PID"
  60                trap 'die' EXIT
  61                error "git daemon failed to start"
  62        fi
  63}
  64
  65stop_git_daemon() {
  66        if test -z "$GIT_DAEMON_PID"
  67        then
  68                return
  69        fi
  70
  71        trap 'die' EXIT
  72
  73        # kill git-daemon child of git
  74        say >&3 "Stopping git daemon ..."
  75        kill "$GIT_DAEMON_PID"
  76        wait "$GIT_DAEMON_PID" >&3 2>&4
  77        ret=$?
  78        # expect exit with status 143 = 128+15 for signal TERM=15
  79        if test $ret -ne 143
  80        then
  81                error "git daemon exited with status: $ret"
  82        fi
  83        GIT_DAEMON_PID=
  84        rm -f git_daemon_output
  85}