Git fork
1#!/bin/sh
2
3test_description='git-hook command'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-terminal.sh
7
8test_expect_success 'git hook usage' '
9 test_expect_code 129 git hook &&
10 test_expect_code 129 git hook run &&
11 test_expect_code 129 git hook run -h &&
12 test_expect_code 129 git hook run --unknown 2>err &&
13 grep "unknown option" err
14'
15
16test_expect_success 'git hook run: nonexistent hook' '
17 cat >stderr.expect <<-\EOF &&
18 error: cannot find a hook named test-hook
19 EOF
20 test_expect_code 1 git hook run test-hook 2>stderr.actual &&
21 test_cmp stderr.expect stderr.actual
22'
23
24test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
25 git hook run --ignore-missing does-not-exist 2>stderr.actual &&
26 test_must_be_empty stderr.actual
27'
28
29test_expect_success 'git hook run: basic' '
30 test_hook test-hook <<-EOF &&
31 echo Test hook
32 EOF
33
34 cat >expect <<-\EOF &&
35 Test hook
36 EOF
37 git hook run test-hook 2>actual &&
38 test_cmp expect actual
39'
40
41test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
42 test_hook test-hook <<-EOF &&
43 echo >&1 Will end up on stderr
44 echo >&2 Will end up on stderr
45 EOF
46
47 cat >stderr.expect <<-\EOF &&
48 Will end up on stderr
49 Will end up on stderr
50 EOF
51 git hook run test-hook >stdout.actual 2>stderr.actual &&
52 test_cmp stderr.expect stderr.actual &&
53 test_must_be_empty stdout.actual
54'
55
56for code in 1 2 128 129
57do
58 test_expect_success "git hook run: exit code $code is passed along" '
59 test_hook test-hook <<-EOF &&
60 exit $code
61 EOF
62
63 test_expect_code $code git hook run test-hook
64 '
65done
66
67test_expect_success 'git hook run arg u ments without -- is not allowed' '
68 test_expect_code 129 git hook run test-hook arg u ments
69'
70
71test_expect_success 'git hook run -- pass arguments' '
72 test_hook test-hook <<-\EOF &&
73 echo $1
74 echo $2
75 EOF
76
77 cat >expect <<-EOF &&
78 arg
79 u ments
80 EOF
81
82 git hook run test-hook -- arg "u ments" 2>actual &&
83 test_cmp expect actual
84'
85
86test_expect_success 'git hook run -- out-of-repo runs excluded' '
87 test_hook test-hook <<-EOF &&
88 echo Test hook
89 EOF
90
91 nongit test_must_fail git hook run test-hook
92'
93
94test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
95 mkdir my-hooks &&
96 write_script my-hooks/test-hook <<-\EOF &&
97 echo Hook ran $1
98 EOF
99
100 cat >expect <<-\EOF &&
101 Test hook
102 Hook ran one
103 Hook ran two
104 Hook ran three
105 Hook ran four
106 EOF
107
108 test_hook test-hook <<-EOF &&
109 echo Test hook
110 EOF
111
112 # Test various ways of specifying the path. See also
113 # t1350-config-hooks-path.sh
114 >actual &&
115 git hook run test-hook -- ignored 2>>actual &&
116 git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
117 git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
118 git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
119 git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
120 test_cmp expect actual
121'
122
123test_hook_tty () {
124 cat >expect <<-\EOF
125 STDOUT TTY
126 STDERR TTY
127 EOF
128
129 test_when_finished "rm -rf repo" &&
130 git init repo &&
131
132 test_commit -C repo A &&
133 test_commit -C repo B &&
134 git -C repo reset --soft HEAD^ &&
135
136 test_hook -C repo pre-commit <<-EOF &&
137 test -t 1 && echo STDOUT TTY >>actual || echo STDOUT NO TTY >>actual &&
138 test -t 2 && echo STDERR TTY >>actual || echo STDERR NO TTY >>actual
139 EOF
140
141 test_terminal git -C repo "$@" &&
142 test_cmp expect repo/actual
143}
144
145test_expect_success TTY 'git hook run: stdout and stderr are connected to a TTY' '
146 test_hook_tty hook run pre-commit
147'
148
149test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' '
150 test_hook_tty commit -m"B.new"
151'
152
153test_expect_success 'git hook run a hook with a bad shebang' '
154 test_when_finished "rm -rf bad-hooks" &&
155 mkdir bad-hooks &&
156 write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
157
158 test_expect_code 1 git \
159 -c core.hooksPath=bad-hooks \
160 hook run test-hook >out 2>err &&
161 test_must_be_empty out &&
162
163 # TODO: We should emit the same (or at least a more similar)
164 # error on MINGW (essentially Git for Windows) and all other
165 # platforms.. See the OS-specific code in start_command()
166 grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err
167'
168
169test_expect_success 'stdin to hooks' '
170 write_script .git/hooks/test-hook <<-\EOF &&
171 echo BEGIN stdin
172 cat
173 echo END stdin
174 EOF
175
176 cat >expect <<-EOF &&
177 BEGIN stdin
178 hello
179 END stdin
180 EOF
181
182 echo hello >input &&
183 git hook run --to-stdin=input test-hook 2>actual &&
184 test_cmp expect actual
185'
186
187test_done