]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0061-run-command.sh
The sixth batch
[thirdparty/git.git] / t / t0061-run-command.sh
CommitLineData
2b541bf8
JS
1#!/bin/sh
2#
3# Copyright (c) 2009 Ilari Liusvaara
4#
5
6test_description='Test run command'
7
8. ./test-lib.sh
9
c0f19bf3
JN
10cat >hello-script <<-EOF
11 #!$SHELL_PATH
12 cat hello-script
13EOF
c0f19bf3 14
9a780a38 15test_expect_success MINGW 'subprocess inherits only std handles' '
eea4a7f4
JS
16 test-tool run-command inherited-handle
17'
18
321fd823 19test_expect_success 'start_command reports ENOENT (slash)' '
e5a329a2
JH
20 test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
21 test_i18ngrep "\./does-not-exist" err
2b541bf8
JS
22'
23
321fd823 24test_expect_success 'start_command reports ENOENT (no slash)' '
e5a329a2
JH
25 test-tool run-command start-command-ENOENT does-not-exist 2>err &&
26 test_i18ngrep "does-not-exist" err
321fd823
JK
27'
28
c0f19bf3
JN
29test_expect_success 'run_command can run a command' '
30 cat hello-script >hello.sh &&
31 chmod +x hello.sh &&
ae6a51f5 32 test-tool run-command run-command ./hello.sh >actual 2>err &&
c0f19bf3
JN
33
34 test_cmp hello-script actual &&
1c5e94f4 35 test_must_be_empty err
c0f19bf3
JN
36'
37
89ba9a79
JH
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' '
321fd823
JK
47 write_script should-not-run <<-\EOF &&
48 echo yikes
49 EOF
e5a329a2
JH
50 test_must_fail test-tool run-command run-command should-not-run 2>err &&
51 test_i18ngrep "should-not-run" err
321fd823
JK
52'
53
c2d3119d
BW
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 &&
ae6a51f5 59 test-tool run-command run-command ./hello >actual 2>err &&
c2d3119d
BW
60
61 test_cmp hello-script actual &&
1c5e94f4 62 test_must_be_empty err
c2d3119d
BW
63'
64
94028310
BW
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 \
ae6a51f5 73 test-tool run-command run-command greet >actual 2>err &&
94028310 74 test_cmp bin2/greet actual &&
1c5e94f4 75 test_must_be_empty err
94028310
BW
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 \
ae6a51f5 90 test-tool run-command run-command greet >actual 2>err &&
94028310 91 test_cmp bin2/greet actual &&
1c5e94f4 92 test_must_be_empty err
94028310
BW
93'
94
c0f19bf3
JN
95test_expect_success POSIXPERM 'run_command reports EACCES' '
96 cat hello-script >hello.sh &&
97 chmod -x hello.sh &&
ae6a51f5 98 test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
c0f19bf3
JN
99
100 grep "fatal: cannot exec.*hello.sh" err
101'
102
eae69530 103test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
38f865c2
JK
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
c553c72e
SB
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' '
ae6a51f5 132 test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
c553c72e
SB
133 test_cmp expect actual
134'
135
136test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
ae6a51f5 137 test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
c553c72e
SB
138 test_cmp expect actual
139'
140
141test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
ae6a51f5 142 test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
c553c72e
SB
143 test_cmp expect actual
144'
145
146cat >expect <<-EOF
147preloaded output of a child
148asking for a quick stop
149preloaded output of a child
150asking for a quick stop
151preloaded output of a child
152asking for a quick stop
153EOF
154
155test_expect_success 'run_command is asked to abort gracefully' '
ae6a51f5 156 test-tool run-command run-command-abort 3 false 2>actual &&
c553c72e
SB
157 test_cmp expect actual
158'
159
160cat >expect <<-EOF
161no further jobs available
162EOF
163
164test_expect_success 'run_command outputs ' '
ae6a51f5 165 test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
c553c72e
SB
166 test_cmp expect actual
167'
168
c61a975d
NTND
169test_trace () {
170 expect="$1"
171 shift
ae6a51f5 172 GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
06718d4a
JS
173 sed -e 's/.* run_command: //' -e '/trace: .*/d' \
174 -e '/RUNTIME_PREFIX requested/d' >actual &&
c61a975d
NTND
175 echo "$expect true" >expect &&
176 test_cmp expect actual
177}
178
179test_expect_success 'GIT_TRACE with environment variables' '
180 test_trace "abc=1 def=2" env abc=1 env def=2 &&
181 test_trace "abc=2" env abc env abc=1 env abc=2 &&
182 test_trace "abc=2" env abc env abc=2 &&
183 (
184 abc=1 && export abc &&
185 test_trace "def=1" env abc=1 env def=1
186 ) &&
187 (
188 abc=1 && export abc &&
189 test_trace "def=1" env abc env abc=1 env def=1
190 ) &&
191 test_trace "def=1" env non-exist env def=1 &&
192 test_trace "abc=2" env abc=1 env abc env abc=2 &&
193 (
194 abc=1 def=2 && export abc def &&
195 test_trace "unset abc def;" env abc env def
196 ) &&
197 (
198 abc=1 def=2 && export abc def &&
199 test_trace "unset def; abc=3" env abc env def env abc=3
200 ) &&
201 (
202 abc=1 && export abc &&
203 test_trace "unset abc;" env abc=2 env abc
204 )
205'
206
9e9da23c
JS
207test_expect_success MINGW 'verify curlies are quoted properly' '
208 : force the rev-parse through the MSYS2 Bash &&
209 git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
210 cat >expect <<-\EOF &&
211 --
212 a{b}c
213 EOF
214 test_cmp expect actual
215'
216
71f4960b
AM
217test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' '
218 bat="$TRASH_DIRECTORY/bat with spaces in name.bat" &&
219
220 # Every .bat invocation will log its arguments to file "out"
221 rm -f out &&
222 echo "echo %* >>out" >"$bat" &&
223
224 # Ask git to invoke .bat; clone will fail due to fake SSH helper
225 test_must_fail env GIT_SSH="$bat" git clone myhost:src ssh-clone &&
226
227 # Spawning .bat can fail if there are two quoted cmd.exe arguments.
228 # .bat itself is first (due to spaces in name), so just one more is
229 # needed to verify. GIT_SSH will invoke .bat multiple times:
230 # 1) -G myhost
231 # 2) myhost "git-upload-pack src"
232 # First invocation will always succeed. Test the second one.
233 grep "git-upload-pack" out
eb7c7863
JS
234'
235
2b541bf8 236test_done