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