t7503: verify proper hook execution

t7503 did not verify that the expected hooks actually ran during testing. Fix that by making the hook scripts write their $0 into a file so that we can compare actual execution vs. expected execution. While we're at it, do some test style cleanups, such as using write_script() and doing setup inside a test_expect_success block. Improved-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Josh Steadmon committed Aug 7, 2019 at 11:57 UTC f78f6c7e0ca91a48a4b519cc2cf1197d16810f7a
1 file changed +89 -68
t/t7503-pre-commit-hook.sh
+89 -68
@@ -4,124 +4,144 @@ test_description='pre-commit hook'
4
5 . ./test-lib.sh
6
7 -test_expect_success 'with no hook' '
7 +HOOKDIR="$(git rev-parse --git-dir)/hooks"
8 +PRECOMMIT="$HOOKDIR/pre-commit"
9 +
10 +# Prepare sample scripts that write their $0 to actual_hooks
11 +test_expect_success 'sample script setup' '
12 + mkdir -p "$HOOKDIR" &&
13 + write_script "$HOOKDIR/success.sample" <<-\EOF &&
14 + echo $0 >>actual_hooks
15 + exit 0
16 + EOF
17 + write_script "$HOOKDIR/fail.sample" <<-\EOF &&
18 + echo $0 >>actual_hooks
19 + exit 1
20 + EOF
21 + write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
22 + echo $0 >>actual_hooks
23 + exit 1
24 + EOF
25 + chmod -x "$HOOKDIR/non-exec.sample" &&
26 + write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
27 + echo $0 >>actual_hooks
28 + test $GIT_PREFIX = "success/"
29 + EOF
30 + write_script "$HOOKDIR/check-author.sample" <<-\EOF
31 + echo $0 >>actual_hooks
32 + test "$GIT_AUTHOR_NAME" = "New Author" &&
33 + test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
34 + EOF
35 +'
36
9 - echo "foo" > file &&
37 +test_expect_success 'with no hook' '
38 + test_when_finished "rm -f actual_hooks" &&
39 + echo "foo" >file &&
40 git add file &&
11 - git commit -m "first"
12 -
41 + git commit -m "first" &&
42 + test_path_is_missing actual_hooks
43 '
44
45 test_expect_success '--no-verify with no hook' '
16 -
17 - echo "bar" > file &&
46 + test_when_finished "rm -f actual_hooks" &&
47 + echo "bar" >file &&
48 git add file &&
19 - git commit --no-verify -m "bar"
20 -
49 + git commit --no-verify -m "bar" &&
50 + test_path_is_missing actual_hooks
51 '
52
23 -# now install hook that always succeeds
24 -HOOKDIR="$(git rev-parse --git-dir)/hooks"
25 -HOOK="$HOOKDIR/pre-commit"
26 -mkdir -p "$HOOKDIR"
27 -cat > "$HOOK" <<EOF
28 -#!/bin/sh
29 -exit 0
30 -EOF
31 -chmod +x "$HOOK"
32 -
53 test_expect_success 'with succeeding hook' '
34 -
35 - echo "more" >> file &&
54 + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
55 + cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
56 + echo "$PRECOMMIT" >expected_hooks &&
57 + echo "more" >>file &&
58 git add file &&
37 - git commit -m "more"
38 -
59 + git commit -m "more" &&
60 + test_cmp expected_hooks actual_hooks
61 '
62
63 test_expect_success '--no-verify with succeeding hook' '
42 -
43 - echo "even more" >> file &&
64 + test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
65 + cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
66 + echo "even more" >>file &&
67 git add file &&
45 - git commit --no-verify -m "even more"
46 -
68 + git commit --no-verify -m "even more" &&
69 + test_path_is_missing actual_hooks
70 '
71
49 -# now a hook that fails
50 -cat > "$HOOK" <<EOF
51 -#!/bin/sh
52 -exit 1
53 -EOF
54 -
72 test_expect_success 'with failing hook' '
56 -
57 - echo "another" >> file &&
73 + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
74 + cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
75 + echo "$PRECOMMIT" >expected_hooks &&
76 + echo "another" >>file &&
77 git add file &&
59 - test_must_fail git commit -m "another"
60 -
78 + test_must_fail git commit -m "another" &&
79 + test_cmp expected_hooks actual_hooks
80 '
81
82 test_expect_success '--no-verify with failing hook' '
64 -
65 - echo "stuff" >> file &&
83 + test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
84 + cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
85 + echo "stuff" >>file &&
86 git add file &&
67 - git commit --no-verify -m "stuff"
68 -
87 + git commit --no-verify -m "stuff" &&
88 + test_path_is_missing actual_hooks
89 '
90
71 -chmod -x "$HOOK"
91 test_expect_success POSIXPERM 'with non-executable hook' '
73 -
74 - echo "content" >> file &&
92 + test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
93 + cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
94 + echo "content" >>file &&
95 git add file &&
76 - git commit -m "content"
77 -
96 + git commit -m "content" &&
97 + test_path_is_missing actual_hooks
98 '
99
100 test_expect_success POSIXPERM '--no-verify with non-executable hook' '
81 -
82 - echo "more content" >> file &&
101 + test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
102 + cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
103 + echo "more content" >>file &&
104 git add file &&
84 - git commit --no-verify -m "more content"
85 -
105 + git commit --no-verify -m "more content" &&
106 + test_path_is_missing actual_hooks
107 '
87 -chmod +x "$HOOK"
88 -
89 -# a hook that checks $GIT_PREFIX and succeeds inside the
90 -# success/ subdirectory only
91 -cat > "$HOOK" <<EOF
92 -#!/bin/sh
93 -test \$GIT_PREFIX = success/
94 -EOF
108
109 test_expect_success 'with hook requiring GIT_PREFIX' '
97 -
98 - echo "more content" >> file &&
110 + test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
111 + cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
112 + echo "$PRECOMMIT" >expected_hooks &&
113 + echo "more content" >>file &&
114 git add file &&
115 mkdir success &&
116 (
117 cd success &&
118 git commit -m "hook requires GIT_PREFIX = success/"
119 ) &&
105 - rmdir success
120 + test_cmp expected_hooks actual_hooks
121 '
122
123 test_expect_success 'with failing hook requiring GIT_PREFIX' '
109 -
110 - echo "more content" >> file &&
124 + test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" &&
125 + cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
126 + echo "$PRECOMMIT" >expected_hooks &&
127 + echo "more content" >>file &&
128 git add file &&
129 mkdir fail &&
130 (
131 cd fail &&
132 test_must_fail git commit -m "hook must fail"
133 ) &&
117 - rmdir fail &&
118 - git checkout -- file
134 + git checkout -- file &&
135 + test_cmp expected_hooks actual_hooks
136 '
137
138 test_expect_success 'check the author in hook' '
122 - write_script "$HOOK" <<-\EOF &&
123 - test "$GIT_AUTHOR_NAME" = "New Author" &&
124 - test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
139 + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
140 + cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
141 + cat >expected_hooks <<-EOF &&
142 + $PRECOMMIT
143 + $PRECOMMIT
144 + $PRECOMMIT
145 EOF
146 test_must_fail git commit --allow-empty -m "by a.u.thor" &&
147 (
@@ -133,7 +153,8 @@ test_expect_success 'check the author in hook' '
153 ) &&
154 git commit --author="New Author <newauthor@example.com>" \
155 --allow-empty -m "by new.author via command line" &&
136 - git show -s
156 + git show -s &&
157 + test_cmp expected_hooks actual_hooks
158 '
159
160 test_done