Raw
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_grep "\./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_grep "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_grep "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" >out 2>actual &&
133 test_must_be_empty out &&
134 test_cmp expect actual
135 '
136
137 test_expect_success 'run_command runs ungrouped in parallel with more jobs available than tasks' '
138 test-tool run-command --ungroup run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
139 test_line_count = 8 out &&
140 test_line_count = 4 err
141 '
142
143 test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
144 test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
145 test_must_be_empty out &&
146 test_cmp expect actual
147 '
148
149 test_expect_success 'run_command runs ungrouped in parallel with as many jobs as tasks' '
150 test-tool run-command --ungroup run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
151 test_line_count = 8 out &&
152 test_line_count = 4 err
153 '
154
155 test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
156 test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
157 test_must_be_empty out &&
158 test_cmp expect actual
159 '
160
161 test_expect_success 'run_command runs ungrouped in parallel with more tasks than jobs available' '
162 test-tool run-command --ungroup run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
163 test_line_count = 8 out &&
164 test_line_count = 4 err
165 '
166
167 test_expect_success 'run_command listens to stdin' '
168 cat >expect <<-\EOF &&
169 preloaded output of a child
170 listening for stdin:
171 sample stdin 1
172 sample stdin 0
173 preloaded output of a child
174 listening for stdin:
175 sample stdin 1
176 sample stdin 0
177 preloaded output of a child
178 listening for stdin:
179 sample stdin 1
180 sample stdin 0
181 preloaded output of a child
182 listening for stdin:
183 sample stdin 1
184 sample stdin 0
185 EOF
186
187 write_script stdin-script <<-\EOF &&
188 echo "listening for stdin:"
189 while read line
190 do
191 echo "$line"
192 done
193 EOF
194 test-tool run-command run-command-stdin 2 ./stdin-script 2>actual &&
195 test_cmp expect actual
196 '
197
198 cat >expect <<-EOF
199 preloaded output of a child
200 asking for a quick stop
201 preloaded output of a child
202 asking for a quick stop
203 preloaded output of a child
204 asking for a quick stop
205 EOF
206
207 test_expect_success 'run_command is asked to abort gracefully' '
208 test-tool run-command run-command-abort 3 false >out 2>actual &&
209 test_must_be_empty out &&
210 test_cmp expect actual
211 '
212
213 test_expect_success 'run_command is asked to abort gracefully (ungroup)' '
214 test-tool run-command --ungroup run-command-abort 3 false >out 2>err &&
215 test_must_be_empty out &&
216 test_line_count = 6 err
217 '
218
219 cat >expect <<-EOF
220 no further jobs available
221 EOF
222
223 test_expect_success 'run_command outputs ' '
224 test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
225 test_must_be_empty out &&
226 test_cmp expect actual
227 '
228
229 test_expect_success 'run_command outputs (ungroup) ' '
230 test-tool run-command --ungroup run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
231 test_must_be_empty out &&
232 test_cmp expect err
233 '
234
235 test_trace () {
236 expect="$1"
237 shift
238 GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
239 sed -e 's/.* run_command: //' -e '/trace: .*/d' \
240 -e '/RUNTIME_PREFIX requested/d' >actual &&
241 echo "$expect true" >expect &&
242 test_cmp expect actual
243 }
244
245 test_expect_success 'GIT_TRACE with environment variables' '
246 test_trace "abc=1 def=2" env abc=1 env def=2 &&
247 test_trace "abc=2" env abc env abc=1 env abc=2 &&
248 test_trace "abc=2" env abc env abc=2 &&
249 (
250 abc=1 && export abc &&
251 test_trace "def=1" env abc=1 env def=1
252 ) &&
253 (
254 abc=1 && export abc &&
255 test_trace "def=1" env abc env abc=1 env def=1
256 ) &&
257 test_trace "def=1" env non-exist env def=1 &&
258 test_trace "abc=2" env abc=1 env abc env abc=2 &&
259 (
260 abc=1 def=2 && export abc def &&
261 test_trace "unset abc def;" env abc env def
262 ) &&
263 (
264 abc=1 def=2 && export abc def &&
265 test_trace "unset def; abc=3" env abc env def env abc=3
266 ) &&
267 (
268 abc=1 && export abc &&
269 test_trace "unset abc;" env abc=2 env abc
270 )
271 '
272
273 test_expect_success MINGW 'verify curlies are quoted properly' '
274 : force the rev-parse through the MSYS2 Bash &&
275 git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
276 cat >expect <<-\EOF &&
277 --
278 a{b}c
279 EOF
280 test_cmp expect actual
281 '
282
283 test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' '
284 bat="$TRASH_DIRECTORY/bat with spaces in name.bat" &&
285
286 # Every .bat invocation will log its arguments to file "out"
287 rm -f out &&
288 echo "echo %* >>out" >"$bat" &&
289
290 test-tool run-command run-command "$bat" "arg with spaces" &&
291 test_grep "arg with spaces" out
292 '
293
294 test_done