Skip to content

Commit a5bf611

Browse files
committed
Merge branch 'ab/hook-tests-updates'
Update tests around the use of hook scripts. * ab/hook-tests-updates: http tests: use "test_hook" for "smart" and "dumb" http tests proc-receive hook tests: use "test_hook" instead of "write_script" tests: extend "test_hook" for "rm" and "chmod -x", convert "$HOOK" tests: use "test_hook" for misc "mkdir -p" and "chmod" cases tests: change "mkdir -p && write_script" to use "test_hook" tests: change "cat && chmod +x" to use "test_hook" gc + p4 tests: use "test_hook", remove sub-shells fetch+push tests: use "test_hook" and "test_when_finished" pattern bugreport tests: tighten up "git bugreport -s hooks" test tests: assume the hooks are disabled by default http tests: don't rely on "hook/post-update.sample" hook tests: turn exit code assertions into a loop test-lib-functions: add and use a "test_hook" wrapper
2 parents 2ea7e40 + f6db603 commit a5bf611

File tree

67 files changed

+575
-606
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+575
-606
lines changed

t/t0029-core-unsetenvvars.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ then
1212
fi
1313

1414
test_expect_success 'setup' '
15-
mkdir -p "$TRASH_DIRECTORY/.git/hooks" &&
16-
write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF
15+
test_hook --setup pre-commit <<-\EOF
1716
echo $HOBBES >&2
1817
EOF
1918
'

t/t0091-bugreport.sh

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ test_expect_success 'can create leading directories outside of a git dir' '
6060

6161
test_expect_success 'indicates populated hooks' '
6262
test_when_finished rm git-bugreport-hooks.txt &&
63-
test_when_finished rm -fr .git/hooks &&
64-
rm -fr .git/hooks &&
65-
mkdir .git/hooks &&
66-
for hook in applypatch-msg prepare-commit-msg.sample
67-
do
68-
write_script ".git/hooks/$hook" <<-EOF || return 1
69-
echo "hook $hook exists"
70-
EOF
71-
done &&
63+
64+
test_hook applypatch-msg <<-\EOF &&
65+
true
66+
EOF
67+
test_hook unknown-hook <<-\EOF &&
68+
true
69+
EOF
7270
git bugreport -s hooks &&
73-
grep applypatch-msg git-bugreport-hooks.txt &&
74-
! grep prepare-commit-msg git-bugreport-hooks.txt
71+
72+
sort >expect <<-\EOF &&
73+
[Enabled Hooks]
74+
applypatch-msg
75+
EOF
76+
77+
sed -ne "/^\[Enabled Hooks\]$/,/^$/p" <git-bugreport-hooks.txt >actual &&
78+
test_cmp expect actual
7579
'
7680

7781
test_done

t/t1350-config-hooks-path.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ test_description='Test the core.hooksPath configuration variable'
66

77
test_expect_success 'set up a pre-commit hook in core.hooksPath' '
88
>actual &&
9-
mkdir -p .git/custom-hooks .git/hooks &&
9+
mkdir -p .git/custom-hooks &&
1010
write_script .git/custom-hooks/pre-commit <<-\EOF &&
1111
echo CUSTOM >>actual
1212
EOF
13-
write_script .git/hooks/pre-commit <<-\EOF
13+
test_hook --setup pre-commit <<-\EOF
1414
echo NORMAL >>actual
1515
EOF
1616
'

t/t1416-ref-transaction-hooks.sh

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
88
. ./test-lib.sh
99

1010
test_expect_success setup '
11-
mkdir -p .git/hooks &&
1211
test_commit PRE &&
1312
PRE_OID=$(git rev-parse PRE) &&
1413
test_commit POST &&
1514
POST_OID=$(git rev-parse POST)
1615
'
1716

1817
test_expect_success 'hook allows updating ref if successful' '
19-
test_when_finished "rm .git/hooks/reference-transaction" &&
2018
git reset --hard PRE &&
21-
write_script .git/hooks/reference-transaction <<-\EOF &&
19+
test_hook reference-transaction <<-\EOF &&
2220
echo "$*" >>actual
2321
EOF
2422
cat >expect <<-EOF &&
@@ -30,9 +28,8 @@ test_expect_success 'hook allows updating ref if successful' '
3028
'
3129

3230
test_expect_success 'hook aborts updating ref in prepared state' '
33-
test_when_finished "rm .git/hooks/reference-transaction" &&
3431
git reset --hard PRE &&
35-
write_script .git/hooks/reference-transaction <<-\EOF &&
32+
test_hook reference-transaction <<-\EOF &&
3633
if test "$1" = prepared
3734
then
3835
exit 1
@@ -43,9 +40,9 @@ test_expect_success 'hook aborts updating ref in prepared state' '
4340
'
4441

4542
test_expect_success 'hook gets all queued updates in prepared state' '
46-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
43+
test_when_finished "rm actual" &&
4744
git reset --hard PRE &&
48-
write_script .git/hooks/reference-transaction <<-\EOF &&
45+
test_hook reference-transaction <<-\EOF &&
4946
if test "$1" = prepared
5047
then
5148
while read -r line
@@ -66,9 +63,9 @@ test_expect_success 'hook gets all queued updates in prepared state' '
6663
'
6764

6865
test_expect_success 'hook gets all queued updates in committed state' '
69-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
66+
test_when_finished "rm actual" &&
7067
git reset --hard PRE &&
71-
write_script .git/hooks/reference-transaction <<-\EOF &&
68+
test_hook reference-transaction <<-\EOF &&
7269
if test "$1" = committed
7370
then
7471
while read -r line
@@ -86,9 +83,9 @@ test_expect_success 'hook gets all queued updates in committed state' '
8683
'
8784

8885
test_expect_success 'hook gets all queued updates in aborted state' '
89-
test_when_finished "rm .git/hooks/reference-transaction actual" &&
86+
test_when_finished "rm actual" &&
9087
git reset --hard PRE &&
91-
write_script .git/hooks/reference-transaction <<-\EOF &&
88+
test_hook reference-transaction <<-\EOF &&
9289
if test "$1" = aborted
9390
then
9491
while read -r line
@@ -115,11 +112,11 @@ test_expect_success 'interleaving hook calls succeed' '
115112
116113
git init --bare target-repo.git &&
117114
118-
write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
115+
test_hook -C target-repo.git reference-transaction <<-\EOF &&
119116
echo $0 "$@" >>actual
120117
EOF
121118
122-
write_script target-repo.git/hooks/update <<-\EOF &&
119+
test_hook -C target-repo.git update <<-\EOF &&
123120
echo $0 "$@" >>actual
124121
EOF
125122
@@ -140,7 +137,7 @@ test_expect_success 'hook does not get called on packing refs' '
140137
# Pack references first such that we are in a known state.
141138
git pack-refs --all &&
142139
143-
write_script .git/hooks/reference-transaction <<-\EOF &&
140+
test_hook reference-transaction <<-\EOF &&
144141
echo "$@" >>actual
145142
cat >>actual
146143
EOF
@@ -166,7 +163,7 @@ test_expect_success 'deleting packed ref calls hook once' '
166163
git update-ref refs/heads/to-be-deleted $POST_OID &&
167164
git pack-refs --all &&
168165
169-
write_script .git/hooks/reference-transaction <<-\EOF &&
166+
test_hook reference-transaction <<-\EOF &&
170167
echo "$@" >>actual
171168
cat >>actual
172169
EOF

t/t1800-hook.sh

Lines changed: 17 additions & 28 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
@@ -53,38 +53,23 @@ test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
5353
test_must_be_empty stdout.actual
5454
'
5555

56-
test_expect_success 'git hook run: exit codes are passed along' '
57-
write_script .git/hooks/test-hook <<-EOF &&
58-
exit 1
59-
EOF
60-
61-
test_expect_code 1 git hook run test-hook &&
62-
63-
write_script .git/hooks/test-hook <<-EOF &&
64-
exit 2
65-
EOF
66-
67-
test_expect_code 2 git hook run test-hook &&
68-
69-
write_script .git/hooks/test-hook <<-EOF &&
70-
exit 128
71-
EOF
56+
for code in 1 2 128 129
57+
do
58+
test_expect_success "git hook run: exit code $code is passed along" '
59+
test_hook test-hook <<-EOF &&
60+
exit $code
61+
EOF
7262
73-
test_expect_code 128 git hook run test-hook &&
74-
75-
write_script .git/hooks/test-hook <<-EOF &&
76-
exit 129
77-
EOF
78-
79-
test_expect_code 129 git hook run test-hook
80-
'
63+
test_expect_code $code git hook run test-hook
64+
'
65+
done
8166

8267
test_expect_success 'git hook run arg u ments without -- is not allowed' '
8368
test_expect_code 129 git hook run test-hook arg u ments
8469
'
8570

8671
test_expect_success 'git hook run -- pass arguments' '
87-
write_script .git/hooks/test-hook <<-\EOF &&
72+
test_hook test-hook <<-\EOF &&
8873
echo $1
8974
echo $2
9075
EOF
@@ -99,7 +84,7 @@ test_expect_success 'git hook run -- pass arguments' '
9984
'
10085

10186
test_expect_success 'git hook run -- out-of-repo runs excluded' '
102-
write_script .git/hooks/test-hook <<-EOF &&
87+
test_hook test-hook <<-EOF &&
10388
echo Test hook
10489
EOF
10590
@@ -120,6 +105,10 @@ test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
120105
Hook ran four
121106
EOF
122107
108+
test_hook test-hook <<-EOF &&
109+
echo Test hook
110+
EOF
111+
123112
# Test various ways of specifying the path. See also
124113
# t1350-config-hooks-path.sh
125114
>actual &&

t/t2400-worktree-add.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,7 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' '
559559
'
560560

561561
post_checkout_hook () {
562-
gitdir=${1:-.git}
563-
test_when_finished "rm -f $gitdir/hooks/post-checkout" &&
564-
mkdir -p $gitdir/hooks &&
565-
write_script $gitdir/hooks/post-checkout <<-\EOF
562+
test_hook -C "$1" post-checkout <<-\EOF
566563
{
567564
echo $*
568565
git rev-parse --git-dir --show-toplevel

t/t3404-rebase-interactive.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,7 @@ test_expect_success 'rebase a detached HEAD' '
619619
'
620620

621621
test_expect_success 'rebase a commit violating pre-commit' '
622-
623-
mkdir -p .git/hooks &&
624-
write_script .git/hooks/pre-commit <<-\EOF &&
622+
test_hook pre-commit <<-\EOF &&
625623
test -z "$(git diff --cached --check)"
626624
EOF
627625
echo "monde! " >> file1 &&
@@ -636,8 +634,6 @@ test_expect_success 'rebase a commit violating pre-commit' '
636634
'
637635

638636
test_expect_success 'rebase with a file named HEAD in worktree' '
639-
640-
rm -fr .git/hooks &&
641637
git reset --hard &&
642638
git checkout -b branch3 A &&
643639
@@ -1688,10 +1684,8 @@ test_expect_success 'valid author header when author contains single quote' '
16881684
'
16891685

16901686
test_expect_success 'post-commit hook is called' '
1691-
test_when_finished "rm -f .git/hooks/post-commit" &&
16921687
>actual &&
1693-
mkdir -p .git/hooks &&
1694-
write_script .git/hooks/post-commit <<-\EOS &&
1688+
test_hook post-commit <<-\EOS &&
16951689
git rev-parse HEAD >>actual
16961690
EOS
16971691
(

t/t3412-rebase-root.sh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ test_expect_success 'rebase --root fails with too many args' '
3131
'
3232

3333
test_expect_success 'setup pre-rebase hook' '
34-
mkdir -p .git/hooks &&
35-
cat >.git/hooks/pre-rebase <<EOF &&
36-
#!$SHELL_PATH
37-
echo "\$1,\$2" >.git/PRE-REBASE-INPUT
38-
EOF
39-
chmod +x .git/hooks/pre-rebase
34+
test_hook --setup pre-rebase <<-\EOF
35+
echo "$1,$2" >.git/PRE-REBASE-INPUT
36+
EOF
4037
'
4138
cat > expect <<EOF
4239
4
@@ -141,12 +138,9 @@ commit work7~5
141138
EOF
142139

143140
test_expect_success 'setup pre-rebase hook that fails' '
144-
mkdir -p .git/hooks &&
145-
cat >.git/hooks/pre-rebase <<EOF &&
146-
#!$SHELL_PATH
147-
false
148-
EOF
149-
chmod +x .git/hooks/pre-rebase
141+
test_hook --setup --clobber pre-rebase <<-\EOF
142+
false
143+
EOF
150144
'
151145

152146
test_expect_success 'pre-rebase hook stops rebase' '

t/t3413-rebase-hook.sh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ test_expect_success 'rebase -i' '
4141
'
4242

4343
test_expect_success 'setup pre-rebase hook' '
44-
mkdir -p .git/hooks &&
45-
cat >.git/hooks/pre-rebase <<EOF &&
46-
#!$SHELL_PATH
47-
echo "\$1,\$2" >.git/PRE-REBASE-INPUT
48-
EOF
49-
chmod +x .git/hooks/pre-rebase
44+
test_hook --setup pre-rebase <<-\EOF
45+
echo "$1,$2" >.git/PRE-REBASE-INPUT
46+
EOF
5047
'
5148

5249
test_expect_success 'pre-rebase hook gets correct input (1)' '
@@ -102,12 +99,9 @@ test_expect_success 'pre-rebase hook gets correct input (6)' '
10299
'
103100

104101
test_expect_success 'setup pre-rebase hook that fails' '
105-
mkdir -p .git/hooks &&
106-
cat >.git/hooks/pre-rebase <<EOF &&
107-
#!$SHELL_PATH
108-
false
109-
EOF
110-
chmod +x .git/hooks/pre-rebase
102+
test_hook --setup --clobber pre-rebase <<-\EOF
103+
false
104+
EOF
111105
'
112106

113107
test_expect_success 'pre-rebase hook stops rebase (1)' '

t/t3430-rebase-merges.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ test_expect_success 'post-rewrite hook and fixups work for merges' '
292292
git commit --fixup HEAD same2.t &&
293293
fixup="$(git rev-parse HEAD)" &&
294294
295-
mkdir -p .git/hooks &&
296-
test_when_finished "rm .git/hooks/post-rewrite" &&
297-
echo "cat >actual" | write_script .git/hooks/post-rewrite &&
295+
test_hook post-rewrite <<-\EOF &&
296+
cat >actual
297+
EOF
298298
299299
test_tick &&
300300
git rebase -i --autosquash -r HEAD^^^ &&

0 commit comments

Comments
 (0)