3 test_description
='git-hook command'
5 TEST_PASSES_SANITIZE_LEAK
=true
7 .
"$TEST_DIRECTORY"/lib-terminal.sh
9 test_expect_success
'git hook usage' '
10 test_expect_code 129 git hook &&
11 test_expect_code 129 git hook run &&
12 test_expect_code 129 git hook run -h &&
13 test_expect_code 129 git hook run --unknown 2>err &&
14 grep "unknown option" err
17 test_expect_success
'git hook run: nonexistent hook' '
18 cat >stderr.expect <<-\EOF &&
19 error: cannot find a hook named test-hook
21 test_expect_code 1 git hook run test-hook 2>stderr.actual &&
22 test_cmp stderr.expect stderr.actual
25 test_expect_success
'git hook run: nonexistent hook with --ignore-missing' '
26 git hook run --ignore-missing does-not-exist 2>stderr.actual &&
27 test_must_be_empty stderr.actual
30 test_expect_success
'git hook run: basic' '
31 test_hook test-hook <<-EOF &&
35 cat >expect <<-\EOF &&
38 git hook run test-hook 2>actual &&
39 test_cmp expect actual
42 test_expect_success
'git hook run: stdout and stderr both write to our stderr' '
43 test_hook test-hook <<-EOF &&
44 echo >&1 Will end up on stderr
45 echo >&2 Will end up on stderr
48 cat >stderr.expect <<-\EOF &&
52 git hook run test-hook >stdout.actual 2>stderr.actual &&
53 test_cmp stderr.expect stderr.actual &&
54 test_must_be_empty stdout.actual
57 for code
in 1 2 128 129
59 test_expect_success
"git hook run: exit code $code is passed along" '
60 test_hook test-hook <<-EOF &&
64 test_expect_code $code git hook run test-hook
68 test_expect_success
'git hook run arg u ments without -- is not allowed' '
69 test_expect_code 129 git hook run test-hook arg u ments
72 test_expect_success
'git hook run -- pass arguments' '
73 test_hook test-hook <<-\EOF &&
83 git hook run test-hook -- arg "u ments" 2>actual &&
84 test_cmp expect actual
87 test_expect_success
'git hook run -- out-of-repo runs excluded' '
88 test_hook test-hook <<-EOF &&
92 nongit test_must_fail git hook run test-hook
95 test_expect_success
'git -c core.hooksPath=<PATH> hook run' '
97 write_script my-hooks/test-hook <<-\EOF &&
101 cat >expect <<-\EOF &&
109 test_hook test-hook <<-EOF &&
113 # Test various ways of specifying the path. See also
114 # t1350-config-hooks-path.sh
116 git hook run test-hook -- ignored 2>>actual &&
117 git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
118 git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
119 git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
120 git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
121 test_cmp expect actual
130 test_when_finished "rm -rf repo" &&
133 test_commit -C repo A &&
134 test_commit -C repo B &&
135 git -C repo reset --soft HEAD^ &&
137 test_hook -C repo pre-commit <<-EOF &&
138 test -t 1 && echo STDOUT TTY >>actual || echo STDOUT NO TTY >>actual &&
139 test -t 2 && echo STDERR TTY >>actual || echo STDERR NO TTY >>actual
142 test_terminal git -C repo "$@" &&
143 test_cmp expect repo/actual
146 test_expect_success TTY 'git hook run: stdout and stderr are connected to a TTY' '
147 test_hook_tty hook run pre-commit
150 test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' '
151 test_hook_tty commit -m"B.new"
154 test_expect_success 'git hook run a hook with a bad shebang' '
155 test_when_finished "rm -rf bad-hooks" &&
157 write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
159 test_expect_code 1 git \
160 -c core.hooksPath=bad-hooks \
161 hook run test-hook >out 2>err &&
162 test_must_be_empty out &&
164 # TODO: We should emit the same (or at least a more similar)
165 # error on MINGW (essentially Git for Windows) and all other
166 # platforms.. See the OS-specific code in start_command()
167 grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err
170 test_expect_success 'stdin to hooks' '
171 write_script .git/hooks/test-hook <<-\
EOF &&
177 cat >expect <<-EOF &&
184 git hook run --to-stdin=input test-hook 2>actual &&
185 test_cmp expect actual