interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t5412-receive-report-hook.sh

257 lines · 7579 bytes Raw Blame History
1#!/bin/sh
2
3test_description='test receive-report hook'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10. "$TEST_DIRECTORY"/t5411/common-functions.sh
11
12URL_PREFIX="\.\."
13
14test_expect_success "setup workbench" '
15 git init workbench &&
16 create_commits_in workbench A B
17'
18
19test_expect_success "no report hook, push succeeds" '
20 test_when_finished "rm -rf upstream" &&
21 test_when_finished "git -C workbench remote remove origin" &&
22 git init --bare upstream &&
23
24 git -C workbench remote add origin ../upstream &&
25 git -C workbench push origin $A:refs/heads/main &&
26 git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
27
28 make_user_friendly_and_stable_output <out >actual &&
29 cat >expect <<-\EOF &&
30 To ../upstream
31 <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
32 EOF
33 test_cmp expect actual
34'
35
36test_expect_success "passthrough does not alter report" '
37 test_when_finished "rm -rf upstream" &&
38 test_when_finished "git -C workbench remote remove origin" &&
39 git init --bare upstream &&
40
41 test_hook -C upstream --setup receive-report <<-\EOF &&
42 cat
43 EOF
44
45 git -C workbench remote add origin ../upstream &&
46 git -C workbench push origin $A:refs/heads/main &&
47 git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
48
49 make_user_friendly_and_stable_output <out >actual &&
50 cat >expect <<-\EOF &&
51 To ../upstream
52 <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
53 EOF
54 test_cmp expect actual
55'
56
57test_expect_success "non-zero exit reports as hook failed" '
58 test_when_finished "rm -rf upstream" &&
59 test_when_finished "git -C workbench remote remove origin" &&
60
61 git init --bare upstream &&
62 git -C workbench remote add origin ../upstream &&
63 git -C workbench push origin $A:refs/heads/main &&
64
65 test_hook -C upstream --setup receive-report <<-\EOF &&
66 exit 1
67 EOF
68
69 test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
70 make_user_friendly_and_stable_output <out >actual &&
71 cat >expect <<-\EOF &&
72 To ../upstream
73 ! [remote rejected] <COMMIT-B> -> main (receive-report hook failed)
74 EOF
75 test_cmp expect actual
76'
77
78test_expect_success "hook is invoked and receives report on stdin" '
79 test_when_finished "rm -rf upstream" &&
80 test_when_finished "git -C workbench remote remove origin" &&
81
82 git init --bare upstream &&
83 test_hook -C upstream --setup receive-report <<-EOF &&
84 tee raw
85 EOF
86
87 git -C workbench remote add origin ../upstream &&
88 git -C workbench push origin $A:refs/heads/main &&
89 git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
90
91 make_user_friendly_and_stable_output <out >actual &&
92 cat >expect <<-EOF &&
93 To ../upstream
94 <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
95 EOF
96 test_cmp expect actual &&
97
98 test-tool pkt-line unpack <upstream/raw >actual-report &&
99 cat >expect-report <<-EOF &&
100 unpack ok
101 ok refs/heads/main
102 0000
103 EOF
104 test_cmp expect-report actual-report
105'
106
107test_expect_success "hook can modify the report sent to client" '
108 test_when_finished "rm -rf upstream" &&
109 test_when_finished "git -C workbench remote remove origin" &&
110
111 git init --bare upstream &&
112 git -C workbench remote add origin ../upstream &&
113 git -C workbench push origin $A:refs/heads/main &&
114
115 test_hook -C upstream --setup receive-report <<-\EOF &&
116 test-tool pkt-line unpack |
117 sed "s/^ok /ng /" |
118 test-tool pkt-line pack
119 EOF
120
121 test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
122 make_user_friendly_and_stable_output <out >actual &&
123 cat >expect <<-\EOF &&
124 To ../upstream
125 ! [remote rejected] <COMMIT-B> -> main (failed)
126 EOF
127 test_cmp expect actual
128'
129
130test_expect_success "hook can modify the unpack status" '
131 test_when_finished "rm -rf upstream" &&
132 test_when_finished "git -C workbench remote remove origin" &&
133
134 git init --bare upstream &&
135 git -C workbench remote add origin ../upstream &&
136 git -C workbench push origin $A:refs/heads/main &&
137
138 test_hook -C upstream --setup receive-report <<-\EOF &&
139 test-tool pkt-line unpack |
140 sed "s/^unpack ok$/unpack push failed due to server error/" |
141 test-tool pkt-line pack
142 EOF
143
144 test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
145 test_grep "error: remote unpack failed: push failed due to server error" out &&
146 make_user_friendly_and_stable_output <out >actual &&
147 cat >expect <<-\EOF &&
148 To ../upstream
149 <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
150 EOF
151 test_cmp expect actual
152'
153
154test_expect_success "hook can report a custom failure message" '
155 test_when_finished "rm -rf upstream" &&
156 test_when_finished "git -C workbench remote remove origin" &&
157
158 git init --bare upstream &&
159 git -C workbench remote add origin ../upstream &&
160 git -C workbench push origin $A:refs/heads/main &&
161
162 test_hook -C upstream --setup receive-report <<-\EOF &&
163 echo "push rejected: service X is down" >&2
164 test-tool pkt-line unpack |
165 sed "s/^ok \(.*\)/ng \1 service-x-is-down/" |
166 test-tool pkt-line pack |
167 tee raw
168 EOF
169
170 test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
171 test_grep "push rejected: service X is down" out &&
172
173 test-tool pkt-line unpack <upstream/raw >actual-report &&
174 cat >expect-report <<-\EOF &&
175 unpack ok
176 ng refs/heads/main service-x-is-down
177 0000
178 EOF
179 test_cmp expect-report actual-report
180'
181
182test_expect_success "hook stderr with zero exit status code" '
183 test_when_finished "rm -rf upstream" &&
184 test_when_finished "git -C workbench remote remove origin" &&
185
186 git init --bare upstream &&
187 git -C workbench remote add origin ../upstream &&
188 git -C workbench push origin $A:refs/heads/main &&
189
190 test_hook -C upstream --setup receive-report <<-\EOF &&
191 echo "push rejected: service X is down" >&2
192 tee raw
193 EOF
194
195 git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
196 test_grep "push rejected: service X is down" out &&
197
198 test-tool pkt-line unpack <upstream/raw >actual-report &&
199 cat >expect-report <<-\EOF &&
200 unpack ok
201 ok refs/heads/main
202 0000
203 EOF
204 test_cmp expect-report actual-report
205'
206
207test_expect_success "non-zero exit with pre-existing ng from proc-receive" '
208 test_when_finished "rm -rf upstream" &&
209 test_when_finished "git -C workbench remote remove origin" &&
210
211 git init --bare upstream &&
212 git -C upstream config receive.procReceiveRefs refs/for &&
213 git -C workbench remote add origin ../upstream &&
214 git -C workbench push origin $A:refs/heads/main &&
215
216 # Use a proc-receive hook to generate a dynamic error string.
217 # This is used to capture any leaks stemming from overriding the
218 # error message via the receive-report.
219 test_hook -C upstream --setup proc-receive <<-\EOF &&
220 test-tool proc-receive -r "ng refs/for/main/topic push-rejected-by-service-x"
221 EOF
222
223 test_hook -C upstream --setup receive-report <<-\EOF &&
224 tee raw
225 exit 1
226 EOF
227
228 test_must_fail git -C workbench push origin HEAD:refs/for/main/topic >out 2>&1 &&
229 test_grep "receive-report hook failed" out &&
230
231 test-tool pkt-line unpack <upstream/raw >actual-report &&
232 cat >expect-report <<-\EOF &&
233 unpack ok
234 ng refs/for/main/topic push-rejected-by-service-x
235 0000
236 EOF
237 test_cmp expect-report actual-report
238'
239
240test_expect_success "hook stderr is relayed to client via sideband" '
241 test_when_finished "rm -rf upstream" &&
242 test_when_finished "git -C workbench remote remove origin" &&
243
244 git init --bare upstream &&
245 git -C workbench remote add origin ../upstream &&
246 git -C workbench push origin $A:refs/heads/main &&
247
248 test_hook -C upstream --setup receive-report <<-\EOF &&
249 echo "hook-stderr-message" >&2
250 exit 1
251 EOF
252
253 test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
254 test_grep "remote: hook-stderr-message" out
255'
256
257test_done