Merge branch 'nd/style-opening-brace'
[git.git] / t / t0061-run-command.sh
blob99a614bc7c45ca792c7bbce7d16f793ab6107896
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
35 test_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
42 test_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
50 test_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 &&
57 test_cmp hello-script actual &&
58 test_must_be_empty err
61 test_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
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
74 test_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
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
91 test_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 &&
96 grep "fatal: cannot exec.*hello.sh" err
99 test_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 &&
105 PATH=./local-command:$PATH &&
106 git nitfol >actual
107 ) &&
108 echo frotz >expect &&
109 test_cmp expect actual
112 cat >expect <<-EOF
113 preloaded output of a child
114 Hello
115 World
116 preloaded output of a child
117 Hello
118 World
119 preloaded output of a child
120 Hello
121 World
122 preloaded output of a child
123 Hello
124 World
127 test_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
132 test_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
137 test_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
142 cat >expect <<-EOF
143 preloaded output of a child
144 asking for a quick stop
145 preloaded output of a child
146 asking for a quick stop
147 preloaded output of a child
148 asking for a quick stop
151 test_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
156 cat >expect <<-EOF
157 no further jobs available
160 test_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
165 test_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' >actual &&
170 echo "$expect true" >expect &&
171 test_cmp expect actual
174 test_expect_success 'GIT_TRACE with environment variables' '
175 test_trace "abc=1 def=2" env abc=1 env def=2 &&
176 test_trace "abc=2" env abc env abc=1 env abc=2 &&
177 test_trace "abc=2" env abc env abc=2 &&
179 abc=1 && export abc &&
180 test_trace "def=1" env abc=1 env def=1
181 ) &&
183 abc=1 && export abc &&
184 test_trace "def=1" env abc env abc=1 env def=1
185 ) &&
186 test_trace "def=1" env non-exist env def=1 &&
187 test_trace "abc=2" env abc=1 env abc env abc=2 &&
189 abc=1 def=2 && export abc def &&
190 test_trace "unset abc def;" env abc env def
191 ) &&
193 abc=1 def=2 && export abc def &&
194 test_trace "unset def; abc=3" env abc env def env abc=3
195 ) &&
197 abc=1 && export abc &&
198 test_trace "unset abc;" env abc=2 env abc
202 test_done