Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2009 Ilari Liusvaara |
| 4 | # |
| 5 | |
| 6 | test_description='Test run command' |
| 7 | |
Ævar Arnfjörð Bjarmason | fdc8f79 | 2021-10-12 15:56:38 +0200 | [diff] [blame] | 8 | TEST_PASSES_SANITIZE_LEAK=true |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 9 | . ./test-lib.sh |
| 10 | |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 11 | cat >hello-script <<-EOF |
| 12 | #!$SHELL_PATH |
| 13 | cat hello-script |
| 14 | EOF |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 15 | |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 16 | test_expect_success MINGW 'subprocess inherits only std handles' ' |
Johannes Schindelin | eea4a7f | 2019-11-22 14:41:02 +0000 | [diff] [blame] | 17 | test-tool run-command inherited-handle |
| 18 | ' |
| 19 | |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 20 | test_expect_success 'start_command reports ENOENT (slash)' ' |
Junio C Hamano | e5a329a | 2018-12-11 14:46:07 +0900 | [diff] [blame] | 21 | test-tool run-command start-command-ENOENT ./does-not-exist 2>err && |
| 22 | test_i18ngrep "\./does-not-exist" err |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 23 | ' |
| 24 | |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 25 | test_expect_success 'start_command reports ENOENT (no slash)' ' |
Junio C Hamano | e5a329a | 2018-12-11 14:46:07 +0900 | [diff] [blame] | 26 | test-tool run-command start-command-ENOENT does-not-exist 2>err && |
| 27 | test_i18ngrep "does-not-exist" err |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 28 | ' |
| 29 | |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 30 | test_expect_success 'run_command can run a command' ' |
| 31 | cat hello-script >hello.sh && |
| 32 | chmod +x hello.sh && |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 33 | test-tool run-command run-command ./hello.sh >actual 2>err && |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 34 | |
| 35 | test_cmp hello-script actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 36 | test_must_be_empty err |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 37 | ' |
| 38 | |
Junio C Hamano | 89ba9a7 | 2018-12-03 10:05:07 +0900 | [diff] [blame] | 39 | |
| 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 |
| 45 | ' |
| 46 | |
| 47 | test_expect_success !RUNS_COMMANDS_FROM_PWD 'run_command is restricted to PATH' ' |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 48 | write_script should-not-run <<-\EOF && |
| 49 | echo yikes |
| 50 | EOF |
Junio C Hamano | e5a329a | 2018-12-11 14:46:07 +0900 | [diff] [blame] | 51 | test_must_fail test-tool run-command run-command should-not-run 2>err && |
| 52 | test_i18ngrep "should-not-run" err |
Jeff King | 321fd82 | 2018-10-24 03:38:00 -0400 | [diff] [blame] | 53 | ' |
| 54 | |
Brandon Williams | c2d3119 | 2017-04-19 16:13:18 -0700 | [diff] [blame] | 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 && |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 60 | test-tool run-command run-command ./hello >actual 2>err && |
Brandon Williams | c2d3119 | 2017-04-19 16:13:18 -0700 | [diff] [blame] | 61 | |
| 62 | test_cmp hello-script actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 63 | test_must_be_empty err |
Brandon Williams | c2d3119 | 2017-04-19 16:13:18 -0700 | [diff] [blame] | 64 | ' |
| 65 | |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 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 |
| 72 | |
| 73 | PATH=$PWD/bin1:$PWD/bin2:$PATH \ |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 74 | test-tool run-command run-command greet >actual 2>err && |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 75 | test_cmp bin2/greet actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 76 | test_must_be_empty err |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 77 | ' |
| 78 | |
| 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 |
| 89 | |
| 90 | PATH=$PWD/bin1:$PWD/bin2:$PATH \ |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 91 | test-tool run-command run-command greet >actual 2>err && |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 92 | test_cmp bin2/greet actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 93 | test_must_be_empty err |
Brandon Williams | 9402831 | 2017-04-25 16:47:00 -0700 | [diff] [blame] | 94 | ' |
| 95 | |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 96 | test_expect_success POSIXPERM 'run_command reports EACCES' ' |
| 97 | cat hello-script >hello.sh && |
| 98 | chmod -x hello.sh && |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 99 | test_must_fail test-tool run-command run-command ./hello.sh 2>err && |
Jonathan Nieder | c0f19bf | 2011-04-20 05:35:08 -0500 | [diff] [blame] | 100 | |
| 101 | grep "fatal: cannot exec.*hello.sh" err |
| 102 | ' |
| 103 | |
Junio C Hamano | eae6953 | 2015-01-16 10:32:09 -0800 | [diff] [blame] | 104 | test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' ' |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 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 && |
| 109 | ( |
| 110 | PATH=./local-command:$PATH && |
| 111 | git nitfol >actual |
| 112 | ) && |
| 113 | echo frotz >expect && |
| 114 | test_cmp expect actual |
| 115 | ' |
| 116 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 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 |
| 130 | EOF |
| 131 | |
| 132 | test_expect_success 'run_command runs in parallel with more jobs available than tasks' ' |
Ævar Arnfjörð Bjarmason | fe004a43 | 2022-10-29 04:59:47 +0200 | [diff] [blame] | 133 | test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual && |
| 134 | test_must_be_empty out && |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 135 | test_cmp expect actual |
| 136 | ' |
| 137 | |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 138 | test_expect_success 'run_command runs ungrouped in parallel with more jobs available than tasks' ' |
| 139 | test-tool run-command --ungroup run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err && |
| 140 | test_line_count = 8 out && |
| 141 | test_line_count = 4 err |
| 142 | ' |
| 143 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 144 | test_expect_success 'run_command runs in parallel with as many jobs as tasks' ' |
Ævar Arnfjörð Bjarmason | fe004a43 | 2022-10-29 04:59:47 +0200 | [diff] [blame] | 145 | test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual && |
| 146 | test_must_be_empty out && |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 147 | test_cmp expect actual |
| 148 | ' |
| 149 | |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 150 | test_expect_success 'run_command runs ungrouped in parallel with as many jobs as tasks' ' |
| 151 | test-tool run-command --ungroup run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err && |
| 152 | test_line_count = 8 out && |
| 153 | test_line_count = 4 err |
| 154 | ' |
| 155 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 156 | test_expect_success 'run_command runs in parallel with more tasks than jobs available' ' |
Ævar Arnfjörð Bjarmason | fe004a43 | 2022-10-29 04:59:47 +0200 | [diff] [blame] | 157 | test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual && |
| 158 | test_must_be_empty out && |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 159 | test_cmp expect actual |
| 160 | ' |
| 161 | |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 162 | test_expect_success 'run_command runs ungrouped in parallel with more tasks than jobs available' ' |
| 163 | test-tool run-command --ungroup run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err && |
| 164 | test_line_count = 8 out && |
| 165 | test_line_count = 4 err |
| 166 | ' |
| 167 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 168 | cat >expect <<-EOF |
| 169 | preloaded output of a child |
| 170 | asking for a quick stop |
| 171 | preloaded output of a child |
| 172 | asking for a quick stop |
| 173 | preloaded output of a child |
| 174 | asking for a quick stop |
| 175 | EOF |
| 176 | |
| 177 | test_expect_success 'run_command is asked to abort gracefully' ' |
Ævar Arnfjörð Bjarmason | fe004a43 | 2022-10-29 04:59:47 +0200 | [diff] [blame] | 178 | test-tool run-command run-command-abort 3 false >out 2>actual && |
| 179 | test_must_be_empty out && |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 180 | test_cmp expect actual |
| 181 | ' |
| 182 | |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 183 | test_expect_success 'run_command is asked to abort gracefully (ungroup)' ' |
| 184 | test-tool run-command --ungroup run-command-abort 3 false >out 2>err && |
| 185 | test_must_be_empty out && |
| 186 | test_line_count = 6 err |
| 187 | ' |
| 188 | |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 189 | cat >expect <<-EOF |
| 190 | no further jobs available |
| 191 | EOF |
| 192 | |
| 193 | test_expect_success 'run_command outputs ' ' |
Ævar Arnfjörð Bjarmason | fe004a43 | 2022-10-29 04:59:47 +0200 | [diff] [blame] | 194 | test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual && |
| 195 | test_must_be_empty out && |
Stefan Beller | c553c72 | 2015-12-15 16:04:10 -0800 | [diff] [blame] | 196 | test_cmp expect actual |
| 197 | ' |
| 198 | |
Ævar Arnfjörð Bjarmason | fd3aaf5 | 2022-06-07 10:48:19 +0200 | [diff] [blame] | 199 | test_expect_success 'run_command outputs (ungroup) ' ' |
| 200 | test-tool run-command --ungroup run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err && |
| 201 | test_must_be_empty out && |
| 202 | test_cmp expect err |
| 203 | ' |
| 204 | |
Nguyễn Thái Ngọc Duy | c61a975 | 2018-01-18 16:45:11 +0700 | [diff] [blame] | 205 | test_trace () { |
| 206 | expect="$1" |
| 207 | shift |
Nguyễn Thái Ngọc Duy | ae6a51f | 2018-03-24 08:44:55 +0100 | [diff] [blame] | 208 | GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \ |
Johannes Schindelin | 06718d4 | 2019-01-29 06:19:36 -0800 | [diff] [blame] | 209 | sed -e 's/.* run_command: //' -e '/trace: .*/d' \ |
| 210 | -e '/RUNTIME_PREFIX requested/d' >actual && |
Nguyễn Thái Ngọc Duy | c61a975 | 2018-01-18 16:45:11 +0700 | [diff] [blame] | 211 | echo "$expect true" >expect && |
| 212 | test_cmp expect actual |
| 213 | } |
| 214 | |
| 215 | test_expect_success 'GIT_TRACE with environment variables' ' |
| 216 | test_trace "abc=1 def=2" env abc=1 env def=2 && |
| 217 | test_trace "abc=2" env abc env abc=1 env abc=2 && |
| 218 | test_trace "abc=2" env abc env abc=2 && |
| 219 | ( |
| 220 | abc=1 && export abc && |
| 221 | test_trace "def=1" env abc=1 env def=1 |
| 222 | ) && |
| 223 | ( |
| 224 | abc=1 && export abc && |
| 225 | test_trace "def=1" env abc env abc=1 env def=1 |
| 226 | ) && |
| 227 | test_trace "def=1" env non-exist env def=1 && |
| 228 | test_trace "abc=2" env abc=1 env abc env abc=2 && |
| 229 | ( |
| 230 | abc=1 def=2 && export abc def && |
| 231 | test_trace "unset abc def;" env abc env def |
| 232 | ) && |
| 233 | ( |
| 234 | abc=1 def=2 && export abc def && |
| 235 | test_trace "unset def; abc=3" env abc env def env abc=3 |
| 236 | ) && |
| 237 | ( |
| 238 | abc=1 && export abc && |
| 239 | test_trace "unset abc;" env abc=2 env abc |
| 240 | ) |
| 241 | ' |
| 242 | |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 243 | test_expect_success MINGW 'verify curlies are quoted properly' ' |
| 244 | : force the rev-parse through the MSYS2 Bash && |
| 245 | git -c alias.r="!git rev-parse" r -- a{b}c >actual && |
| 246 | cat >expect <<-\EOF && |
| 247 | -- |
| 248 | a{b}c |
| 249 | EOF |
| 250 | test_cmp expect actual |
| 251 | ' |
| 252 | |
Alexandr Miloslavskiy | 71f4960 | 2019-10-01 04:41:05 -0700 | [diff] [blame] | 253 | test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' ' |
| 254 | bat="$TRASH_DIRECTORY/bat with spaces in name.bat" && |
| 255 | |
| 256 | # Every .bat invocation will log its arguments to file "out" |
| 257 | rm -f out && |
| 258 | echo "echo %* >>out" >"$bat" && |
| 259 | |
| 260 | # Ask git to invoke .bat; clone will fail due to fake SSH helper |
| 261 | test_must_fail env GIT_SSH="$bat" git clone myhost:src ssh-clone && |
| 262 | |
| 263 | # Spawning .bat can fail if there are two quoted cmd.exe arguments. |
| 264 | # .bat itself is first (due to spaces in name), so just one more is |
| 265 | # needed to verify. GIT_SSH will invoke .bat multiple times: |
| 266 | # 1) -G myhost |
| 267 | # 2) myhost "git-upload-pack src" |
| 268 | # First invocation will always succeed. Test the second one. |
| 269 | grep "git-upload-pack" out |
Johannes Schindelin | eb7c786 | 2019-07-16 07:03:12 -0700 | [diff] [blame] | 270 | ' |
| 271 | |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 272 | test_done |