]>
Commit | Line | Data |
---|---|---|
96e7225b ES |
1 | #!/bin/sh |
2 | ||
3 | test_description='git-hook command' | |
4 | ||
5 | TEST_PASSES_SANITIZE_LEAK=true | |
6 | . ./test-lib.sh | |
a0823453 | 7 | . "$TEST_DIRECTORY"/lib-terminal.sh |
96e7225b ES |
8 | |
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 | |
15 | ' | |
16 | ||
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 | |
23 | ' | |
24 | ||
0d3979c1 ÆAB |
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 | |
28 | ' | |
29 | ||
96e7225b | 30 | test_expect_success 'git hook run: basic' ' |
7da7f63c | 31 | test_hook test-hook <<-EOF && |
96e7225b ES |
32 | echo Test hook |
33 | EOF | |
34 | ||
35 | cat >expect <<-\EOF && | |
36 | Test hook | |
37 | EOF | |
38 | git hook run test-hook 2>actual && | |
39 | test_cmp expect actual | |
40 | ' | |
41 | ||
42 | test_expect_success 'git hook run: stdout and stderr both write to our stderr' ' | |
7da7f63c | 43 | test_hook test-hook <<-EOF && |
96e7225b ES |
44 | echo >&1 Will end up on stderr |
45 | echo >&2 Will end up on stderr | |
46 | EOF | |
47 | ||
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 | |
55 | ' | |
56 | ||
62e2486b ÆAB |
57 | for code in 1 2 128 129 |
58 | do | |
59 | test_expect_success "git hook run: exit code $code is passed along" ' | |
60 | test_hook test-hook <<-EOF && | |
61 | exit $code | |
62 | EOF | |
63 | ||
64 | test_expect_code $code git hook run test-hook | |
65 | ' | |
66 | done | |
96e7225b ES |
67 | |
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 | |
70 | ' | |
71 | ||
72 | test_expect_success 'git hook run -- pass arguments' ' | |
7da7f63c | 73 | test_hook test-hook <<-\EOF && |
96e7225b ES |
74 | echo $1 |
75 | echo $2 | |
76 | EOF | |
77 | ||
78 | cat >expect <<-EOF && | |
79 | arg | |
80 | u ments | |
81 | EOF | |
82 | ||
83 | git hook run test-hook -- arg "u ments" 2>actual && | |
84 | test_cmp expect actual | |
85 | ' | |
86 | ||
87 | test_expect_success 'git hook run -- out-of-repo runs excluded' ' | |
7da7f63c | 88 | test_hook test-hook <<-EOF && |
96e7225b ES |
89 | echo Test hook |
90 | EOF | |
91 | ||
92 | nongit test_must_fail git hook run test-hook | |
93 | ' | |
94 | ||
95 | test_expect_success 'git -c core.hooksPath=<PATH> hook run' ' | |
96 | mkdir my-hooks && | |
97 | write_script my-hooks/test-hook <<-\EOF && | |
035cccf4 | 98 | echo Hook ran $1 |
96e7225b ES |
99 | EOF |
100 | ||
101 | cat >expect <<-\EOF && | |
102 | Test hook | |
103 | Hook ran one | |
104 | Hook ran two | |
105 | Hook ran three | |
106 | Hook ran four | |
107 | EOF | |
108 | ||
7da7f63c ÆAB |
109 | test_hook test-hook <<-EOF && |
110 | echo Test hook | |
111 | EOF | |
112 | ||
96e7225b ES |
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 | |
122 | ' | |
123 | ||
a0823453 ÆAB |
124 | test_hook_tty () { |
125 | cat >expect <<-\EOF | |
126 | STDOUT TTY | |
127 | STDERR TTY | |
128 | EOF | |
129 | ||
130 | test_when_finished "rm -rf repo" && | |
131 | git init repo && | |
132 | ||
133 | test_commit -C repo A && | |
134 | test_commit -C repo B && | |
135 | git -C repo reset --soft HEAD^ && | |
136 | ||
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 | |
140 | EOF | |
141 | ||
142 | test_terminal git -C repo "$@" && | |
143 | test_cmp expect repo/actual | |
144 | } | |
145 | ||
146 | test_expect_success TTY 'git hook run: stdout and stderr are connected to a TTY' ' | |
147 | test_hook_tty hook run pre-commit | |
148 | ' | |
149 | ||
150 | test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' | |
151 | test_hook_tty commit -m"B.new" | |
152 | ' | |
153 | ||
99ddc246 ÆAB |
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 && | |
158 | ||
99ddc246 ÆAB |
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 && | |
6b6fe8b4 RS |
163 | |
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() | |
6d224ac2 | 167 | grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err |
99ddc246 ÆAB |
168 | ' |
169 | ||
0414b389 ES |
170 | test_expect_success 'stdin to hooks' ' |
171 | write_script .git/hooks/test-hook <<-\EOF && | |
172 | echo BEGIN stdin | |
173 | cat | |
174 | echo END stdin | |
175 | EOF | |
176 | ||
177 | cat >expect <<-EOF && | |
178 | BEGIN stdin | |
179 | hello | |
180 | END stdin | |
181 | EOF | |
182 | ||
183 | echo hello >input && | |
184 | git hook run --to-stdin=input test-hook 2>actual && | |
185 | test_cmp expect actual | |
186 | ' | |
187 | ||
96e7225b | 188 | test_done |