Merge branch 'jc/run-command-report-exec-failure-fix' into maint
[git.git] / t / t0061-run-command.sh
blob96bf6d6a7d0145334666fcd15373bad6ac66fd67
1 #!/bin/sh
3 # Copyright (c) 2009 Ilari Liusvaara
6 test_description='Test run command'
8 . ./test-lib.sh
10 cat >hello-script <<-EOF
11 #!$SHELL_PATH
12 cat hello-script
13 EOF
15 test_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
20 test_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
25 test_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 &&
30 test_cmp hello-script actual &&
31 test_must_be_empty err
34 test_expect_success 'run_command is restricted to PATH' '
35 write_script should-not-run <<-\EOF &&
36 echo yikes
37 EOF
38 test_must_fail test-tool run-command run-command should-not-run 2>err &&
39 test_i18ngrep "should-not-run" err
42 test_expect_success !MINGW 'run_command can run a script without a #! line' '
43 cat >hello <<-\EOF &&
44 cat hello-script
45 EOF
46 chmod +x hello &&
47 test-tool run-command run-command ./hello >actual 2>err &&
49 test_cmp hello-script actual &&
50 test_must_be_empty err
53 test_expect_success 'run_command does not try to execute a directory' '
54 test_when_finished "rm -rf bin1 bin2" &&
55 mkdir -p bin1/greet bin2 &&
56 write_script bin2/greet <<-\EOF &&
57 cat bin2/greet
58 EOF
60 PATH=$PWD/bin1:$PWD/bin2:$PATH \
61 test-tool run-command run-command greet >actual 2>err &&
62 test_cmp bin2/greet actual &&
63 test_must_be_empty err
66 test_expect_success POSIXPERM 'run_command passes over non-executable file' '
67 test_when_finished "rm -rf bin1 bin2" &&
68 mkdir -p bin1 bin2 &&
69 write_script bin1/greet <<-\EOF &&
70 cat bin1/greet
71 EOF
72 chmod -x bin1/greet &&
73 write_script bin2/greet <<-\EOF &&
74 cat bin2/greet
75 EOF
77 PATH=$PWD/bin1:$PWD/bin2:$PATH \
78 test-tool run-command run-command greet >actual 2>err &&
79 test_cmp bin2/greet actual &&
80 test_must_be_empty err
83 test_expect_success POSIXPERM 'run_command reports EACCES' '
84 cat hello-script >hello.sh &&
85 chmod -x hello.sh &&
86 test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
88 grep "fatal: cannot exec.*hello.sh" err
91 test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
92 mkdir local-command &&
93 test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
94 git config alias.nitfol "!echo frotz" &&
95 chmod a-rx local-command &&
97 PATH=./local-command:$PATH &&
98 git nitfol >actual
99 ) &&
100 echo frotz >expect &&
101 test_cmp expect actual
104 cat >expect <<-EOF
105 preloaded output of a child
106 Hello
107 World
108 preloaded output of a child
109 Hello
110 World
111 preloaded output of a child
112 Hello
113 World
114 preloaded output of a child
115 Hello
116 World
119 test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
120 test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
121 test_cmp expect actual
124 test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
125 test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
126 test_cmp expect actual
129 test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
130 test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
131 test_cmp expect actual
134 cat >expect <<-EOF
135 preloaded output of a child
136 asking for a quick stop
137 preloaded output of a child
138 asking for a quick stop
139 preloaded output of a child
140 asking for a quick stop
143 test_expect_success 'run_command is asked to abort gracefully' '
144 test-tool run-command run-command-abort 3 false 2>actual &&
145 test_cmp expect actual
148 cat >expect <<-EOF
149 no further jobs available
152 test_expect_success 'run_command outputs ' '
153 test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
154 test_cmp expect actual
157 test_trace () {
158 expect="$1"
159 shift
160 GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
161 sed -e 's/.* run_command: //' -e '/trace: .*/d' >actual &&
162 echo "$expect true" >expect &&
163 test_cmp expect actual
166 test_expect_success 'GIT_TRACE with environment variables' '
167 test_trace "abc=1 def=2" env abc=1 env def=2 &&
168 test_trace "abc=2" env abc env abc=1 env abc=2 &&
169 test_trace "abc=2" env abc env abc=2 &&
171 abc=1 && export abc &&
172 test_trace "def=1" env abc=1 env def=1
173 ) &&
175 abc=1 && export abc &&
176 test_trace "def=1" env abc env abc=1 env def=1
177 ) &&
178 test_trace "def=1" env non-exist env def=1 &&
179 test_trace "abc=2" env abc=1 env abc env abc=2 &&
181 abc=1 def=2 && export abc def &&
182 test_trace "unset abc def;" env abc env def
183 ) &&
185 abc=1 def=2 && export abc def &&
186 test_trace "unset def; abc=3" env abc env def env abc=3
187 ) &&
189 abc=1 && export abc &&
190 test_trace "unset abc;" env abc=2 env abc
194 test_done