aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RunExternalScriptTest.java
blob: 2d38c05c76e6969b777cabfe7265e5ce8969a4d0 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2015, Christian Halstrick <christian.halstrick@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.util;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.util.FS.ExecutionResult;
import org.junit.Before;
import org.junit.Test;

public class RunExternalScriptTest {
	private static final String LF = "\n";

	private ByteArrayOutputStream out;

	private ByteArrayOutputStream err;

	@Before
	public void setUp() throws Exception {
		out = new ByteArrayOutputStream();
		err = new ByteArrayOutputStream();
	}

	@Test
	public void testCopyStdIn() throws IOException, InterruptedException {
		String inputStr = "a\nb\rc\r\nd";
		File script = writeTempFile("cat -");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath()), out, err,
				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
		assertEquals(0, rc);
		assertEquals(inputStr, new String(out.toByteArray(), UTF_8));
		assertEquals("", new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testCopyNullStdIn() throws IOException, InterruptedException {
		File script = writeTempFile("cat -");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath()), out, err,
				(InputStream) null);
		assertEquals(0, rc);
		assertEquals("", new String(out.toByteArray(), UTF_8));
		assertEquals("", new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testArguments() throws IOException, InterruptedException {
		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh",
				script.getPath(), "a", "b", "c"), out, err, (InputStream) null);
		assertEquals(0, rc);
		assertEquals("3,a,b,c,,,\n", new String(out.toByteArray(), UTF_8));
		assertEquals("", new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testRc() throws IOException, InterruptedException {
		File script = writeTempFile("exit 3");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
				out, err, (InputStream) null);
		assertEquals(3, rc);
		assertEquals("", new String(out.toByteArray(), UTF_8));
		assertEquals("", new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testNullStdout() throws IOException, InterruptedException {
		File script = writeTempFile("echo hi");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath()), null, err,
				(InputStream) null);
		assertEquals(0, rc);
		assertEquals("", new String(out.toByteArray(), UTF_8));
		assertEquals("", new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testStdErr() throws IOException, InterruptedException {
		File script = writeTempFile("echo hi >&2");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath()), null, err,
				(InputStream) null);
		assertEquals(0, rc);
		assertEquals("", new String(out.toByteArray(), UTF_8));
		assertEquals("hi" + LF, new String(err.toByteArray(), UTF_8));
	}

	@Test
	public void testAllTogetherBin() throws IOException, InterruptedException {
		String inputStr = "a\nb\rc\r\nd";
		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
				out, err, new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
		assertEquals(5, rc);
		assertEquals(inputStr, new String(out.toByteArray(), UTF_8));
		assertEquals("3,a,b,c,,," + LF, new String(err.toByteArray(), UTF_8));
	}

	@Test(expected = IOException.class)
	public void testWrongSh() throws IOException, InterruptedException {
		File script = writeTempFile("cat -");
		FS.DETECTED.runProcess(
				new ProcessBuilder("/bin/sh-foo", script.getPath(), "a", "b",
						"c"), out, err, (InputStream) null);
	}

	@Test
	public void testWrongScript() throws IOException, InterruptedException {
		File script = writeTempFile("cat-foo -");
		int rc = FS.DETECTED.runProcess(
				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
				out, err, (InputStream) null);
		assertEquals(127, rc);
	}

	@Test
	public void testCopyStdInExecute()
			throws IOException, InterruptedException {
		String inputStr = "a\nb\rc\r\nd";
		File script = writeTempFile("cat -");
		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
		ExecutionResult res = FS.DETECTED.execute(pb,
				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
		assertEquals(0, res.getRc());
		assertEquals(inputStr,
				new String(res.getStdout().toByteArray(), UTF_8));
		assertEquals("", new String(res.getStderr().toByteArray(), UTF_8));
	}

	@Test
	public void testStdErrExecute() throws IOException, InterruptedException {
		File script = writeTempFile("echo hi >&2");
		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
		ExecutionResult res = FS.DETECTED.execute(pb, null);
		assertEquals(0, res.getRc());
		assertEquals("", new String(res.getStdout().toByteArray(), UTF_8));
		assertEquals("hi" + LF,
				new String(res.getStderr().toByteArray(), UTF_8));
	}

	@Test
	public void testAllTogetherBinExecute()
			throws IOException, InterruptedException {
		String inputStr = "a\nb\rc\r\nd";
		File script = writeTempFile(
				"echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath(), "a",
				"b", "c");
		ExecutionResult res = FS.DETECTED.execute(pb,
				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
		assertEquals(5, res.getRc());
		assertEquals(inputStr,
				new String(res.getStdout().toByteArray(), UTF_8));
		assertEquals("3,a,b,c,,," + LF,
				new String(res.getStderr().toByteArray(), UTF_8));
	}

	private File writeTempFile(String body) throws IOException {
		File f = File.createTempFile("RunProcessTestScript_", "");
		JGitTestUtil.write(f, body);
		return f;
	}
}