Skip to content

Commit 66865d1

Browse files
avargitster
authored andcommitted
tests: extend "test_hook" for "rm" and "chmod -x", convert "$HOOK"
Extend the "test_hook" function to take options to disable and remove hooks. Using the wrapper instead of getting the path and running "chmod -x" or "rm" will make it easier to eventually emulate the same behavior with config-based hooks. Not all of these tests need that new mode, but since the rest are either closely related or use the same "$HOOK" pattern let's convert them too. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c36c628 commit 66865d1

8 files changed

+165
-163
lines changed

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/t5543-atomic-push.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,10 @@ test_expect_success 'atomic push obeys update hook preventing a branch to be pus
162162
test_commit two &&
163163
git push --mirror up
164164
) &&
165-
(
166-
cd upstream &&
167-
HOOKDIR="$(git rev-parse --git-dir)/hooks" &&
168-
HOOK="$HOOKDIR/update" &&
169-
mkdir -p "$HOOKDIR" &&
170-
write_script "$HOOK" <<-\EOF
171-
# only allow update to main from now on
172-
test "$1" = "refs/heads/main"
173-
EOF
174-
) &&
165+
test_hook -C upstream update <<-\EOF &&
166+
# only allow update to main from now on
167+
test "$1" = "refs/heads/main"
168+
EOF
175169
(
176170
cd workbench &&
177171
git checkout main &&

t/t5571-pre-push-hook.sh

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

77
. ./test-lib.sh
88

9-
# Setup hook that always succeeds
10-
HOOKDIR="$(git rev-parse --git-dir)/hooks"
11-
HOOK="$HOOKDIR/pre-push"
12-
mkdir -p "$HOOKDIR"
13-
write_script "$HOOK" <<EOF
14-
cat >actual
15-
exit 0
16-
EOF
17-
189
test_expect_success 'setup' '
10+
test_hook pre-push <<-\EOF &&
11+
cat >actual
12+
EOF
13+
1914
git config push.default upstream &&
2015
git init --bare repo1 &&
2116
git remote add parent1 repo1 &&
@@ -28,15 +23,16 @@ test_expect_success 'setup' '
2823
git push parent1 HEAD:foreign &&
2924
test_cmp expect actual
3025
'
31-
write_script "$HOOK" <<EOF
32-
cat >actual
33-
exit 1
34-
EOF
3526

3627
COMMIT1="$(git rev-parse HEAD)"
3728
export COMMIT1
3829

3930
test_expect_success 'push with failing hook' '
31+
test_hook pre-push <<-\EOF &&
32+
cat >actual &&
33+
exit 1
34+
EOF
35+
4036
test_commit two &&
4137
cat >expect <<-EOF &&
4238
HEAD $(git rev-parse HEAD) refs/heads/main $(test_oid zero)
@@ -55,13 +51,13 @@ test_expect_success '--no-verify bypasses hook' '
5551
COMMIT2="$(git rev-parse HEAD)"
5652
export COMMIT2
5753

58-
write_script "$HOOK" <<'EOF'
59-
echo "$1" >actual
60-
echo "$2" >>actual
61-
cat >>actual
62-
EOF
63-
6454
test_expect_success 'push with hook' '
55+
test_hook --setup pre-push <<-\EOF &&
56+
echo "$1" >actual
57+
echo "$2" >>actual
58+
cat >>actual
59+
EOF
60+
6561
cat >expect <<-EOF &&
6662
parent1
6763
repo1
@@ -136,7 +132,9 @@ test_expect_success 'set up many-ref tests' '
136132
'
137133

138134
test_expect_success 'sigpipe does not cause pre-push hook failure' '
139-
echo "exit 0" | write_script "$HOOK" &&
135+
test_hook --clobber pre-push <<-\EOF &&
136+
exit 0
137+
EOF
140138
git push parent1 "refs/heads/b/*:refs/heads/b/*"
141139
'
142140

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,6 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

88
. ./test-lib.sh
99

10-
HOOKDIR="$(git rev-parse --git-dir)/hooks"
11-
PRECOMMIT="$HOOKDIR/pre-commit"
12-
PREMERGE="$HOOKDIR/pre-merge-commit"
13-
14-
# Prepare sample scripts that write their $0 to actual_hooks
15-
test_expect_success 'sample script setup' '
16-
mkdir -p "$HOOKDIR" &&
17-
write_script "$HOOKDIR/success.sample" <<-\EOF &&
18-
echo $0 >>actual_hooks
19-
exit 0
20-
EOF
21-
write_script "$HOOKDIR/fail.sample" <<-\EOF &&
22-
echo $0 >>actual_hooks
23-
exit 1
24-
EOF
25-
write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
26-
echo $0 >>actual_hooks
27-
exit 1
28-
EOF
29-
chmod -x "$HOOKDIR/non-exec.sample" &&
30-
write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
31-
echo $0 >>actual_hooks
32-
test $GIT_PREFIX = "success/"
33-
EOF
34-
write_script "$HOOKDIR/check-author.sample" <<-\EOF
35-
echo $0 >>actual_hooks
36-
test "$GIT_AUTHOR_NAME" = "New Author" &&
37-
test "$GIT_AUTHOR_EMAIL" = "[email protected]"
38-
EOF
39-
'
40-
4110
test_expect_success 'root commit' '
4211
echo "root" >file &&
4312
git add file &&
@@ -96,123 +65,136 @@ test_expect_success '--no-verify with no hook (merge)' '
9665
test_path_is_missing actual_hooks
9766
'
9867

68+
setup_success_hook () {
69+
test_when_finished "rm -f actual_hooks expected_hooks" &&
70+
echo "$1" >expected_hooks &&
71+
test_hook "$1" <<-EOF
72+
echo $1 >>actual_hooks
73+
EOF
74+
}
75+
9976
test_expect_success 'with succeeding hook' '
100-
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
101-
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
102-
echo "$PRECOMMIT" >expected_hooks &&
77+
setup_success_hook "pre-commit" &&
10378
echo "more" >>file &&
10479
git add file &&
10580
git commit -m "more" &&
10681
test_cmp expected_hooks actual_hooks
10782
'
10883

10984
test_expect_success 'with succeeding hook (merge)' '
110-
test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
111-
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
112-
echo "$PREMERGE" >expected_hooks &&
85+
setup_success_hook "pre-merge-commit" &&
11386
git checkout side &&
11487
git merge -m "merge main" main &&
11588
git checkout main &&
11689
test_cmp expected_hooks actual_hooks
11790
'
11891

11992
test_expect_success 'automatic merge fails; both hooks are available' '
120-
test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" &&
121-
test_when_finished "rm -f expected_hooks actual_hooks" &&
122-
test_when_finished "git checkout main" &&
123-
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
124-
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
93+
setup_success_hook "pre-commit" &&
94+
setup_success_hook "pre-merge-commit" &&
12595
12696
git checkout conflicting-a &&
12797
test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
12898
test_path_is_missing actual_hooks &&
12999
130-
echo "$PRECOMMIT" >expected_hooks &&
100+
echo "pre-commit" >expected_hooks &&
131101
echo a+b >conflicting &&
132102
git add conflicting &&
133103
git commit -m "resolve conflict" &&
134104
test_cmp expected_hooks actual_hooks
135105
'
136106

137107
test_expect_success '--no-verify with succeeding hook' '
138-
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
139-
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
108+
setup_success_hook "pre-commit" &&
140109
echo "even more" >>file &&
141110
git add file &&
142111
git commit --no-verify -m "even more" &&
143112
test_path_is_missing actual_hooks
144113
'
145114

146115
test_expect_success '--no-verify with succeeding hook (merge)' '
147-
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
148-
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
116+
setup_success_hook "pre-merge-commit" &&
149117
git branch -f side side-orig &&
150118
git checkout side &&
151119
git merge --no-verify -m "merge main" main &&
152120
git checkout main &&
153121
test_path_is_missing actual_hooks
154122
'
155123

124+
setup_failing_hook () {
125+
test_when_finished "rm -f actual_hooks" &&
126+
test_hook "$1" <<-EOF
127+
echo $1-failing-hook >>actual_hooks
128+
exit 1
129+
EOF
130+
}
131+
156132
test_expect_success 'with failing hook' '
157-
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
158-
cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
159-
echo "$PRECOMMIT" >expected_hooks &&
133+
setup_failing_hook "pre-commit" &&
134+
test_when_finished "rm -f expected_hooks" &&
135+
echo "pre-commit-failing-hook" >expected_hooks &&
136+
160137
echo "another" >>file &&
161138
git add file &&
162139
test_must_fail git commit -m "another" &&
163140
test_cmp expected_hooks actual_hooks
164141
'
165142

166143
test_expect_success '--no-verify with failing hook' '
167-
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
168-
cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
144+
setup_failing_hook "pre-commit" &&
169145
echo "stuff" >>file &&
170146
git add file &&
171147
git commit --no-verify -m "stuff" &&
172148
test_path_is_missing actual_hooks
173149
'
174150

175151
test_expect_success 'with failing hook (merge)' '
176-
test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
177-
cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
178-
echo "$PREMERGE" >expected_hooks &&
152+
setup_failing_hook "pre-merge-commit" &&
153+
echo "pre-merge-commit-failing-hook" >expected_hooks &&
179154
git checkout side &&
180155
test_must_fail git merge -m "merge main" main &&
181156
git checkout main &&
182157
test_cmp expected_hooks actual_hooks
183158
'
184159

185160
test_expect_success '--no-verify with failing hook (merge)' '
186-
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
187-
cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
161+
setup_failing_hook "pre-merge-commit" &&
162+
188163
git branch -f side side-orig &&
189164
git checkout side &&
190165
git merge --no-verify -m "merge main" main &&
191166
git checkout main &&
192167
test_path_is_missing actual_hooks
193168
'
194169

170+
setup_non_exec_hook () {
171+
test_when_finished "rm -f actual_hooks" &&
172+
test_hook "$1" <<-\EOF &&
173+
echo non-exec >>actual_hooks
174+
exit 1
175+
EOF
176+
test_hook --disable "$1"
177+
}
178+
179+
195180
test_expect_success POSIXPERM 'with non-executable hook' '
196-
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
197-
cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
181+
setup_non_exec_hook "pre-commit" &&
198182
echo "content" >>file &&
199183
git add file &&
200184
git commit -m "content" &&
201185
test_path_is_missing actual_hooks
202186
'
203187

204188
test_expect_success POSIXPERM '--no-verify with non-executable hook' '
205-
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
206-
cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
189+
setup_non_exec_hook "pre-commit" &&
207190
echo "more content" >>file &&
208191
git add file &&
209192
git commit --no-verify -m "more content" &&
210193
test_path_is_missing actual_hooks
211194
'
212195

213196
test_expect_success POSIXPERM 'with non-executable hook (merge)' '
214-
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
215-
cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
197+
setup_non_exec_hook "pre-merge" &&
216198
git branch -f side side-orig &&
217199
git checkout side &&
218200
git merge -m "merge main" main &&
@@ -221,19 +203,26 @@ test_expect_success POSIXPERM 'with non-executable hook (merge)' '
221203
'
222204

223205
test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
224-
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
225-
cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
206+
setup_non_exec_hook "pre-merge" &&
226207
git branch -f side side-orig &&
227208
git checkout side &&
228209
git merge --no-verify -m "merge main" main &&
229210
git checkout main &&
230211
test_path_is_missing actual_hooks
231212
'
232213

214+
setup_require_prefix_hook () {
215+
test_when_finished "rm -f expected_hooks" &&
216+
echo require-prefix >expected_hooks &&
217+
test_hook pre-commit <<-\EOF
218+
echo require-prefix >>actual_hooks
219+
test $GIT_PREFIX = "success/"
220+
EOF
221+
}
222+
233223
test_expect_success 'with hook requiring GIT_PREFIX' '
234-
test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
235-
cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
236-
echo "$PRECOMMIT" >expected_hooks &&
224+
test_when_finished "rm -rf actual_hooks success" &&
225+
setup_require_prefix_hook &&
237226
echo "more content" >>file &&
238227
git add file &&
239228
mkdir success &&
@@ -245,9 +234,8 @@ test_expect_success 'with hook requiring GIT_PREFIX' '
245234
'
246235

247236
test_expect_success 'with failing hook requiring GIT_PREFIX' '
248-
test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" &&
249-
cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
250-
echo "$PRECOMMIT" >expected_hooks &&
237+
test_when_finished "rm -rf actual_hooks fail" &&
238+
setup_require_prefix_hook &&
251239
echo "more content" >>file &&
252240
git add file &&
253241
mkdir fail &&
@@ -259,13 +247,23 @@ test_expect_success 'with failing hook requiring GIT_PREFIX' '
259247
test_cmp expected_hooks actual_hooks
260248
'
261249

250+
setup_require_author_hook () {
251+
test_when_finished "rm -f expected_hooks actual_hooks" &&
252+
echo check-author >expected_hooks &&
253+
test_hook pre-commit <<-\EOF
254+
echo check-author >>actual_hooks
255+
test "$GIT_AUTHOR_NAME" = "New Author" &&
256+
test "$GIT_AUTHOR_EMAIL" = "[email protected]"
257+
EOF
258+
}
259+
260+
262261
test_expect_success 'check the author in hook' '
263-
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
264-
cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
262+
setup_require_author_hook &&
265263
cat >expected_hooks <<-EOF &&
266-
$PRECOMMIT
267-
$PRECOMMIT
268-
$PRECOMMIT
264+
check-author
265+
check-author
266+
check-author
269267
EOF
270268
test_must_fail git commit --allow-empty -m "by a.u.thor" &&
271269
(

0 commit comments

Comments
 (0)