Skip to content

Commit 7da7f63

Browse files
avargitster
authored andcommitted
test-lib-functions: add and use a "test_hook" wrapper
Add a "test_hook" wrapper similar to the existing "test_config" wrapper added in d960c47 (test-lib: add helper functions for config, 2011-08-17). This wrapper: - Will clean up the hook with "test_when_finished", unless --setup is provided. - Will error if we clobber a hook, unless --clobber is provided. - Takes a name like "update" instead of ".git/hooks/update". - Accepts -C <dir>, like "test_config" and "test_commit". By using a wrapper we'll be able to easily change all the hook-related code that assumes that the template-created ".git/hooks" directory is created by "init", "clone" etc. once another topic follows-up and changes the test suite to stop creating trash directories using those templates. In addition this will make it easy to have the hooks configured using the "configuration-based hooks" topic, once we get around to integrating that. I.e. we'll be able to run the tests in a mode where we sometimes create a .git/hooks/<name>, and other times create a script in another location, and point the relevant configuration snippet to it. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74cc1aa commit 7da7f63

13 files changed

+97
-43
lines changed

t/t1416-ref-transaction-hooks.sh

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ test_expect_success setup '
1616
'
1717

1818
test_expect_success 'hook allows updating ref if successful' '
19-
test_when_finished "rm .git/hooks/reference-transaction" &&
2019
git reset --hard PRE &&
21-
write_script .git/hooks/reference-transaction <<-\EOF &&
20+
test_hook reference-transaction <<-\EOF &&
2221
echo "$*" >>actual
2322
EOF
2423
cat >expect <<-EOF &&
@@ -30,9 +29,8 @@ test_expect_success 'hook allows updating ref if successful' '
3029
'
3130

3231
test_expect_success 'hook aborts updating ref in prepared state' '
33-
test_when_finished "rm .git/hooks/reference-transaction" &&
3432
git reset --hard PRE &&
35-
write_script .git/hooks/reference-transaction <<-\EOF &&
33+
test_hook reference-transaction <<-\EOF &&
3634
if test "$1" = prepared
3735
then
3836
exit 1
@@ -43,9 +41,9 @@ test_expect_success 'hook aborts updating ref in prepared state' '
4341
'
4442

4543
test_expect_success 'hook gets all queued updates in prepared state' '
46-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
44+
test_when_finished "rm actual" &&
4745
git reset --hard PRE &&
48-
write_script .git/hooks/reference-transaction <<-\EOF &&
46+
test_hook reference-transaction <<-\EOF &&
4947
if test "$1" = prepared
5048
then
5149
while read -r line
@@ -66,9 +64,9 @@ test_expect_success 'hook gets all queued updates in prepared state' '
6664
'
6765

6866
test_expect_success 'hook gets all queued updates in committed state' '
69-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
67+
test_when_finished "rm actual" &&
7068
git reset --hard PRE &&
71-
write_script .git/hooks/reference-transaction <<-\EOF &&
69+
test_hook reference-transaction <<-\EOF &&
7270
if test "$1" = committed
7371
then
7472
while read -r line
@@ -86,9 +84,9 @@ test_expect_success 'hook gets all queued updates in committed state' '
8684
'
8785

8886
test_expect_success 'hook gets all queued updates in aborted state' '
89-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
87+
test_when_finished "rm actual" &&
9088
git reset --hard PRE &&
91-
write_script .git/hooks/reference-transaction <<-\EOF &&
89+
test_hook reference-transaction <<-\EOF &&
9290
if test "$1" = aborted
9391
then
9492
while read -r line
@@ -115,11 +113,11 @@ test_expect_success 'interleaving hook calls succeed' '
115113
116114
git init --bare target-repo.git &&
117115
118-
write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
116+
test_hook -C target-repo.git reference-transaction <<-\EOF &&
119117
echo $0 "$@" >>actual
120118
EOF
121119
122-
write_script target-repo.git/hooks/update <<-\EOF &&
120+
test_hook -C target-repo.git update <<-\EOF &&
123121
echo $0 "$@" >>actual
124122
EOF
125123
@@ -140,7 +138,7 @@ test_expect_success 'hook does not get called on packing refs' '
140138
# Pack references first such that we are in a known state.
141139
git pack-refs --all &&
142140
143-
write_script .git/hooks/reference-transaction <<-\EOF &&
141+
test_hook reference-transaction <<-\EOF &&
144142
echo "$@" >>actual
145143
cat >>actual
146144
EOF
@@ -166,7 +164,7 @@ test_expect_success 'deleting packed ref calls hook once' '
166164
git update-ref refs/heads/to-be-deleted $POST_OID &&
167165
git pack-refs --all &&
168166
169-
write_script .git/hooks/reference-transaction <<-\EOF &&
167+
test_hook reference-transaction <<-\EOF &&
170168
echo "$@" >>actual
171169
cat >>actual
172170
EOF

t/t1800-hook.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
2727
'
2828

2929
test_expect_success 'git hook run: basic' '
30-
write_script .git/hooks/test-hook <<-EOF &&
30+
test_hook test-hook <<-EOF &&
3131
echo Test hook
3232
EOF
3333
@@ -39,7 +39,7 @@ test_expect_success 'git hook run: basic' '
3939
'
4040

4141
test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
42-
write_script .git/hooks/test-hook <<-EOF &&
42+
test_hook test-hook <<-EOF &&
4343
echo >&1 Will end up on stderr
4444
echo >&2 Will end up on stderr
4545
EOF
@@ -84,7 +84,7 @@ test_expect_success 'git hook run arg u ments without -- is not allowed' '
8484
'
8585

8686
test_expect_success 'git hook run -- pass arguments' '
87-
write_script .git/hooks/test-hook <<-\EOF &&
87+
test_hook test-hook <<-\EOF &&
8888
echo $1
8989
echo $2
9090
EOF
@@ -99,7 +99,7 @@ test_expect_success 'git hook run -- pass arguments' '
9999
'
100100

101101
test_expect_success 'git hook run -- out-of-repo runs excluded' '
102-
write_script .git/hooks/test-hook <<-EOF &&
102+
test_hook test-hook <<-EOF &&
103103
echo Test hook
104104
EOF
105105
@@ -120,6 +120,10 @@ test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
120120
Hook ran four
121121
EOF
122122
123+
test_hook test-hook <<-EOF &&
124+
echo Test hook
125+
EOF
126+
123127
# Test various ways of specifying the path. See also
124128
# t1350-config-hooks-path.sh
125129
>actual &&

t/t5401-update-hooks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ test_expect_success 'send-pack stderr contains hook messages' '
136136
'
137137

138138
test_expect_success 'pre-receive hook that forgets to read its input' '
139-
write_script victim.git/hooks/pre-receive <<-\EOF &&
139+
test_hook --clobber -C victim.git pre-receive <<-\EOF &&
140140
exit 0
141141
EOF
142142
rm -f victim.git/hooks/update victim.git/hooks/post-update &&

t/t5406-remote-rejects.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_description='remote push rejects are reported by client'
55
. ./test-lib.sh
66

77
test_expect_success 'setup' '
8-
write_script .git/hooks/update <<-\EOF &&
8+
test_hook update <<-\EOF &&
99
exit 1
1010
EOF
1111
echo 1 >file &&

t/t5409-colorize-remote-messages.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_description='remote messages are colorized on the client'
55
. ./test-lib.sh
66

77
test_expect_success 'setup' '
8-
write_script .git/hooks/update <<-\EOF &&
8+
test_hook --setup update <<-\EOF &&
99
echo error: error
1010
echo ERROR: also highlighted
1111
echo hint: hint

t/t5411-proc-receive-hook.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ setup_upstream_and_workbench () {
3636
TAG=$(git -C workbench rev-parse v123) &&
3737
3838
# setup pre-receive hook
39-
write_script upstream.git/hooks/pre-receive <<-\EOF &&
39+
test_hook --setup -C upstream.git pre-receive <<-\EOF &&
4040
exec >&2
4141
echo "# pre-receive hook"
4242
while read old new ref
@@ -46,7 +46,7 @@ setup_upstream_and_workbench () {
4646
EOF
4747
4848
# setup post-receive hook
49-
write_script upstream.git/hooks/post-receive <<-\EOF &&
49+
test_hook --setup -C upstream.git post-receive <<-\EOF &&
5050
exec >&2
5151
echo "# post-receive hook"
5252
while read old new ref

t/t5503-tagfollow.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ test_expect_success 'atomic fetch with failing backfill' '
169169
# one of both fails to update correctly.
170170
#
171171
# To trigger failure we simply abort when backfilling a tag.
172-
write_script clone3/.git/hooks/reference-transaction <<-\EOF &&
172+
test_hook -C clone3 reference-transaction <<-\EOF &&
173173
while read oldrev newrev reference
174174
do
175175
if test "$reference" = refs/tags/tag1
@@ -201,7 +201,7 @@ test_expect_success 'atomic fetch with backfill should use single transaction' '
201201
$ZERO_OID $T refs/tags/tag1
202202
EOF
203203
204-
write_script clone4/.git/hooks/reference-transaction <<-\EOF &&
204+
test_hook -C clone4 reference-transaction <<-\EOF &&
205205
( echo "$*" && cat ) >>actual
206206
EOF
207207

t/t5510-fetch.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ test_expect_success 'fetch --atomic executes a single reference transaction only
273273
EOF
274274
275275
rm -f atomic/actual &&
276-
write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
276+
test_hook -C atomic reference-transaction <<-\EOF &&
277277
( echo "$*" && cat ) >>actual
278278
EOF
279279
@@ -306,7 +306,7 @@ test_expect_success 'fetch --atomic aborts all reference updates if hook aborts'
306306
EOF
307307
308308
rm -f atomic/actual &&
309-
write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
309+
test_hook -C atomic/.git reference-transaction <<-\EOF &&
310310
( echo "$*" && cat ) >>actual
311311
exit 1
312312
EOF
@@ -334,7 +334,7 @@ test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
334334
test_line_count = 2 atomic/.git/FETCH_HEAD &&
335335
cp atomic/.git/FETCH_HEAD expected &&
336336
337-
write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
337+
test_hook -C atomic reference-transaction <<-\EOF &&
338338
exit 1
339339
EOF
340340
@@ -364,7 +364,7 @@ test_expect_success 'fetch --atomic --prune executes a single reference transact
364364
$ZERO_OID $head_oid refs/remotes/origin/new-branch
365365
EOF
366366
367-
write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
367+
test_hook -C atomic reference-transaction <<-\EOF &&
368368
( echo "$*" && cat ) >>actual
369369
EOF
370370

t/t5521-pull-options.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ test_expect_success 'git pull --no-verify flag passed to merge' '
233233
git init src &&
234234
test_commit -C src one &&
235235
git clone src dst &&
236-
write_script dst/.git/hooks/commit-msg <<-\EOF &&
236+
test_hook -C dst commit-msg <<-\EOF &&
237237
false
238238
EOF
239239
test_commit -C src two &&
@@ -245,7 +245,7 @@ test_expect_success 'git pull --no-verify --verify passed to merge' '
245245
git init src &&
246246
test_commit -C src one &&
247247
git clone src dst &&
248-
write_script dst/.git/hooks/commit-msg <<-\EOF &&
248+
test_hook -C dst commit-msg <<-\EOF &&
249249
false
250250
EOF
251251
test_commit -C src two &&

t/t5547-push-quarantine.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_description='check quarantine of objects during push'
55

66
test_expect_success 'create picky dest repo' '
77
git init --bare dest.git &&
8-
write_script dest.git/hooks/pre-receive <<-\EOF
8+
test_hook --setup -C dest.git pre-receive <<-\EOF
99
while read old new ref; do
1010
test "$(git log -1 --format=%s $new)" = reject && exit 1
1111
done
@@ -60,7 +60,7 @@ test_expect_success 'push to repo path with path separator (colon)' '
6060

6161
test_expect_success 'updating a ref from quarantine is forbidden' '
6262
git init --bare update.git &&
63-
write_script update.git/hooks/pre-receive <<-\EOF &&
63+
test_hook -C update.git pre-receive <<-\EOF &&
6464
read old new refname
6565
git update-ref refs/heads/unrelated $new
6666
exit 1

0 commit comments

Comments
 (0)