run-command: add stdin callback for parallelization
[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 does-not-exist
10         EOF
11         test_expect_code 1 git hook run does-not-exist 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 does-not-exist 2>stderr.actual &&
17         test_must_be_empty stderr.actual
18 '
19
20 test_expect_success 'git hook run -- basic' '
21         write_script .git/hooks/test-hook <<-EOF &&
22         echo Test hook
23         EOF
24
25         cat >expect <<-\EOF &&
26         Test hook
27         EOF
28         git hook run test-hook 2>actual &&
29         test_cmp expect actual
30 '
31
32 test_expect_success 'git hook run -- stdout and stderr handling' '
33         write_script .git/hooks/test-hook <<-EOF &&
34         echo >&1 Will end up on stderr
35         echo >&2 Will end up on stderr
36         EOF
37
38         cat >stderr.expect <<-\EOF &&
39         Will end up on stderr
40         Will end up on stderr
41         EOF
42         git hook run test-hook >stdout.actual 2>stderr.actual &&
43         test_cmp stderr.expect stderr.actual &&
44         test_must_be_empty stdout.actual
45 '
46
47 test_expect_success 'git hook run -- exit codes are passed along' '
48         write_script .git/hooks/test-hook <<-EOF &&
49         exit 1
50         EOF
51
52         test_expect_code 1 git hook run test-hook &&
53
54         write_script .git/hooks/test-hook <<-EOF &&
55         exit 2
56         EOF
57
58         test_expect_code 2 git hook run test-hook &&
59
60         write_script .git/hooks/test-hook <<-EOF &&
61         exit 128
62         EOF
63
64         test_expect_code 128 git hook run test-hook &&
65
66         write_script .git/hooks/test-hook <<-EOF &&
67         exit 129
68         EOF
69
70         test_expect_code 129 git hook run test-hook
71 '
72
73 test_expect_success 'git hook run arg u ments without -- is not allowed' '
74         test_expect_code 129 git hook run test-hook arg u ments
75 '
76
77 test_expect_success 'git hook run -- pass arguments' '
78         write_script .git/hooks/test-hook <<-\EOF &&
79         echo $1
80         echo $2
81         EOF
82
83         cat >expect <<-EOF &&
84         arg
85         u ments
86         EOF
87
88         git hook run test-hook -- arg "u ments" 2>actual &&
89         test_cmp expect actual
90 '
91
92 test_expect_success 'git hook run -- out-of-repo runs excluded' '
93         write_script .git/hooks/test-hook <<-EOF &&
94         echo Test hook
95         EOF
96
97         nongit test_must_fail git hook run test-hook
98 '
99
100 test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
101         mkdir my-hooks &&
102         write_script my-hooks/test-hook <<-EOF &&
103         echo Hook ran >>actual
104         EOF
105
106         cat >expect <<-\EOF &&
107         Test hook
108         Hook ran
109         Hook ran
110         Hook ran
111         Hook ran
112         EOF
113
114         # Test various ways of specifying the path. See also
115         # t1350-config-hooks-path.sh
116         >actual &&
117         git hook run test-hook 2>>actual &&
118         git -c core.hooksPath=my-hooks hook run test-hook 2>>actual &&
119         git -c core.hooksPath=my-hooks/ hook run test-hook 2>>actual &&
120         git -c core.hooksPath="$PWD/my-hooks" hook run test-hook 2>>actual &&
121         git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook 2>>actual &&
122         test_cmp expect actual
123 '
124
125 test_expect_success 'set up a pre-commit hook in core.hooksPath' '
126         >actual &&
127         mkdir -p .git/custom-hooks .git/hooks &&
128         write_script .git/custom-hooks/pre-commit <<-\EOF &&
129         echo CUSTOM >>actual
130         EOF
131         write_script .git/hooks/pre-commit <<-\EOF
132         echo NORMAL >>actual
133         EOF
134 '
135
136 test_expect_success 'stdin to hooks' '
137         write_script .git/hooks/test-hook <<-\EOF &&
138         echo BEGIN stdin
139         cat
140         echo END stdin
141         EOF
142
143         cat >expect <<-EOF &&
144         BEGIN stdin
145         hello
146         END stdin
147         EOF
148
149         echo hello >input &&
150         git hook run --to-stdin=input test-hook 2>actual &&
151         test_cmp expect actual
152 '
153
154 test_done