Merge branch 'ds/reprepare-alternates-when-repreparing-packfiles'
[alt-git.git] / t / t1800-hook.sh
blob3506f627b6cf20f11e4cfd78b309e9e9acc07148
1 #!/bin/sh
3 test_description='git-hook command'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
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
20 EOF
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 &&
32 echo Test hook
33 EOF
35 cat >expect <<-\EOF &&
36 Test hook
37 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
46 EOF
48 cat >stderr.expect <<-\EOF &&
49 Will end up on stderr
50 Will end up on stderr
51 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 &&
61 exit $code
62 EOF
64 test_expect_code $code git hook run test-hook
66 done
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 &&
74 echo $1
75 echo $2
76 EOF
78 cat >expect <<-EOF &&
79 arg
80 u ments
81 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 &&
89 echo Test hook
90 EOF
92 nongit test_must_fail git hook run test-hook
95 test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
96 mkdir my-hooks &&
97 write_script my-hooks/test-hook <<-\EOF &&
98 echo Hook ran $1
99 EOF
101 cat >expect <<-\EOF &&
102 Test hook
103 Hook ran one
104 Hook ran two
105 Hook ran three
106 Hook ran four
109 test_hook test-hook <<-EOF &&
110 echo Test hook
113 # Test various ways of specifying the path. See also
114 # t1350-config-hooks-path.sh
115 >actual &&
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
124 test_hook_tty () {
125 cat >expect <<-\EOF
126 STDOUT TTY
127 STDERR TTY
130 test_when_finished "rm -rf repo" &&
131 git init 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" &&
156 mkdir bad-hooks &&
157 write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
159 # TODO: We should emit the same (or at least a more similar)
160 # error on MINGW (essentially Git for Windows) and all other
161 # platforms.. See the OS-specific code in start_command()
162 if test_have_prereq !MINGW
163 then
164 cat >expect <<-\EOF
165 fatal: cannot run bad-hooks/test-hook: ...
167 else
168 cat >expect <<-\EOF
169 error: cannot spawn bad-hooks/test-hook: ...
171 fi &&
172 test_expect_code 1 git \
173 -c core.hooksPath=bad-hooks \
174 hook run test-hook >out 2>err &&
175 test_must_be_empty out &&
176 sed -e "s/test-hook: .*/test-hook: .../" <err >actual &&
177 test_cmp expect actual
180 test_expect_success 'stdin to hooks' '
181 write_script .git/hooks/test-hook <<-\EOF &&
182 echo BEGIN stdin
184 echo END stdin
187 cat >expect <<-EOF &&
188 BEGIN stdin
189 hello
190 END stdin
193 echo hello >input &&
194 git hook run --to-stdin=input test-hook 2>actual &&
195 test_cmp expect actual
198 test_done