]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1800-hook.sh
clone: allow "--bare" with "-o"
[thirdparty/git.git] / t / t1800-hook.sh
1 #!/bin/sh
2
3 test_description='git-hook command'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
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
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
30 test_expect_success 'git hook run: basic' '
31 test_hook test-hook <<-EOF &&
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' '
43 test_hook test-hook <<-EOF &&
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
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
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' '
73 test_hook test-hook <<-\EOF &&
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' '
88 test_hook test-hook <<-EOF &&
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 &&
98 echo Hook ran $1 >>actual
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
109 test_hook test-hook <<-EOF &&
110 echo Test hook
111 EOF
112
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
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
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
159 # TODO: We should emit the same (or at least a more similar)
160 # error on Windows and !Windows. See the OS-specific code in
161 # start_command()
162 if test_have_prereq !WINDOWS
163 then
164 cat >expect <<-\EOF
165 fatal: cannot run bad-hooks/test-hook: ...
166 EOF
167 else
168 cat >expect <<-\EOF
169 error: cannot spawn bad-hooks/test-hook: ...
170 EOF
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
178 '
179
180 test_done