run-command: add stdin callback for parallelization
[git] / t / t0061-run-command.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Ilari Liusvaara
4 #
5
6 test_description='Test run command'
7
8 . ./test-lib.sh
9
10 cat >hello-script <<-EOF
11         #!$SHELL_PATH
12         cat hello-script
13 EOF
14
15 test_expect_success MINGW 'subprocess inherits only std handles' '
16         test-tool run-command inherited-handle
17 '
18
19 test_expect_success 'start_command reports ENOENT (slash)' '
20         test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
21         test_i18ngrep "\./does-not-exist" err
22 '
23
24 test_expect_success 'start_command reports ENOENT (no slash)' '
25         test-tool run-command start-command-ENOENT does-not-exist 2>err &&
26         test_i18ngrep "does-not-exist" err
27 '
28
29 test_expect_success 'run_command can run a command' '
30         cat hello-script >hello.sh &&
31         chmod +x hello.sh &&
32         test-tool run-command run-command ./hello.sh >actual 2>err &&
33
34         test_cmp hello-script actual &&
35         test_must_be_empty err
36 '
37
38
39 test_lazy_prereq RUNS_COMMANDS_FROM_PWD '
40         write_script runs-commands-from-pwd <<-\EOF &&
41         true
42         EOF
43         runs-commands-from-pwd >/dev/null 2>&1
44 '
45
46 test_expect_success !RUNS_COMMANDS_FROM_PWD 'run_command is restricted to PATH' '
47         write_script should-not-run <<-\EOF &&
48         echo yikes
49         EOF
50         test_must_fail test-tool run-command run-command should-not-run 2>err &&
51         test_i18ngrep "should-not-run" err
52 '
53
54 test_expect_success !MINGW 'run_command can run a script without a #! line' '
55         cat >hello <<-\EOF &&
56         cat hello-script
57         EOF
58         chmod +x hello &&
59         test-tool run-command run-command ./hello >actual 2>err &&
60
61         test_cmp hello-script actual &&
62         test_must_be_empty err
63 '
64
65 test_expect_success 'run_command does not try to execute a directory' '
66         test_when_finished "rm -rf bin1 bin2" &&
67         mkdir -p bin1/greet bin2 &&
68         write_script bin2/greet <<-\EOF &&
69         cat bin2/greet
70         EOF
71
72         PATH=$PWD/bin1:$PWD/bin2:$PATH \
73                 test-tool run-command run-command greet >actual 2>err &&
74         test_cmp bin2/greet actual &&
75         test_must_be_empty err
76 '
77
78 test_expect_success POSIXPERM 'run_command passes over non-executable file' '
79         test_when_finished "rm -rf bin1 bin2" &&
80         mkdir -p bin1 bin2 &&
81         write_script bin1/greet <<-\EOF &&
82         cat bin1/greet
83         EOF
84         chmod -x bin1/greet &&
85         write_script bin2/greet <<-\EOF &&
86         cat bin2/greet
87         EOF
88
89         PATH=$PWD/bin1:$PWD/bin2:$PATH \
90                 test-tool run-command run-command greet >actual 2>err &&
91         test_cmp bin2/greet actual &&
92         test_must_be_empty err
93 '
94
95 test_expect_success POSIXPERM 'run_command reports EACCES' '
96         cat hello-script >hello.sh &&
97         chmod -x hello.sh &&
98         test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
99
100         grep "fatal: cannot exec.*hello.sh" err
101 '
102
103 test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
104         mkdir local-command &&
105         test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
106         git config alias.nitfol "!echo frotz" &&
107         chmod a-rx local-command &&
108         (
109                 PATH=./local-command:$PATH &&
110                 git nitfol >actual
111         ) &&
112         echo frotz >expect &&
113         test_cmp expect actual
114 '
115
116 cat >expect <<-EOF
117 preloaded output of a child
118 Hello
119 World
120 preloaded output of a child
121 Hello
122 World
123 preloaded output of a child
124 Hello
125 World
126 preloaded output of a child
127 Hello
128 World
129 EOF
130
131 test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
132         test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
133         test_cmp expect actual
134 '
135
136 test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
137         test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
138         test_cmp expect actual
139 '
140
141 test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
142         test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
143         test_cmp expect actual
144 '
145
146 cat >expect <<-EOF
147 preloaded output of a child
148 listening for stdin:
149 sample stdin 1
150 sample stdin 0
151 preloaded output of a child
152 listening for stdin:
153 sample stdin 1
154 sample stdin 0
155 preloaded output of a child
156 listening for stdin:
157 sample stdin 1
158 sample stdin 0
159 preloaded output of a child
160 listening for stdin:
161 sample stdin 1
162 sample stdin 0
163 EOF
164
165 test_expect_success 'run_command listens to stdin' '
166         write_script stdin-script <<-\EOF &&
167         echo "listening for stdin:"
168         while read line; do
169                 echo "$line"
170         done
171         EOF
172         test-tool run-command run-command-stdin 2 ./stdin-script 2>actual &&
173         test_cmp expect actual
174 '
175
176 cat >expect <<-EOF
177 preloaded output of a child
178 asking for a quick stop
179 preloaded output of a child
180 asking for a quick stop
181 preloaded output of a child
182 asking for a quick stop
183 EOF
184
185 test_expect_success 'run_command is asked to abort gracefully' '
186         test-tool run-command run-command-abort 3 false 2>actual &&
187         test_cmp expect actual
188 '
189
190 cat >expect <<-EOF
191 no further jobs available
192 EOF
193
194 test_expect_success 'run_command outputs ' '
195         test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
196         test_cmp expect actual
197 '
198
199 test_trace () {
200         expect="$1"
201         shift
202         GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
203                 sed -e 's/.* run_command: //' -e '/trace: .*/d' \
204                         -e '/RUNTIME_PREFIX requested/d' >actual &&
205         echo "$expect true" >expect &&
206         test_cmp expect actual
207 }
208
209 test_expect_success 'GIT_TRACE with environment variables' '
210         test_trace "abc=1 def=2" env abc=1 env def=2 &&
211         test_trace "abc=2" env abc env abc=1 env abc=2 &&
212         test_trace "abc=2" env abc env abc=2 &&
213         (
214                 abc=1 && export abc &&
215                 test_trace "def=1" env abc=1 env def=1
216         ) &&
217         (
218                 abc=1 && export abc &&
219                 test_trace "def=1" env abc env abc=1 env def=1
220         ) &&
221         test_trace "def=1" env non-exist env def=1 &&
222         test_trace "abc=2" env abc=1 env abc env abc=2 &&
223         (
224                 abc=1 def=2 && export abc def &&
225                 test_trace "unset abc def;" env abc env def
226         ) &&
227         (
228                 abc=1 def=2 && export abc def &&
229                 test_trace "unset def; abc=3" env abc env def env abc=3
230         ) &&
231         (
232                 abc=1 && export abc &&
233                 test_trace "unset abc;" env abc=2 env abc
234         )
235 '
236
237 test_expect_success MINGW 'verify curlies are quoted properly' '
238         : force the rev-parse through the MSYS2 Bash &&
239         git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
240         cat >expect <<-\EOF &&
241         --
242         a{b}c
243         EOF
244         test_cmp expect actual
245 '
246
247 test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' '
248         bat="$TRASH_DIRECTORY/bat with spaces in name.bat" &&
249
250         # Every .bat invocation will log its arguments to file "out"
251         rm -f out &&
252         echo "echo %* >>out" >"$bat" &&
253
254         # Ask git to invoke .bat; clone will fail due to fake SSH helper
255         test_must_fail env GIT_SSH="$bat" git clone myhost:src ssh-clone &&
256
257         # Spawning .bat can fail if there are two quoted cmd.exe arguments.
258         # .bat itself is first (due to spaces in name), so just one more is
259         # needed to verify. GIT_SSH will invoke .bat multiple times:
260         # 1) -G myhost
261         # 2) myhost "git-upload-pack src"
262         # First invocation will always succeed. Test the second one.
263         grep "git-upload-pack" out
264 '
265
266 test_done