1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.util;
45
46 import static org.junit.Assert.assertEquals;
47
48 import java.io.ByteArrayInputStream;
49 import java.io.ByteArrayOutputStream;
50 import java.io.File;
51 import java.io.IOException;
52 import java.io.InputStream;
53
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.util.FS.ExecutionResult;
56 import org.junit.Before;
57 import org.junit.Test;
58
59 public class RunExternalScriptTest {
60 private static final String LF = "\n";
61
62 private ByteArrayOutputStream out;
63
64 private ByteArrayOutputStream err;
65
66 @Before
67 public void setUp() throws Exception {
68 out = new ByteArrayOutputStream();
69 err = new ByteArrayOutputStream();
70 }
71
72 @Test
73 public void testCopyStdIn() throws IOException, InterruptedException {
74 String inputStr = "a\nb\rc\r\nd";
75 File script = writeTempFile("cat -");
76 int rc = FS.DETECTED.runProcess(
77 new ProcessBuilder("sh", script.getPath()), out, err,
78 new ByteArrayInputStream(inputStr.getBytes()));
79 assertEquals(0, rc);
80 assertEquals(inputStr, new String(out.toByteArray()));
81 assertEquals("", new String(err.toByteArray()));
82 }
83
84 @Test
85 public void testCopyNullStdIn() throws IOException, InterruptedException {
86 File script = writeTempFile("cat -");
87 int rc = FS.DETECTED.runProcess(
88 new ProcessBuilder("sh", script.getPath()), out, err,
89 (InputStream) null);
90 assertEquals(0, rc);
91 assertEquals("", new String(out.toByteArray()));
92 assertEquals("", new String(err.toByteArray()));
93 }
94
95 @Test
96 public void testArguments() throws IOException, InterruptedException {
97 File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6");
98 int rc = FS.DETECTED.runProcess(
99 new ProcessBuilder("sh",
100 script.getPath(), "a", "b", "c"), out, err, (InputStream) null);
101 assertEquals(0, rc);
102 assertEquals("3,a,b,c,,,\n", new String(out.toByteArray()));
103 assertEquals("", new String(err.toByteArray()));
104 }
105
106 @Test
107 public void testRc() throws IOException, InterruptedException {
108 File script = writeTempFile("exit 3");
109 int rc = FS.DETECTED.runProcess(
110 new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
111 out, err, (InputStream) null);
112 assertEquals(3, rc);
113 assertEquals("", new String(out.toByteArray()));
114 assertEquals("", new String(err.toByteArray()));
115 }
116
117 @Test
118 public void testNullStdout() throws IOException, InterruptedException {
119 File script = writeTempFile("echo hi");
120 int rc = FS.DETECTED.runProcess(
121 new ProcessBuilder("sh", script.getPath()), null, err,
122 (InputStream) null);
123 assertEquals(0, rc);
124 assertEquals("", new String(out.toByteArray()));
125 assertEquals("", new String(err.toByteArray()));
126 }
127
128 @Test
129 public void testStdErr() throws IOException, InterruptedException {
130 File script = writeTempFile("echo hi >&2");
131 int rc = FS.DETECTED.runProcess(
132 new ProcessBuilder("sh", script.getPath()), null, err,
133 (InputStream) null);
134 assertEquals(0, rc);
135 assertEquals("", new String(out.toByteArray()));
136 assertEquals("hi" + LF, new String(err.toByteArray()));
137 }
138
139 @Test
140 public void testAllTogetherBin() throws IOException, InterruptedException {
141 String inputStr = "a\nb\rc\r\nd";
142 File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
143 int rc = FS.DETECTED.runProcess(
144 new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
145 out, err, new ByteArrayInputStream(inputStr.getBytes()));
146 assertEquals(5, rc);
147 assertEquals(inputStr, new String(out.toByteArray()));
148 assertEquals("3,a,b,c,,," + LF, new String(err.toByteArray()));
149 }
150
151 @Test(expected = IOException.class)
152 public void testWrongSh() throws IOException, InterruptedException {
153 File script = writeTempFile("cat -");
154 FS.DETECTED.runProcess(
155 new ProcessBuilder("/bin/sh-foo", script.getPath(), "a", "b",
156 "c"), out, err, (InputStream) null);
157 }
158
159 @Test
160 public void testWrongScript() throws IOException, InterruptedException {
161 File script = writeTempFile("cat-foo -");
162 int rc = FS.DETECTED.runProcess(
163 new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
164 out, err, (InputStream) null);
165 assertEquals(127, rc);
166 }
167
168 @Test
169 public void testCopyStdInExecute()
170 throws IOException, InterruptedException {
171 String inputStr = "a\nb\rc\r\nd";
172 File script = writeTempFile("cat -");
173 ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
174 ExecutionResult res = FS.DETECTED.execute(pb,
175 new ByteArrayInputStream(inputStr.getBytes()));
176 assertEquals(0, res.getRc());
177 assertEquals(inputStr, new String(res.getStdout().toByteArray()));
178 assertEquals("", new String(res.getStderr().toByteArray()));
179 }
180
181 @Test
182 public void testStdErrExecute() throws IOException, InterruptedException {
183 File script = writeTempFile("echo hi >&2");
184 ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
185 ExecutionResult res = FS.DETECTED.execute(pb, null);
186 assertEquals(0, res.getRc());
187 assertEquals("", new String(res.getStdout().toByteArray()));
188 assertEquals("hi" + LF, new String(res.getStderr().toByteArray()));
189 }
190
191 @Test
192 public void testAllTogetherBinExecute()
193 throws IOException, InterruptedException {
194 String inputStr = "a\nb\rc\r\nd";
195 File script = writeTempFile(
196 "echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
197 ProcessBuilder pb = new ProcessBuilder("sh", script.getPath(), "a",
198 "b", "c");
199 ExecutionResult res = FS.DETECTED.execute(pb,
200 new ByteArrayInputStream(inputStr.getBytes()));
201 assertEquals(5, res.getRc());
202 assertEquals(inputStr, new String(res.getStdout().toByteArray()));
203 assertEquals("3,a,b,c,,," + LF,
204 new String(res.getStderr().toByteArray()));
205 }
206
207 private File writeTempFile(String body) throws IOException {
208 File f = File.createTempFile("RunProcessTestScript_", "");
209 JGitTestUtil.write(f, body);
210 return f;
211 }
212 }