Imported Upstream version 2.19.0
[platform/upstream/git.git] / t / t0061-run-command.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Ilari Liusvaara
4 #
5
6 test_description='Test run command'
7
8 . ./test-lib.sh
9
10 cat >hello-script <<-EOF
11         #!$SHELL_PATH
12         cat hello-script
13 EOF
14
15 test_expect_success 'start_command reports ENOENT' '
16         test-tool run-command start-command-ENOENT ./does-not-exist
17 '
18
19 test_expect_success 'run_command can run a command' '
20         cat hello-script >hello.sh &&
21         chmod +x hello.sh &&
22         test-tool run-command run-command ./hello.sh >actual 2>err &&
23
24         test_cmp hello-script actual &&
25         test_must_be_empty err
26 '
27
28 test_expect_success !MINGW 'run_command can run a script without a #! line' '
29         cat >hello <<-\EOF &&
30         cat hello-script
31         EOF
32         chmod +x hello &&
33         test-tool run-command run-command ./hello >actual 2>err &&
34
35         test_cmp hello-script actual &&
36         test_must_be_empty err
37 '
38
39 test_expect_success 'run_command does not try to execute a directory' '
40         test_when_finished "rm -rf bin1 bin2" &&
41         mkdir -p bin1/greet bin2 &&
42         write_script bin2/greet <<-\EOF &&
43         cat bin2/greet
44         EOF
45
46         PATH=$PWD/bin1:$PWD/bin2:$PATH \
47                 test-tool run-command run-command greet >actual 2>err &&
48         test_cmp bin2/greet actual &&
49         test_must_be_empty err
50 '
51
52 test_expect_success POSIXPERM 'run_command passes over non-executable file' '
53         test_when_finished "rm -rf bin1 bin2" &&
54         mkdir -p bin1 bin2 &&
55         write_script bin1/greet <<-\EOF &&
56         cat bin1/greet
57         EOF
58         chmod -x bin1/greet &&
59         write_script bin2/greet <<-\EOF &&
60         cat bin2/greet
61         EOF
62
63         PATH=$PWD/bin1:$PWD/bin2:$PATH \
64                 test-tool run-command run-command greet >actual 2>err &&
65         test_cmp bin2/greet actual &&
66         test_must_be_empty err
67 '
68
69 test_expect_success POSIXPERM 'run_command reports EACCES' '
70         cat hello-script >hello.sh &&
71         chmod -x hello.sh &&
72         test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
73
74         grep "fatal: cannot exec.*hello.sh" err
75 '
76
77 test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
78         mkdir local-command &&
79         test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
80         git config alias.nitfol "!echo frotz" &&
81         chmod a-rx local-command &&
82         (
83                 PATH=./local-command:$PATH &&
84                 git nitfol >actual
85         ) &&
86         echo frotz >expect &&
87         test_cmp expect actual
88 '
89
90 cat >expect <<-EOF
91 preloaded output of a child
92 Hello
93 World
94 preloaded output of a child
95 Hello
96 World
97 preloaded output of a child
98 Hello
99 World
100 preloaded output of a child
101 Hello
102 World
103 EOF
104
105 test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
106         test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
107         test_cmp expect actual
108 '
109
110 test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
111         test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
112         test_cmp expect actual
113 '
114
115 test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
116         test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
117         test_cmp expect actual
118 '
119
120 cat >expect <<-EOF
121 preloaded output of a child
122 asking for a quick stop
123 preloaded output of a child
124 asking for a quick stop
125 preloaded output of a child
126 asking for a quick stop
127 EOF
128
129 test_expect_success 'run_command is asked to abort gracefully' '
130         test-tool run-command run-command-abort 3 false 2>actual &&
131         test_cmp expect actual
132 '
133
134 cat >expect <<-EOF
135 no further jobs available
136 EOF
137
138 test_expect_success 'run_command outputs ' '
139         test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
140         test_cmp expect actual
141 '
142
143 test_trace () {
144         expect="$1"
145         shift
146         GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
147                 sed -e 's/.* run_command: //' -e '/trace: .*/d' >actual &&
148         echo "$expect true" >expect &&
149         test_cmp expect actual
150 }
151
152 test_expect_success 'GIT_TRACE with environment variables' '
153         test_trace "abc=1 def=2" env abc=1 env def=2 &&
154         test_trace "abc=2" env abc env abc=1 env abc=2 &&
155         test_trace "abc=2" env abc env abc=2 &&
156         (
157                 abc=1 && export abc &&
158                 test_trace "def=1" env abc=1 env def=1
159         ) &&
160         (
161                 abc=1 && export abc &&
162                 test_trace "def=1" env abc env abc=1 env def=1
163         ) &&
164         test_trace "def=1" env non-exist env def=1 &&
165         test_trace "abc=2" env abc=1 env abc env abc=2 &&
166         (
167                 abc=1 def=2 && export abc def &&
168                 test_trace "unset abc def;" env abc env def
169         ) &&
170         (
171                 abc=1 def=2 && export abc def &&
172                 test_trace "unset def; abc=3" env abc env def env abc=3
173         ) &&
174         (
175                 abc=1 && export abc &&
176                 test_trace "unset abc;" env abc=2 env abc
177         )
178 '
179
180 test_done