Merge branch 'ab/config-based-hooks-base' into seen
[git] / t / t1800-hook.sh
1 #!/bin/bash
2
3 test_description='git-hook command'
4
5 . ./test-lib.sh
6
7 test_expect_success 'git hook run -- nonexistent hook' '
8         cat >stderr.expect <<-\EOF &&
9         error: cannot find a hook named test-hook
10         EOF
11         test_expect_code 1 git hook run test-hook 2>stderr.actual &&
12         test_cmp stderr.expect stderr.actual
13 '
14
15 test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' '
16         git hook run --ignore-missing test-hook 2>stderr.actual &&
17         test_must_be_empty stderr.actual
18 '
19
20 test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' '
21         cat >stderr.expect <<-\EOF &&
22         fatal: the hook '"'"'unknown-hook'"'"' is not known to git, should be in hook-list.h via githooks(5)
23         EOF
24         test_expect_code 128 git hook run unknown-hook 2>stderr.actual &&
25         test_cmp stderr.expect stderr.actual
26 '
27
28 test_expect_success 'git hook run -- basic' '
29         write_script .git/hooks/test-hook <<-EOF &&
30         echo Test hook
31         EOF
32
33         cat >expect <<-\EOF &&
34         Test hook
35         EOF
36         git hook run test-hook 2>actual &&
37         test_cmp expect actual
38 '
39
40 test_expect_success 'git hook run -- stdout and stderr handling' '
41         write_script .git/hooks/test-hook <<-EOF &&
42         echo >&1 Will end up on stderr
43         echo >&2 Will end up on stderr
44         EOF
45
46         cat >stderr.expect <<-\EOF &&
47         Will end up on stderr
48         Will end up on stderr
49         EOF
50         git hook run test-hook >stdout.actual 2>stderr.actual &&
51         test_cmp stderr.expect stderr.actual &&
52         test_must_be_empty stdout.actual
53 '
54
55 test_expect_success 'git hook run -- exit codes are passed along' '
56         write_script .git/hooks/test-hook <<-EOF &&
57         exit 1
58         EOF
59
60         test_expect_code 1 git hook run test-hook &&
61
62         write_script .git/hooks/test-hook <<-EOF &&
63         exit 2
64         EOF
65
66         test_expect_code 2 git hook run test-hook &&
67
68         write_script .git/hooks/test-hook <<-EOF &&
69         exit 128
70         EOF
71
72         test_expect_code 128 git hook run test-hook &&
73
74         write_script .git/hooks/test-hook <<-EOF &&
75         exit 129
76         EOF
77
78         test_expect_code 129 git hook run test-hook
79 '
80
81 test_expect_success 'git hook run arg u ments without -- is not allowed' '
82         test_expect_code 129 git hook run test-hook arg u ments
83 '
84
85 test_expect_success 'git hook run -- pass arguments' '
86         write_script .git/hooks/test-hook <<-\EOF &&
87         echo $1
88         echo $2
89         EOF
90
91         cat >expect <<-EOF &&
92         arg
93         u ments
94         EOF
95
96         git hook run test-hook -- arg "u ments" 2>actual &&
97         test_cmp expect actual
98 '
99
100 test_expect_success 'git hook run -- out-of-repo runs excluded' '
101         write_script .git/hooks/test-hook <<-EOF &&
102         echo Test hook
103         EOF
104
105         nongit test_must_fail git hook run test-hook
106 '
107
108 test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
109         mkdir my-hooks &&
110         write_script my-hooks/test-hook <<-EOF &&
111         echo Hook ran >>actual
112         EOF
113
114         cat >expect <<-\EOF &&
115         Test hook
116         Hook ran
117         Hook ran
118         Hook ran
119         Hook ran
120         EOF
121
122         # Test various ways of specifying the path. See also
123         # t1350-config-hooks-path.sh
124         >actual &&
125         git hook run test-hook 2>>actual &&
126         git -c core.hooksPath=my-hooks hook run test-hook 2>>actual &&
127         git -c core.hooksPath=my-hooks/ hook run test-hook 2>>actual &&
128         git -c core.hooksPath="$PWD/my-hooks" hook run test-hook 2>>actual &&
129         git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook 2>>actual &&
130         test_cmp expect actual
131 '
132
133 test_expect_success 'set up a pre-commit hook in core.hooksPath' '
134         >actual &&
135         mkdir -p .git/custom-hooks .git/hooks &&
136         write_script .git/custom-hooks/pre-commit <<-\EOF &&
137         echo CUSTOM >>actual
138         EOF
139         write_script .git/hooks/pre-commit <<-\EOF
140         echo NORMAL >>actual
141         EOF
142 '
143
144 test_expect_success 'stdin to hooks' '
145         write_script .git/hooks/test-hook <<-\EOF &&
146         echo BEGIN stdin
147         cat
148         echo END stdin
149         EOF
150
151         cat >expect <<-EOF &&
152         BEGIN stdin
153         hello
154         END stdin
155         EOF
156
157         echo hello >input &&
158         git hook run --to-stdin=input test-hook 2>actual &&
159         test_cmp expect actual
160 '
161
162 test_done