]> git.ipfire.org Git - thirdparty/git.git/blame - t/lib-git-p4.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / lib-git-p4.sh
CommitLineData
fc002330 1#
6ab1d76c 2# Library code for git p4 tests
fc002330
PW
3#
4
c88015a4
PW
5# p4 tests never use the top-level repo; always build/clone into
6# a subdirectory called "$git"
7TEST_NO_CREATE_REPO=NoThanks
8
23aee419
LS
9# Some operations require multiple attempts to be successful. Define
10# here the maximal retry timeout in seconds.
11RETRY_TIMEOUT=60
12
842addef
LS
13# Sometimes p4d seems to hang. Terminate the p4d process automatically after
14# the defined timeout in seconds.
15P4D_TIMEOUT=300
16
fc002330
PW
17. ./test-lib.sh
18
cfa96496
PW
19if ! test_have_prereq PYTHON
20then
6ab1d76c 21 skip_all='skipping git p4 tests; python not available'
fc002330
PW
22 test_done
23fi
24( p4 -h && p4d -h ) >/dev/null 2>&1 || {
6ab1d76c 25 skip_all='skipping git p4 tests; no p4 or p4d'
fc002330
PW
26 test_done
27}
28
cfa96496
PW
29# On cygwin, the NT version of Perforce can be used. When giving
30# it paths, either on the command-line or in client specifications,
31# be sure to use the native windows form.
32#
33# Older versions of perforce were available compiled natively for
34# cygwin. Those do not accept native windows paths, so make sure
35# not to convert for them.
a98772c6 36native_path () {
cfa96496
PW
37 path="$1" &&
38 if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
39 then
40 path=$(cygpath --windows "$path")
41 else
b8d5cf4f 42 path=$(test-tool path-utils real_path "$path")
cfa96496
PW
43 fi &&
44 echo "$path"
45}
46
fa840581 47test_set_port P4DPORT
fc002330 48
8c291350
PW
49P4PORT=localhost:$P4DPORT
50P4CLIENT=client
0055b56e 51P4USER=author
0cf1b72a 52P4EDITOR=true
79946031 53unset P4CHARSET
0055b56e 54export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
fc002330
PW
55
56db="$TRASH_DIRECTORY/db"
cfa96496 57cli="$TRASH_DIRECTORY/cli"
fc002330
PW
58git="$TRASH_DIRECTORY/git"
59pidfile="$TRASH_DIRECTORY/p4d.pid"
60
07353d90
SG
61stop_p4d_and_watchdog () {
62 kill -9 $p4d_pid $watchdog_pid
dfe90e8b 63}
dfe90e8b 64
0cf1b72a
PW
65# git p4 submit generates a temp file, which will
66# not get cleaned up if the submission fails. Don't
67# clutter up /tmp on the test machine.
68TMPDIR="$TRASH_DIRECTORY"
69export TMPDIR
70
99e37c25 71registered_stop_p4d_atexit_handler=
a98772c6 72start_p4d () {
99e37c25
JS
73 # One of the test scripts stops and then re-starts p4d.
74 # Don't register and then run the same atexit handlers several times.
75 if test -z "$registered_stop_p4d_atexit_handler"
76 then
07353d90 77 test_atexit 'stop_p4d_and_watchdog'
99e37c25
JS
78 registered_stop_p4d_atexit_handler=AlreadyDone
79 fi
80
fc002330 81 mkdir -p "$db" "$cli" "$git" &&
f89f35a9 82 rm -f "$pidfile" &&
fc002330 83 (
6492a104
PW
84 cd "$db" &&
85 {
e80967b2 86 p4d -q -p $P4DPORT "$@" &
6492a104
PW
87 echo $! >"$pidfile"
88 }
fc002330 89 ) &&
07353d90 90 p4d_pid=$(cat "$pidfile")
f89f35a9
PW
91
92 # This gives p4d a long time to start up, as it can be
93 # quite slow depending on the machine. Set this environment
94 # variable to something smaller to fail faster in, say,
95 # an automated test setup. If the p4d process dies, that
96 # will be caught with the "kill -0" check below.
97 i=${P4D_START_PATIENCE:-300}
842addef 98
0e67c329 99 nr_tries_left=$P4D_TIMEOUT
842addef
LS
100 while true
101 do
0e67c329 102 if test $nr_tries_left -eq 0
842addef 103 then
07353d90 104 kill -9 $p4d_pid
842addef
LS
105 exit 1
106 fi
107 sleep 1
0e67c329 108 nr_tries_left=$(($nr_tries_left - 1))
3fae7ad9 109 done 2>/dev/null 4>&2 &
842addef
LS
110 watchdog_pid=$!
111
f89f35a9
PW
112 ready=
113 while test $i -gt 0
114 do
115 # succeed when p4 client commands start to work
116 if p4 info >/dev/null 2>&1
117 then
118 ready=true
119 break
120 fi
121 # fail if p4d died
07353d90 122 kill -0 $p4d_pid 2>/dev/null || break
f89f35a9 123 echo waiting for p4d to start
fc002330 124 sleep 1
f89f35a9
PW
125 i=$(( $i - 1 ))
126 done
127
128 if test -z "$ready"
129 then
130 # p4d failed to start
131 return 1
132 fi
133
0055b56e
PW
134 # build a p4 user so author@example.com has an entry
135 p4_add_user author
136
f89f35a9 137 # build a client
daa38f4a
PW
138 client_view "//depot/... //client/..." &&
139
f89f35a9 140 return 0
fc002330
PW
141}
142
a98772c6 143p4_add_user () {
0055b56e 144 name=$1 &&
f7b5ff60 145 fullname="${2:-Dr. $1}"
0055b56e
PW
146 p4 user -f -i <<-EOF
147 User: $name
148 Email: $name@example.com
f7b5ff60 149 FullName: $fullname
0055b56e
PW
150 EOF
151}
152
26e6a27d
JD
153p4_add_job () {
154 p4 job -f -i <<-EOF
155 Job: $1
156 Status: open
157 User: dummy
158 Description:
159 EOF
160}
161
a98772c6 162retry_until_success () {
0e67c329
SG
163 nr_tries_left=$RETRY_TIMEOUT
164 until "$@" 2>/dev/null || test $nr_tries_left -eq 0
23aee419
LS
165 do
166 sleep 1
0e67c329 167 nr_tries_left=$(($nr_tries_left - 1))
23aee419
LS
168 done
169}
170
07353d90
SG
171stop_and_cleanup_p4d () {
172 kill -9 $p4d_pid $watchdog_pid
173 wait $p4d_pid
174 rm -rf "$db" "$cli" "$pidfile"
fc002330
PW
175}
176
a98772c6 177cleanup_git () {
23aee419 178 retry_until_success rm -r "$git"
b8afb908 179 test_path_is_missing "$git" &&
23aee419 180 retry_until_success mkdir "$git"
fc002330 181}
798d5980 182
a98772c6 183marshal_dump () {
798d5980
PW
184 what=$1 &&
185 line=${2:-1} &&
186 cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
187 import marshal
188 import sys
84096814 189 instream = getattr(sys.stdin, 'buffer', sys.stdin)
798d5980 190 for i in range($line):
84096814
LD
191 d = marshal.load(instream)
192 print(d[b'$what'].decode('utf-8'))
798d5980
PW
193 EOF
194 "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
195}
d2018293
PW
196
197#
198# Construct a client with this list of View lines
199#
a98772c6 200client_view () {
d2018293
PW
201 (
202 cat <<-EOF &&
50038ba9
PW
203 Client: $P4CLIENT
204 Description: $P4CLIENT
d2018293 205 Root: $cli
cfa96496 206 AltRoots: $(native_path "$cli")
e93f8695 207 LineEnd: unix
d2018293
PW
208 View:
209 EOF
6112541b 210 printf "\t%s\n" "$@"
d2018293
PW
211 ) | p4 client -i
212}
e9df0f9c 213
a98772c6 214is_cli_file_writeable () {
e9df0f9c
PW
215 # cygwin version of p4 does not set read-only attr,
216 # will be marked 444 but -w is true
217 file="$1" &&
218 if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
219 then
220 stat=$(stat --format=%a "$file") &&
221 test $stat = 644
222 else
223 test -w "$file"
224 fi
225}