Merge branch 'rr/test-make-sure-we-have-git' into maint
[git.git] / t / lib-git-p4.sh
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"
7 TEST_NO_CREATE_REPO=NoThanks
8
9 . ./test-lib.sh
10
11 if ! test_have_prereq PYTHON; then
12         skip_all='skipping git p4 tests; python not available'
13         test_done
14 fi
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.
25 testid=${this_test#t}
26 git_p4_test_start=9800
27 P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
28
29 export P4PORT=localhost:$P4DPORT
30 export P4CLIENT=client
31 export P4EDITOR=:
32
33 db="$TRASH_DIRECTORY/db"
34 cli=$(test-path-utils real_path "$TRASH_DIRECTORY/cli")
35 git="$TRASH_DIRECTORY/git"
36 pidfile="$TRASH_DIRECTORY/p4d.pid"
37
38 start_p4d() {
39         mkdir -p "$db" "$cli" "$git" &&
40         rm -f "$pidfile" &&
41         (
42                 p4d -q -r "$db" -p $P4DPORT &
43                 echo $! >"$pidfile"
44         ) &&
45
46         # This gives p4d a long time to start up, as it can be
47         # quite slow depending on the machine.  Set this environment
48         # variable to something smaller to fail faster in, say,
49         # an automated test setup.  If the p4d process dies, that
50         # will be caught with the "kill -0" check below.
51         i=${P4D_START_PATIENCE:-300}
52         pid=$(cat "$pidfile")
53         ready=
54         while test $i -gt 0
55         do
56                 # succeed when p4 client commands start to work
57                 if p4 info >/dev/null 2>&1
58                 then
59                         ready=true
60                         break
61                 fi
62                 # fail if p4d died
63                 kill -0 $pid 2>/dev/null || break
64                 echo waiting for p4d to start
65                 sleep 1
66                 i=$(( $i - 1 ))
67         done
68
69         if test -z "$ready"
70         then
71                 # p4d failed to start
72                 return 1
73         fi
74
75         # build a client
76         (
77                 cd "$cli" &&
78                 p4 client -i <<-EOF
79                 Client: client
80                 Description: client
81                 Root: $cli
82                 View: //depot/... //client/...
83                 EOF
84         )
85         return 0
86 }
87
88 kill_p4d() {
89         pid=$(cat "$pidfile")
90         # it had better exist for the first kill
91         kill $pid &&
92         for i in 1 2 3 4 5 ; do
93                 kill $pid >/dev/null 2>&1 || break
94                 sleep 1
95         done &&
96         # complain if it would not die
97         test_must_fail kill $pid >/dev/null 2>&1 &&
98         rm -rf "$db" "$cli" "$pidfile"
99 }
100
101 cleanup_git() {
102         rm -rf "$git" &&
103         mkdir "$git"
104 }
105
106 marshal_dump() {
107         what=$1 &&
108         line=${2:-1} &&
109         cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
110         import marshal
111         import sys
112         for i in range($line):
113             d = marshal.load(sys.stdin)
114         print d['$what']
115         EOF
116         "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
117 }