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