1# 2# Library code for git p4 tests 3# 4 5# p4 tests never use the top-level repo; always build/clone into 6# a subdirectory called "$git" 7TEST_NO_CREATE_REPO=NoThanks 8 9. ./test-lib.sh 10 11if ! test_have_prereq PYTHON; then 12 skip_all='skipping git p4 tests; python not available' 13 test_done 14fi 15( p4 -h && p4d -h ) >/dev/null 2>&1 || { 16 skip_all='skipping git p4 tests; no p4 or p4d' 17 test_done 18} 19 20# Try to pick a unique port: guess a large number, then hope 21# no more than one of each test is running. 22# 23# This does not handle the case where somebody else is running the 24# same tests and has chosen the same ports. 25testid=${this_test#t} 26git_p4_test_start=9800 27P4DPORT=$((10669 + ($testid - $git_p4_test_start))) 28 29P4PORT=localhost:$P4DPORT 30P4CLIENT=client 31P4EDITOR=: 32export P4PORT P4CLIENT P4EDITOR 33 34db="$TRASH_DIRECTORY/db" 35cli=$(test-path-utils real_path "$TRASH_DIRECTORY/cli") 36git="$TRASH_DIRECTORY/git" 37pidfile="$TRASH_DIRECTORY/p4d.pid" 38 39start_p4d() { 40 mkdir -p "$db" "$cli" "$git" && 41 rm -f "$pidfile" && 42 ( 43 cd "$db" && 44 { 45 p4d -q -p $P4DPORT & 46 echo $! >"$pidfile" 47 } 48 ) && 49 50 # This gives p4d a long time to start up, as it can be 51 # quite slow depending on the machine. Set this environment 52 # variable to something smaller to fail faster in, say, 53 # an automated test setup. If the p4d process dies, that 54 # will be caught with the "kill -0" check below. 55 i=${P4D_START_PATIENCE:-300} 56 pid=$(cat "$pidfile") 57 ready= 58 while test $i -gt 0 59 do 60 # succeed when p4 client commands start to work 61 if p4 info >/dev/null 2>&1 62 then 63 ready=true 64 break 65 fi 66 # fail if p4d died 67 kill -0 $pid 2>/dev/null || break 68 echo waiting for p4d to start 69 sleep 1 70 i=$(( $i - 1 )) 71 done 72 73 if test -z "$ready" 74 then 75 # p4d failed to start 76 return 1 77 fi 78 79 # build a client 80 client_view "//depot/... //client/..." && 81 82 return 0 83} 84 85kill_p4d() { 86 pid=$(cat "$pidfile") 87 # it had better exist for the first kill 88 kill $pid && 89 for i in 1 2 3 4 5 ; do 90 kill $pid >/dev/null 2>&1 || break 91 sleep 1 92 done && 93 # complain if it would not die 94 test_must_fail kill $pid >/dev/null 2>&1 && 95 rm -rf "$db" "$cli" "$pidfile" 96} 97 98cleanup_git() { 99 rm -rf "$git" && 100 mkdir "$git" 101} 102 103marshal_dump() { 104 what=$1 && 105 line=${2:-1} && 106 cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF && 107 import marshal 108 import sys 109 for i in range($line): 110 d = marshal.load(sys.stdin) 111 print d['$what'] 112 EOF 113 "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py" 114} 115 116# 117# Construct a client with this list of View lines 118# 119client_view() { 120 ( 121 cat <<-EOF && 122 Client: $P4CLIENT 123 Description: $P4CLIENT 124 Root: $cli 125 View: 126 EOF 127 printf "\t%s\n" "$@" 128 ) | p4 client -i 129}