Git fork
1#!/bin/sh
2#
3# Copyright (c) 2009 Ilari Liusvaara
4#
5
6test_description='Test run command'
7
8. ./test-lib.sh
9
10cat >hello-script <<-EOF
11 #!$SHELL_PATH
12 cat hello-script
13EOF
14
15test_expect_success MINGW 'subprocess inherits only std handles' '
16 test-tool run-command inherited-handle
17'
18
19test_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
24test_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
29test_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
39test_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
46test_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
54test_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
65test_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
78test_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
95test_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
103test_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
116cat >expect <<-EOF
117preloaded output of a child
118Hello
119World
120preloaded output of a child
121Hello
122World
123preloaded output of a child
124Hello
125World
126preloaded output of a child
127Hello
128World
129EOF
130
131test_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
137test_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
143test_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
149test_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
155test_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
161test_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
167cat >expect <<-EOF
168preloaded output of a child
169asking for a quick stop
170preloaded output of a child
171asking for a quick stop
172preloaded output of a child
173asking for a quick stop
174EOF
175
176test_expect_success 'run_command is asked to abort gracefully' '
177 test-tool run-command run-command-abort 3 false >out 2>actual &&
178 test_must_be_empty out &&
179 test_cmp expect actual
180'
181
182test_expect_success 'run_command is asked to abort gracefully (ungroup)' '
183 test-tool run-command --ungroup run-command-abort 3 false >out 2>err &&
184 test_must_be_empty out &&
185 test_line_count = 6 err
186'
187
188cat >expect <<-EOF
189no further jobs available
190EOF
191
192test_expect_success 'run_command outputs ' '
193 test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
194 test_must_be_empty out &&
195 test_cmp expect actual
196'
197
198test_expect_success 'run_command outputs (ungroup) ' '
199 test-tool run-command --ungroup run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
200 test_must_be_empty out &&
201 test_cmp expect err
202'
203
204test_trace () {
205 expect="$1"
206 shift
207 GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
208 sed -e 's/.* run_command: //' -e '/trace: .*/d' \
209 -e '/RUNTIME_PREFIX requested/d' >actual &&
210 echo "$expect true" >expect &&
211 test_cmp expect actual
212}
213
214test_expect_success 'GIT_TRACE with environment variables' '
215 test_trace "abc=1 def=2" env abc=1 env def=2 &&
216 test_trace "abc=2" env abc env abc=1 env abc=2 &&
217 test_trace "abc=2" env abc env abc=2 &&
218 (
219 abc=1 && export abc &&
220 test_trace "def=1" env abc=1 env def=1
221 ) &&
222 (
223 abc=1 && export abc &&
224 test_trace "def=1" env abc env abc=1 env def=1
225 ) &&
226 test_trace "def=1" env non-exist env def=1 &&
227 test_trace "abc=2" env abc=1 env abc env abc=2 &&
228 (
229 abc=1 def=2 && export abc def &&
230 test_trace "unset abc def;" env abc env def
231 ) &&
232 (
233 abc=1 def=2 && export abc def &&
234 test_trace "unset def; abc=3" env abc env def env abc=3
235 ) &&
236 (
237 abc=1 && export abc &&
238 test_trace "unset abc;" env abc=2 env abc
239 )
240'
241
242test_expect_success MINGW 'verify curlies are quoted properly' '
243 : force the rev-parse through the MSYS2 Bash &&
244 git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
245 cat >expect <<-\EOF &&
246 --
247 a{b}c
248 EOF
249 test_cmp expect actual
250'
251
252test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' '
253 bat="$TRASH_DIRECTORY/bat with spaces in name.bat" &&
254
255 # Every .bat invocation will log its arguments to file "out"
256 rm -f out &&
257 echo "echo %* >>out" >"$bat" &&
258
259 # Ask git to invoke .bat; clone will fail due to fake SSH helper
260 test_must_fail env GIT_SSH="$bat" git clone myhost:src ssh-clone &&
261
262 # Spawning .bat can fail if there are two quoted cmd.exe arguments.
263 # .bat itself is first (due to spaces in name), so just one more is
264 # needed to verify. GIT_SSH will invoke .bat multiple times:
265 # 1) -G myhost
266 # 2) myhost "git-upload-pack src"
267 # First invocation will always succeed. Test the second one.
268 grep "git-upload-pack" out
269'
270
271test_done