t5548: refactor test cases by resetting upstream

Refactor the test cases with the following changes: - Calling setup_upstream() to reset upstream after running each test case. - Change the initial branch tips of the workspace to reduce the branch setup operations in the workspace. - Reduced the two steps of setting up and cleaning up the pre-receive hook by moving the operations into the corresponding test case, Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Feb 3, 2025 at 07:29 UTC bc0f5939a58fda328bdba70eb64a19c969b9f8d8
1 file changed +67 -82
t/t5548-push-porcelain.sh
+67 -82
@@ -94,8 +94,8 @@ setup_upstream_and_workbench () {
94 fi
95 upstream="$1"
96
97 - # Upstream after setup : main(B) foo(A) bar(A) baz(A)
98 - # Workbench after setup : main(A)
97 + # Upstream after setup: main(B) foo(A) bar(A) baz(A)
98 + # Workbench after setup: main(A) baz(A) next(A)
99 test_expect_success "setup upstream repository and workbench" '
100 setup_upstream "$upstream" &&
101 rm -rf workbench &&
@@ -103,6 +103,8 @@ setup_upstream_and_workbench () {
103 (
104 cd workbench &&
105 git update-ref refs/heads/main $A &&
106 + git update-ref refs/heads/baz $A &&
107 + git update-ref refs/heads/next $A &&
108 # Try to make a stable fixed width for abbreviated commit ID,
109 # this fixed-width oid will be replaced with "<OID>".
110 git config core.abbrev 7 &&
@@ -131,19 +133,14 @@ run_git_push_porcelain_output_test() {
133 # Refs of upstream : main(B) foo(A) bar(A) baz(A)
134 # Refs of workbench: main(A) baz(A) next(A)
135 # git-push : main(A) NULL (B) baz(A) next(A)
134 - test_expect_success "porcelain output of successful git-push ($PROTOCOL)" '
135 - (
136 - cd workbench &&
137 - git update-ref refs/heads/main $A &&
138 - git update-ref refs/heads/baz $A &&
139 - git update-ref refs/heads/next $A &&
140 - git push --porcelain --force origin \
141 - main \
142 - :refs/heads/foo \
143 - $B:bar \
144 - baz \
145 - next
146 - ) >out &&
136 + test_expect_success ".. git-push --porcelain --force ($PROTOCOL)" '
137 + test_when_finished "setup_upstream \"$upstream\"" &&
138 + git -C workbench push --porcelain --force origin \
139 + main \
140 + :refs/heads/foo \
141 + $B:bar \
142 + baz \
143 + next >out &&
144 make_user_friendly_and_stable_output <out >actual &&
145 format_and_save_expect <<-EOF &&
146 > To <URL/of/upstream.git>
@@ -167,115 +164,103 @@ run_git_push_porcelain_output_test() {
164 test_cmp expect actual
165 '
166
170 - # Refs of upstream : main(A) bar(B) baz(A) next(A)
171 - # Refs of workbench: main(B) bar(A) baz(A) next(A)
172 - # git-push : main(B) bar(A) NULL next(A)
173 - test_expect_success "atomic push failed ($PROTOCOL)" '
174 - (
175 - cd workbench &&
176 - git update-ref refs/heads/main $B &&
177 - git update-ref refs/heads/bar $A &&
178 - test_must_fail git push --atomic --porcelain origin \
179 - main \
180 - bar \
181 - :baz \
182 - next
183 - ) >out &&
167 + # Refs of upstream : main(B) foo(A) bar(A) baz(A)
168 + # Refs of workbench: main(A) baz(A) next(A)
169 + # git-push : main(A) NULL (B) baz(A) next(A)
170 + test_expect_success ".. git push --porcelain --atomic ($PROTOCOL)" '
171 + test_when_finished "setup_upstream \"$upstream\"" &&
172 + test_must_fail git -C workbench push --porcelain --atomic origin \
173 + main \
174 + :refs/heads/foo \
175 + $B:bar \
176 + baz \
177 + next >out &&
178 make_user_friendly_and_stable_output <out >actual &&
179 format_and_save_expect <<-EOF &&
186 - To <URL/of/upstream.git>
187 - > = refs/heads/next:refs/heads/next [up to date]
188 - > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
189 - > ! (delete):refs/heads/baz [rejected] (atomic push failed)
190 - > ! refs/heads/main:refs/heads/main [rejected] (atomic push failed)
191 - Done
180 + > To <URL/of/upstream.git>
181 + > = refs/heads/baz:refs/heads/baz [up to date]
182 + > ! <COMMIT-B>:refs/heads/bar [rejected] (atomic push failed)
183 + > ! (delete):refs/heads/foo [rejected] (atomic push failed)
184 + > ! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
185 + > ! refs/heads/next:refs/heads/next [rejected] (atomic push failed)
186 + > Done
187 EOF
188 test_cmp expect actual &&
189
190 git -C "$upstream" show-ref >out &&
191 make_user_friendly_and_stable_output <out >actual &&
192 cat >expect <<-EOF &&
198 - <COMMIT-B> refs/heads/bar
193 + <COMMIT-A> refs/heads/bar
194 <COMMIT-A> refs/heads/baz
200 - <COMMIT-A> refs/heads/main
201 - <COMMIT-A> refs/heads/next
195 + <COMMIT-A> refs/heads/foo
196 + <COMMIT-B> refs/heads/main
197 EOF
198 test_cmp expect actual
199 '
200
206 - test_expect_success "prepare pre-receive hook ($PROTOCOL)" '
207 - test_hook --setup -C "$upstream" pre-receive <<-EOF
208 - exit 1
201 + # Refs of upstream : main(B) foo(A) bar(A) baz(A)
202 + # Refs of workbench: main(A) baz(A) next(A)
203 + # git-push : main(A) NULL (B) baz(A) next(A)
204 + test_expect_success ".. pre-receive hook declined ($PROTOCOL)" '
205 + test_when_finished "rm -f \"$upstream/hooks/pre-receive\" &&
206 + setup_upstream \"$upstream\"" &&
207 + test_hook --setup -C "$upstream" pre-receive <<-EOF &&
208 + exit 1
209 EOF
210 - '
211 -
212 - # Refs of upstream : main(A) bar(B) baz(A) next(A)
213 - # Refs of workbench: main(B) bar(A) baz(A) next(A)
214 - # git-push : main(B) bar(A) NULL next(A)
215 - test_expect_success "pre-receive hook declined ($PROTOCOL)" '
216 - (
217 - cd workbench &&
218 - git update-ref refs/heads/main $B &&
219 - git update-ref refs/heads/bar $A &&
220 - test_must_fail git push --porcelain --force origin \
221 - main \
222 - bar \
223 - :baz \
224 - next
225 - ) >out &&
210 + test_must_fail git -C workbench push --porcelain --force origin \
211 + main \
212 + :refs/heads/foo \
213 + $B:bar \
214 + baz \
215 + next >out &&
216 make_user_friendly_and_stable_output <out >actual &&
217 format_and_save_expect <<-EOF &&
228 - To <URL/of/upstream.git>
229 - > = refs/heads/next:refs/heads/next [up to date]
230 - > ! refs/heads/bar:refs/heads/bar [remote rejected] (pre-receive hook declined)
231 - > ! :refs/heads/baz [remote rejected] (pre-receive hook declined)
218 + > To <URL/of/upstream.git>
219 + > = refs/heads/baz:refs/heads/baz [up to date]
220 + > ! <COMMIT-B>:refs/heads/bar [remote rejected] (pre-receive hook declined)
221 + > ! :refs/heads/foo [remote rejected] (pre-receive hook declined)
222 > ! refs/heads/main:refs/heads/main [remote rejected] (pre-receive hook declined)
233 - Done
223 + > ! refs/heads/next:refs/heads/next [remote rejected] (pre-receive hook declined)
224 + > Done
225 EOF
226 test_cmp expect actual &&
227
228 git -C "$upstream" show-ref >out &&
229 make_user_friendly_and_stable_output <out >actual &&
230 cat >expect <<-EOF &&
240 - <COMMIT-B> refs/heads/bar
231 + <COMMIT-A> refs/heads/bar
232 <COMMIT-A> refs/heads/baz
242 - <COMMIT-A> refs/heads/main
243 - <COMMIT-A> refs/heads/next
233 + <COMMIT-A> refs/heads/foo
234 + <COMMIT-B> refs/heads/main
235 EOF
236 test_cmp expect actual
237 '
238
248 - test_expect_success "remove pre-receive hook ($PROTOCOL)" '
249 - rm "$upstream/hooks/pre-receive"
250 - '
251 -
252 - # Refs of upstream : main(A) bar(B) baz(A) next(A)
253 - # Refs of workbench: main(B) bar(A) baz(A) next(A)
254 - # git-push : main(B) bar(A) NULL next(A)
255 - test_expect_success "non-fastforward push ($PROTOCOL)" '
239 + # Refs of upstream : main(B) foo(A) bar(A) baz(A)
240 + # Refs of workbench: main(A) baz(A) next(A)
241 + # git-push : main(A) next(A)
242 + test_expect_success ".. non-fastforward push ($PROTOCOL)" '
243 (
244 cd workbench &&
245 test_must_fail git push --porcelain origin \
246 main \
260 - bar \
261 - :baz \
247 next
248 ) >out &&
249 make_user_friendly_and_stable_output <out >actual &&
250 format_and_save_expect <<-EOF &&
266 - To <URL/of/upstream.git>
267 - > = refs/heads/next:refs/heads/next [up to date]
268 - > - :refs/heads/baz [deleted]
269 - > refs/heads/main:refs/heads/main <COMMIT-A>..<COMMIT-B>
270 - > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
271 - Done
251 + > To <URL/of/upstream.git>
252 + > * refs/heads/next:refs/heads/next [new branch]
253 + > ! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
254 + > Done
255 EOF
256 test_cmp expect actual &&
257
258 git -C "$upstream" show-ref >out &&
259 make_user_friendly_and_stable_output <out >actual &&
260 cat >expect <<-EOF &&
278 - <COMMIT-B> refs/heads/bar
261 + <COMMIT-A> refs/heads/bar
262 + <COMMIT-A> refs/heads/baz
263 + <COMMIT-A> refs/heads/foo
264 <COMMIT-B> refs/heads/main
265 <COMMIT-A> refs/heads/next
266 EOF