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