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 'start_command reports ENOENT (slash)' '
16 test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
17 test_i18ngrep "\./does-not-exist" err
18'
19
20test_expect_success 'start_command reports ENOENT (no slash)' '
21 test-tool run-command start-command-ENOENT does-not-exist 2>err &&
22 test_i18ngrep "does-not-exist" err
23'
24
25test_expect_success 'run_command can run a command' '
26 cat hello-script >hello.sh &&
27 chmod +x hello.sh &&
28 test-tool run-command run-command ./hello.sh >actual 2>err &&
29
30 test_cmp hello-script actual &&
31 test_must_be_empty err
32'
33
34
35test_lazy_prereq RUNS_COMMANDS_FROM_PWD '
36 write_script runs-commands-from-pwd <<-\EOF &&
37 true
38 EOF
39 runs-commands-from-pwd >/dev/null 2>&1
40'
41
42test_expect_success !RUNS_COMMANDS_FROM_PWD 'run_command is restricted to PATH' '
43 write_script should-not-run <<-\EOF &&
44 echo yikes
45 EOF
46 test_must_fail test-tool run-command run-command should-not-run 2>err &&
47 test_i18ngrep "should-not-run" err
48'
49
50test_expect_success !MINGW 'run_command can run a script without a #! line' '
51 cat >hello <<-\EOF &&
52 cat hello-script
53 EOF
54 chmod +x hello &&
55 test-tool run-command run-command ./hello >actual 2>err &&
56
57 test_cmp hello-script actual &&
58 test_must_be_empty err
59'
60
61test_expect_success 'run_command does not try to execute a directory' '
62 test_when_finished "rm -rf bin1 bin2" &&
63 mkdir -p bin1/greet bin2 &&
64 write_script bin2/greet <<-\EOF &&
65 cat bin2/greet
66 EOF
67
68 PATH=$PWD/bin1:$PWD/bin2:$PATH \
69 test-tool run-command run-command greet >actual 2>err &&
70 test_cmp bin2/greet actual &&
71 test_must_be_empty err
72'
73
74test_expect_success POSIXPERM 'run_command passes over non-executable file' '
75 test_when_finished "rm -rf bin1 bin2" &&
76 mkdir -p bin1 bin2 &&
77 write_script bin1/greet <<-\EOF &&
78 cat bin1/greet
79 EOF
80 chmod -x bin1/greet &&
81 write_script bin2/greet <<-\EOF &&
82 cat bin2/greet
83 EOF
84
85 PATH=$PWD/bin1:$PWD/bin2:$PATH \
86 test-tool run-command run-command greet >actual 2>err &&
87 test_cmp bin2/greet actual &&
88 test_must_be_empty err
89'
90
91test_expect_success POSIXPERM 'run_command reports EACCES' '
92 cat hello-script >hello.sh &&
93 chmod -x hello.sh &&
94 test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
95
96 grep "fatal: cannot exec.*hello.sh" err
97'
98
99test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
100 mkdir local-command &&
101 test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
102 git config alias.nitfol "!echo frotz" &&
103 chmod a-rx local-command &&
104 (
105 PATH=./local-command:$PATH &&
106 git nitfol >actual
107 ) &&
108 echo frotz >expect &&
109 test_cmp expect actual
110'
111
112cat >expect <<-EOF
113preloaded output of a child
114Hello
115World
116preloaded output of a child
117Hello
118World
119preloaded output of a child
120Hello
121World
122preloaded output of a child
123Hello
124World
125EOF
126
127test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
128 test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
129 test_cmp expect actual
130'
131
132test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
133 test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
134 test_cmp expect actual
135'
136
137test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
138 test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
139 test_cmp expect actual
140'
141
142cat >expect <<-EOF
143preloaded output of a child
144asking for a quick stop
145preloaded output of a child
146asking for a quick stop
147preloaded output of a child
148asking for a quick stop
149EOF
150
151test_expect_success 'run_command is asked to abort gracefully' '
152 test-tool run-command run-command-abort 3 false 2>actual &&
153 test_cmp expect actual
154'
155
156cat >expect <<-EOF
157no further jobs available
158EOF
159
160test_expect_success 'run_command outputs ' '
161 test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
162 test_cmp expect actual
163'
164
165test_trace () {
166 expect="$1"
167 shift
168 GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
169 sed -e 's/.* run_command: //' -e '/trace: .*/d' \
170 -e '/RUNTIME_PREFIX requested/d' >actual &&
171 echo "$expect true" >expect &&
172 test_cmp expect actual
173}
174
175test_expect_success 'GIT_TRACE with environment variables' '
176 test_trace "abc=1 def=2" env abc=1 env def=2 &&
177 test_trace "abc=2" env abc env abc=1 env abc=2 &&
178 test_trace "abc=2" env abc env abc=2 &&
179 (
180 abc=1 && export abc &&
181 test_trace "def=1" env abc=1 env def=1
182 ) &&
183 (
184 abc=1 && export abc &&
185 test_trace "def=1" env abc env abc=1 env def=1
186 ) &&
187 test_trace "def=1" env non-exist env def=1 &&
188 test_trace "abc=2" env abc=1 env abc env abc=2 &&
189 (
190 abc=1 def=2 && export abc def &&
191 test_trace "unset abc def;" env abc env def
192 ) &&
193 (
194 abc=1 def=2 && export abc def &&
195 test_trace "unset def; abc=3" env abc env def env abc=3
196 ) &&
197 (
198 abc=1 && export abc &&
199 test_trace "unset abc;" env abc=2 env abc
200 )
201'
202
203test_expect_success MINGW 'verify curlies are quoted properly' '
204 : force the rev-parse through the MSYS2 Bash &&
205 git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
206 cat >expect <<-\EOF &&
207 --
208 a{b}c
209 EOF
210 test_cmp expect actual
211'
212
213test_expect_success MINGW 'can spawn with argv[0] containing spaces' '
214 cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" ./ &&
215 test_must_fail "$PWD/test-fake-ssh$X" 2>err &&
216 grep TRASH_DIRECTORY err
217'
218
219test_done