Skip to content

Commit f818f7f

Browse files
avargitster
authored andcommitted
fetch+push tests: use "test_hook" and "test_when_finished" pattern
Change the "t5516-fetch-push.sh" test code to make use of the new "test_hook" helper, and to use "test_when_finished" to have tests clean up their own state, instead of relying on subsequent tests to clean the trash directory. Before this each test would have been responsible for cleaning up after a preceding test (which may or may not have run, e.g. if --run or "GIT_SKIP_TESTS" was used), now each test will instead clean up after itself. In order to use both "test_hook" and "test_when_finished" we need to move them out of sub-shells, which requires some refactoring. While we're at it split up the "push with negotiation" test, now the middle of the test doesn't need to "rm event", and since it delimited two halves that were testing two different things the end-state is easier to read and reason about. While changing these lines make the minor change from "-fr" to "-rf" as the "rm" argument, some of them used it already, it's more common in the test suite, and it leaves the end-state of the file with more consistency. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 003cdf8 commit f818f7f

File tree

1 file changed

+88
-103
lines changed

1 file changed

+88
-103
lines changed

t/t5516-fetch-push.sh

Lines changed: 88 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ D=$(pwd)
2323

2424
mk_empty () {
2525
repo_name="$1"
26-
rm -fr "$repo_name" &&
27-
mkdir "$repo_name" &&
28-
(
29-
cd "$repo_name" &&
30-
git init &&
31-
git config receive.denyCurrentBranch warn
32-
)
26+
test_when_finished "rm -rf \"$repo_name\"" &&
27+
test_path_is_missing "$repo_name" &&
28+
git init "$repo_name" &&
29+
git -C "$repo_name" config receive.denyCurrentBranch warn
3330
}
3431

3532
mk_test () {
@@ -58,40 +55,28 @@ mk_test () {
5855
mk_test_with_hooks() {
5956
repo_name=$1
6057
mk_test "$@" &&
61-
(
62-
cd "$repo_name" &&
63-
mkdir .git/hooks &&
64-
cd .git/hooks &&
65-
66-
cat >pre-receive <<-'EOF' &&
67-
#!/bin/sh
68-
cat - >>pre-receive.actual
69-
EOF
70-
71-
cat >update <<-'EOF' &&
72-
#!/bin/sh
73-
printf "%s %s %s\n" "$@" >>update.actual
74-
EOF
75-
76-
cat >post-receive <<-'EOF' &&
77-
#!/bin/sh
78-
cat - >>post-receive.actual
79-
EOF
80-
81-
cat >post-update <<-'EOF' &&
82-
#!/bin/sh
83-
for ref in "$@"
84-
do
85-
printf "%s\n" "$ref" >>post-update.actual
86-
done
87-
EOF
88-
89-
chmod +x pre-receive update post-receive post-update
90-
)
58+
test_hook -C "$repo_name" pre-receive <<-'EOF' &&
59+
cat - >>pre-receive.actual
60+
EOF
61+
62+
test_hook -C "$repo_name" update <<-'EOF' &&
63+
printf "%s %s %s\n" "$@" >>update.actual
64+
EOF
65+
66+
test_hook -C "$repo_name" post-receive <<-'EOF' &&
67+
cat - >>post-receive.actual
68+
EOF
69+
70+
test_hook -C "$repo_name" post-update <<-'EOF'
71+
for ref in "$@"
72+
do
73+
printf "%s\n" "$ref" >>post-update.actual
74+
done
75+
EOF
9176
}
9277

9378
mk_child() {
94-
rm -rf "$2" &&
79+
test_when_finished "rm -rf \"$2\"" &&
9580
git clone "$1" "$2"
9681
}
9782

@@ -196,32 +181,32 @@ grep_wrote () {
196181
grep 'write_pack_file/wrote.*"value":"'$1'"' $2
197182
}
198183

199-
test_expect_success 'push with negotiation' '
200-
# Without negotiation
184+
test_expect_success 'push without negotiation' '
201185
mk_empty testrepo &&
202186
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
203187
test_commit -C testrepo unrelated_commit &&
204188
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
205-
echo now pushing without negotiation &&
189+
test_when_finished "rm event" &&
206190
GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
207-
grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
191+
grep_wrote 5 event # 2 commits, 2 trees, 1 blob
192+
'
208193

209-
# Same commands, but with negotiation
210-
rm event &&
194+
test_expect_success 'push with negotiation' '
211195
mk_empty testrepo &&
212196
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
213197
test_commit -C testrepo unrelated_commit &&
214198
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
199+
test_when_finished "rm event" &&
215200
GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
216201
grep_wrote 2 event # 1 commit, 1 tree
217202
'
218203

219204
test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
220-
rm event &&
221205
mk_empty testrepo &&
222206
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
223207
test_commit -C testrepo unrelated_commit &&
224208
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
209+
test_when_finished "rm event" &&
225210
GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
226211
git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
227212
grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
@@ -667,7 +652,6 @@ test_expect_success 'push does not update local refs on failure' '
667652
668653
mk_test testrepo heads/main &&
669654
mk_child testrepo child &&
670-
mkdir testrepo/.git/hooks &&
671655
echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
672656
chmod +x testrepo/.git/hooks/pre-receive &&
673657
(
@@ -1329,7 +1313,7 @@ done
13291313

13301314
test_expect_success 'fetch follows tags by default' '
13311315
mk_test testrepo heads/main &&
1332-
rm -fr src dst &&
1316+
test_when_finished "rm -rf src" &&
13331317
git init src &&
13341318
(
13351319
cd src &&
@@ -1339,6 +1323,7 @@ test_expect_success 'fetch follows tags by default' '
13391323
sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
13401324
sort -k 3 >../expect
13411325
) &&
1326+
test_when_finished "rm -rf dst" &&
13421327
git init dst &&
13431328
(
13441329
cd dst &&
@@ -1364,8 +1349,9 @@ test_expect_success 'peeled advertisements are not considered ref tips' '
13641349

13651350
test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
13661351
mk_test testrepo heads/main &&
1367-
rm -fr src dst &&
1352+
test_when_finished "rm -rf src" &&
13681353
git init src &&
1354+
test_when_finished "rm -rf dst" &&
13691355
git init --bare dst &&
13701356
(
13711357
cd src &&
@@ -1388,8 +1374,9 @@ test_expect_success 'pushing a specific ref applies remote.$name.push as refmap'
13881374

13891375
test_expect_success 'with no remote.$name.push, it is not used as refmap' '
13901376
mk_test testrepo heads/main &&
1391-
rm -fr src dst &&
1377+
test_when_finished "rm -rf src" &&
13921378
git init src &&
1379+
test_when_finished "rm -rf dst" &&
13931380
git init --bare dst &&
13941381
(
13951382
cd src &&
@@ -1410,8 +1397,9 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
14101397

14111398
test_expect_success 'with no remote.$name.push, upstream mapping is used' '
14121399
mk_test testrepo heads/main &&
1413-
rm -fr src dst &&
1400+
test_when_finished "rm -rf src" &&
14141401
git init src &&
1402+
test_when_finished "rm -rf dst" &&
14151403
git init --bare dst &&
14161404
(
14171405
cd src &&
@@ -1439,8 +1427,9 @@ test_expect_success 'with no remote.$name.push, upstream mapping is used' '
14391427

14401428
test_expect_success 'push does not follow tags by default' '
14411429
mk_test testrepo heads/main &&
1442-
rm -fr src dst &&
1430+
test_when_finished "rm -rf src" &&
14431431
git init src &&
1432+
test_when_finished "rm -rf dst" &&
14441433
git init --bare dst &&
14451434
(
14461435
cd src &&
@@ -1462,8 +1451,9 @@ test_expect_success 'push does not follow tags by default' '
14621451

14631452
test_expect_success 'push --follow-tags only pushes relevant tags' '
14641453
mk_test testrepo heads/main &&
1465-
rm -fr src dst &&
1454+
test_when_finished "rm -rf src" &&
14661455
git init src &&
1456+
test_when_finished "rm -rf dst" &&
14671457
git init --bare dst &&
14681458
(
14691459
cd src &&
@@ -1501,9 +1491,9 @@ EOF
15011491
'
15021492

15031493
test_expect_success 'pushing a tag pushes the tagged object' '
1504-
rm -rf dst.git &&
15051494
blob=$(echo unreferenced | git hash-object -w --stdin) &&
15061495
git tag -m foo tag-of-blob $blob &&
1496+
test_when_finished "rm -rf dst.git" &&
15071497
git init --bare dst.git &&
15081498
git push dst.git tag-of-blob &&
15091499
# the receiving index-pack should have noticed
@@ -1514,7 +1504,7 @@ test_expect_success 'pushing a tag pushes the tagged object' '
15141504
'
15151505

15161506
test_expect_success 'push into bare respects core.logallrefupdates' '
1517-
rm -rf dst.git &&
1507+
test_when_finished "rm -rf dst.git" &&
15181508
git init --bare dst.git &&
15191509
git -C dst.git config core.logallrefupdates true &&
15201510
@@ -1532,7 +1522,7 @@ test_expect_success 'push into bare respects core.logallrefupdates' '
15321522
'
15331523

15341524
test_expect_success 'fetch into bare respects core.logallrefupdates' '
1535-
rm -rf dst.git &&
1525+
test_when_finished "rm -rf dst.git" &&
15361526
git init --bare dst.git &&
15371527
(
15381528
cd dst.git &&
@@ -1553,6 +1543,7 @@ test_expect_success 'fetch into bare respects core.logallrefupdates' '
15531543
'
15541544

15551545
test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1546+
mk_empty testrepo &&
15561547
git push testrepo main &&
15571548
(
15581549
cd testrepo &&
@@ -1655,7 +1646,7 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
16551646
) &&
16561647
16571648
# (5) push into void
1658-
rm -fr void &&
1649+
test_when_finished "rm -rf void" &&
16591650
git init void &&
16601651
(
16611652
cd void &&
@@ -1677,26 +1668,23 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
16771668
'
16781669

16791670
test_expect_success 'updateInstead with push-to-checkout hook' '
1680-
rm -fr testrepo &&
1671+
test_when_finished "rm -rf testrepo" &&
16811672
git init testrepo &&
1682-
(
1683-
cd testrepo &&
1684-
git pull .. main &&
1685-
git reset --hard HEAD^^ &&
1686-
git tag initial &&
1687-
git config receive.denyCurrentBranch updateInstead &&
1688-
write_script .git/hooks/push-to-checkout <<-\EOF
1689-
echo >&2 updating from $(git rev-parse HEAD)
1690-
echo >&2 updating to "$1"
1691-
1692-
git update-index -q --refresh &&
1693-
git read-tree -u -m HEAD "$1" || {
1694-
status=$?
1695-
echo >&2 read-tree failed
1696-
exit $status
1697-
}
1698-
EOF
1699-
) &&
1673+
git -C testrepo pull .. main &&
1674+
git -C testrepo reset --hard HEAD^^ &&
1675+
git -C testrepo tag initial &&
1676+
git -C testrepo config receive.denyCurrentBranch updateInstead &&
1677+
test_hook -C testrepo push-to-checkout <<-\EOF &&
1678+
echo >&2 updating from $(git rev-parse HEAD)
1679+
echo >&2 updating to "$1"
1680+
1681+
git update-index -q --refresh &&
1682+
git read-tree -u -m HEAD "$1" || {
1683+
status=$?
1684+
echo >&2 read-tree failed
1685+
exit $status
1686+
}
1687+
EOF
17001688
17011689
# Try pushing into a pristine
17021690
git push testrepo main &&
@@ -1739,35 +1727,32 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
17391727
) &&
17401728
17411729
# push into void
1742-
rm -fr void &&
1730+
test_when_finished "rm -rf void" &&
17431731
git init void &&
1744-
(
1745-
cd void &&
1746-
git config receive.denyCurrentBranch updateInstead &&
1747-
write_script .git/hooks/push-to-checkout <<-\EOF
1748-
if git rev-parse --quiet --verify HEAD
1749-
then
1750-
has_head=yes
1751-
echo >&2 updating from $(git rev-parse HEAD)
1752-
else
1753-
has_head=no
1754-
echo >&2 pushing into void
1755-
fi
1756-
echo >&2 updating to "$1"
1757-
1758-
git update-index -q --refresh &&
1759-
case "$has_head" in
1760-
yes)
1761-
git read-tree -u -m HEAD "$1" ;;
1762-
no)
1763-
git read-tree -u -m "$1" ;;
1764-
esac || {
1765-
status=$?
1766-
echo >&2 read-tree failed
1767-
exit $status
1768-
}
1769-
EOF
1770-
) &&
1732+
git -C void config receive.denyCurrentBranch updateInstead &&
1733+
test_hook -C void push-to-checkout <<-\EOF &&
1734+
if git rev-parse --quiet --verify HEAD
1735+
then
1736+
has_head=yes
1737+
echo >&2 updating from $(git rev-parse HEAD)
1738+
else
1739+
has_head=no
1740+
echo >&2 pushing into void
1741+
fi
1742+
echo >&2 updating to "$1"
1743+
1744+
git update-index -q --refresh &&
1745+
case "$has_head" in
1746+
yes)
1747+
git read-tree -u -m HEAD "$1" ;;
1748+
no)
1749+
git read-tree -u -m "$1" ;;
1750+
esac || {
1751+
status=$?
1752+
echo >&2 read-tree failed
1753+
exit $status
1754+
}
1755+
EOF
17711756
17721757
git push void main &&
17731758
(

0 commit comments

Comments
 (0)