Start the 2.46 cycle
[alt-git.git] / t / t1800-hook.sh
blob8b0234cf2d5d96980403d65b844639715acfe91b
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 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 &&
172 echo BEGIN stdin
174 echo END stdin
177 cat >expect <<-EOF &&
178 BEGIN stdin
179 hello
180 END stdin
183 echo hello >input &&
184 git hook run --to-stdin=input test-hook 2>actual &&
185 test_cmp expect actual
188 test_done