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