You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RunExternalScriptTest.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2015, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import org.eclipse.jgit.junit.JGitTestUtil;
  51. import org.eclipse.jgit.util.FS.ExecutionResult;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class RunExternalScriptTest {
  55. private static final String LF = "\n";
  56. private ByteArrayOutputStream out;
  57. private ByteArrayOutputStream err;
  58. @Before
  59. public void setUp() throws Exception {
  60. out = new ByteArrayOutputStream();
  61. err = new ByteArrayOutputStream();
  62. }
  63. @Test
  64. public void testCopyStdIn() throws IOException, InterruptedException {
  65. String inputStr = "a\nb\rc\r\nd";
  66. File script = writeTempFile("cat -");
  67. int rc = FS.DETECTED.runProcess(
  68. new ProcessBuilder("sh", script.getPath()), out, err,
  69. new ByteArrayInputStream(inputStr.getBytes()));
  70. assertEquals(0, rc);
  71. assertEquals(inputStr, new String(out.toByteArray()));
  72. assertEquals("", new String(err.toByteArray()));
  73. }
  74. @Test
  75. public void testCopyNullStdIn() throws IOException, InterruptedException {
  76. File script = writeTempFile("cat -");
  77. int rc = FS.DETECTED.runProcess(
  78. new ProcessBuilder("sh", script.getPath()), out, err,
  79. (InputStream) null);
  80. assertEquals(0, rc);
  81. assertEquals("", new String(out.toByteArray()));
  82. assertEquals("", new String(err.toByteArray()));
  83. }
  84. @Test
  85. public void testArguments() throws IOException, InterruptedException {
  86. File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6");
  87. int rc = FS.DETECTED.runProcess(
  88. new ProcessBuilder("sh",
  89. script.getPath(), "a", "b", "c"), out, err, (InputStream) null);
  90. assertEquals(0, rc);
  91. assertEquals("3,a,b,c,,,\n", new String(out.toByteArray()));
  92. assertEquals("", new String(err.toByteArray()));
  93. }
  94. @Test
  95. public void testRc() throws IOException, InterruptedException {
  96. File script = writeTempFile("exit 3");
  97. int rc = FS.DETECTED.runProcess(
  98. new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
  99. out, err, (InputStream) null);
  100. assertEquals(3, rc);
  101. assertEquals("", new String(out.toByteArray()));
  102. assertEquals("", new String(err.toByteArray()));
  103. }
  104. @Test
  105. public void testNullStdout() throws IOException, InterruptedException {
  106. File script = writeTempFile("echo hi");
  107. int rc = FS.DETECTED.runProcess(
  108. new ProcessBuilder("sh", script.getPath()), null, err,
  109. (InputStream) null);
  110. assertEquals(0, rc);
  111. assertEquals("", new String(out.toByteArray()));
  112. assertEquals("", new String(err.toByteArray()));
  113. }
  114. @Test
  115. public void testStdErr() throws IOException, InterruptedException {
  116. File script = writeTempFile("echo hi >&2");
  117. int rc = FS.DETECTED.runProcess(
  118. new ProcessBuilder("sh", script.getPath()), null, err,
  119. (InputStream) null);
  120. assertEquals(0, rc);
  121. assertEquals("", new String(out.toByteArray()));
  122. assertEquals("hi" + LF, new String(err.toByteArray()));
  123. }
  124. @Test
  125. public void testAllTogetherBin() throws IOException, InterruptedException {
  126. String inputStr = "a\nb\rc\r\nd";
  127. File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
  128. int rc = FS.DETECTED.runProcess(
  129. new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
  130. out, err, new ByteArrayInputStream(inputStr.getBytes()));
  131. assertEquals(5, rc);
  132. assertEquals(inputStr, new String(out.toByteArray()));
  133. assertEquals("3,a,b,c,,," + LF, new String(err.toByteArray()));
  134. }
  135. @Test(expected = IOException.class)
  136. public void testWrongSh() throws IOException, InterruptedException {
  137. File script = writeTempFile("cat -");
  138. FS.DETECTED.runProcess(
  139. new ProcessBuilder("/bin/sh-foo", script.getPath(), "a", "b",
  140. "c"), out, err, (InputStream) null);
  141. }
  142. @Test
  143. public void testWrongScript() throws IOException, InterruptedException {
  144. File script = writeTempFile("cat-foo -");
  145. int rc = FS.DETECTED.runProcess(
  146. new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
  147. out, err, (InputStream) null);
  148. assertEquals(127, rc);
  149. }
  150. @Test
  151. public void testCopyStdInExecute()
  152. throws IOException, InterruptedException {
  153. String inputStr = "a\nb\rc\r\nd";
  154. File script = writeTempFile("cat -");
  155. ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
  156. ExecutionResult res = FS.DETECTED.execute(pb,
  157. new ByteArrayInputStream(inputStr.getBytes()));
  158. assertEquals(0, res.getRc());
  159. assertEquals(inputStr, new String(res.getStdout().toByteArray()));
  160. assertEquals("", new String(res.getStderr().toByteArray()));
  161. }
  162. @Test
  163. public void testStdErrExecute() throws IOException, InterruptedException {
  164. File script = writeTempFile("echo hi >&2");
  165. ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
  166. ExecutionResult res = FS.DETECTED.execute(pb, null);
  167. assertEquals(0, res.getRc());
  168. assertEquals("", new String(res.getStdout().toByteArray()));
  169. assertEquals("hi" + LF, new String(res.getStderr().toByteArray()));
  170. }
  171. @Test
  172. public void testAllTogetherBinExecute()
  173. throws IOException, InterruptedException {
  174. String inputStr = "a\nb\rc\r\nd";
  175. File script = writeTempFile(
  176. "echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
  177. ProcessBuilder pb = new ProcessBuilder("sh", script.getPath(), "a",
  178. "b", "c");
  179. ExecutionResult res = FS.DETECTED.execute(pb,
  180. new ByteArrayInputStream(inputStr.getBytes()));
  181. assertEquals(5, res.getRc());
  182. assertEquals(inputStr, new String(res.getStdout().toByteArray()));
  183. assertEquals("3,a,b,c,,," + LF,
  184. new String(res.getStderr().toByteArray()));
  185. }
  186. private File writeTempFile(String body) throws IOException {
  187. File f = File.createTempFile("RunProcessTestScript_", "");
  188. JGitTestUtil.write(f, body);
  189. return f;
  190. }
  191. }