PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/t/lib-git-p4.sh

https://gitlab.com/oyvholm/git
Shell | 246 lines | 171 code | 29 blank | 46 comment | 20 complexity | 4a7e31b4df8bfef237d37db94313fc5e MD5 | raw file
  1. #
  2. # Library code for git p4 tests
  3. #
  4. # p4 tests never use the top-level repo; always build/clone into
  5. # a subdirectory called "$git"
  6. TEST_NO_CREATE_REPO=NoThanks
  7. # Some operations require multiple attempts to be successful. Define
  8. # here the maximal retry timeout in seconds.
  9. RETRY_TIMEOUT=60
  10. # Sometimes p4d seems to hang. Terminate the p4d process automatically after
  11. # the defined timeout in seconds.
  12. P4D_TIMEOUT=300
  13. . ./test-lib.sh
  14. if ! test_have_prereq PYTHON
  15. then
  16. skip_all='skipping git p4 tests; python not available'
  17. test_done
  18. fi
  19. ( p4 -h && p4d -h ) >/dev/null 2>&1 || {
  20. skip_all='skipping git p4 tests; no p4 or p4d'
  21. test_done
  22. }
  23. # On cygwin, the NT version of Perforce can be used. When giving
  24. # it paths, either on the command-line or in client specifications,
  25. # be sure to use the native windows form.
  26. #
  27. # Older versions of perforce were available compiled natively for
  28. # cygwin. Those do not accept native windows paths, so make sure
  29. # not to convert for them.
  30. native_path () {
  31. path="$1" &&
  32. if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
  33. then
  34. path=$(cygpath --windows "$path")
  35. else
  36. path=$(test-path-utils real_path "$path")
  37. fi &&
  38. echo "$path"
  39. }
  40. # On Solaris the 'date +%s' function is not supported and therefore we
  41. # need this replacement.
  42. # Attention: This function is not safe again against time offset updates
  43. # at runtime (e.g. via NTP). The 'clock_gettime(CLOCK_MONOTONIC)'
  44. # function could fix that but it is not in Python until 3.3.
  45. time_in_seconds () {
  46. (cd / && "$PYTHON_PATH" -c 'import time; print(int(time.time()))')
  47. }
  48. # Try to pick a unique port: guess a large number, then hope
  49. # no more than one of each test is running.
  50. #
  51. # This does not handle the case where somebody else is running the
  52. # same tests and has chosen the same ports.
  53. testid=${this_test#t}
  54. git_p4_test_start=9800
  55. P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
  56. P4PORT=localhost:$P4DPORT
  57. P4CLIENT=client
  58. P4USER=author
  59. P4EDITOR=true
  60. unset P4CHARSET
  61. export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
  62. db="$TRASH_DIRECTORY/db"
  63. cli="$TRASH_DIRECTORY/cli"
  64. git="$TRASH_DIRECTORY/git"
  65. pidfile="$TRASH_DIRECTORY/p4d.pid"
  66. # Sometimes "prove" seems to hang on exit because p4d is still running
  67. cleanup () {
  68. if test -f "$pidfile"
  69. then
  70. kill -9 $(cat "$pidfile") 2>/dev/null && exit 255
  71. fi
  72. }
  73. trap cleanup EXIT
  74. # git p4 submit generates a temp file, which will
  75. # not get cleaned up if the submission fails. Don't
  76. # clutter up /tmp on the test machine.
  77. TMPDIR="$TRASH_DIRECTORY"
  78. export TMPDIR
  79. start_p4d () {
  80. mkdir -p "$db" "$cli" "$git" &&
  81. rm -f "$pidfile" &&
  82. (
  83. cd "$db" &&
  84. {
  85. p4d -q -p $P4DPORT "$@" &
  86. echo $! >"$pidfile"
  87. }
  88. ) &&
  89. # This gives p4d a long time to start up, as it can be
  90. # quite slow depending on the machine. Set this environment
  91. # variable to something smaller to fail faster in, say,
  92. # an automated test setup. If the p4d process dies, that
  93. # will be caught with the "kill -0" check below.
  94. i=${P4D_START_PATIENCE:-300}
  95. pid=$(cat "$pidfile")
  96. timeout=$(($(time_in_seconds) + $P4D_TIMEOUT))
  97. while true
  98. do
  99. if test $(time_in_seconds) -gt $timeout
  100. then
  101. kill -9 $pid
  102. exit 1
  103. fi
  104. sleep 1
  105. done &
  106. watchdog_pid=$!
  107. ready=
  108. while test $i -gt 0
  109. do
  110. # succeed when p4 client commands start to work
  111. if p4 info >/dev/null 2>&1
  112. then
  113. ready=true
  114. break
  115. fi
  116. # fail if p4d died
  117. kill -0 $pid 2>/dev/null || break
  118. echo waiting for p4d to start
  119. sleep 1
  120. i=$(( $i - 1 ))
  121. done
  122. if test -z "$ready"
  123. then
  124. # p4d failed to start
  125. return 1
  126. fi
  127. # build a p4 user so author@example.com has an entry
  128. p4_add_user author
  129. # build a client
  130. client_view "//depot/... //client/..." &&
  131. return 0
  132. }
  133. p4_add_user () {
  134. name=$1 &&
  135. p4 user -f -i <<-EOF
  136. User: $name
  137. Email: $name@example.com
  138. FullName: Dr. $name
  139. EOF
  140. }
  141. p4_add_job () {
  142. p4 job -f -i <<-EOF
  143. Job: $1
  144. Status: open
  145. User: dummy
  146. Description:
  147. EOF
  148. }
  149. retry_until_success () {
  150. timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
  151. until "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
  152. do
  153. sleep 1
  154. done
  155. }
  156. retry_until_fail () {
  157. timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
  158. until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
  159. do
  160. sleep 1
  161. done
  162. }
  163. kill_p4d () {
  164. pid=$(cat "$pidfile")
  165. retry_until_fail kill $pid
  166. retry_until_fail kill -9 $pid
  167. # complain if it would not die
  168. test_must_fail kill $pid >/dev/null 2>&1 &&
  169. rm -rf "$db" "$cli" "$pidfile" &&
  170. retry_until_fail kill -9 $watchdog_pid
  171. }
  172. cleanup_git () {
  173. retry_until_success rm -r "$git"
  174. test_must_fail test -d "$git" &&
  175. retry_until_success mkdir "$git"
  176. }
  177. marshal_dump () {
  178. what=$1 &&
  179. line=${2:-1} &&
  180. cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
  181. import marshal
  182. import sys
  183. instream = getattr(sys.stdin, 'buffer', sys.stdin)
  184. for i in range($line):
  185. d = marshal.load(instream)
  186. print(d[b'$what'].decode('utf-8'))
  187. EOF
  188. "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
  189. }
  190. #
  191. # Construct a client with this list of View lines
  192. #
  193. client_view () {
  194. (
  195. cat <<-EOF &&
  196. Client: $P4CLIENT
  197. Description: $P4CLIENT
  198. Root: $cli
  199. AltRoots: $(native_path "$cli")
  200. LineEnd: unix
  201. View:
  202. EOF
  203. printf "\t%s\n" "$@"
  204. ) | p4 client -i
  205. }
  206. is_cli_file_writeable () {
  207. # cygwin version of p4 does not set read-only attr,
  208. # will be marked 444 but -w is true
  209. file="$1" &&
  210. if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
  211. then
  212. stat=$(stat --format=%a "$file") &&
  213. test $stat = 644
  214. else
  215. test -w "$file"
  216. fi
  217. }