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.

CLIRepositoryTestCase.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2012, IBM Corporation and others.
  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.lib;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.file.Path;
  48. import java.util.ArrayList;
  49. import java.util.Arrays;
  50. import java.util.List;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  53. import org.eclipse.jgit.pgm.CLIGitCommand;
  54. import org.eclipse.jgit.pgm.CLIGitCommand.Result;
  55. import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException;
  56. import org.junit.Before;
  57. public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
  58. /** Test repository, initialized for this test case. */
  59. protected Repository db;
  60. /** Working directory of {@link #db}. */
  61. protected File trash;
  62. @Override
  63. @Before
  64. public void setUp() throws Exception {
  65. super.setUp();
  66. db = createWorkRepository();
  67. trash = db.getWorkTree();
  68. }
  69. /**
  70. * Executes specified git commands (with arguments)
  71. *
  72. * @param cmds
  73. * each string argument must be a valid git command line, e.g.
  74. * "git branch -h"
  75. * @return command output
  76. * @throws Exception
  77. */
  78. protected String[] executeUnchecked(String... cmds) throws Exception {
  79. List<String> result = new ArrayList<String>(cmds.length);
  80. for (String cmd : cmds) {
  81. result.addAll(CLIGitCommand.executeUnchecked(cmd, db));
  82. }
  83. return result.toArray(new String[0]);
  84. }
  85. /**
  86. * Executes specified git commands (with arguments), throws exception and
  87. * stops execution on first command which output contains a 'fatal:' error
  88. *
  89. * @param cmds
  90. * each string argument must be a valid git command line, e.g.
  91. * "git branch -h"
  92. * @return command output
  93. * @throws Exception
  94. */
  95. protected String[] execute(String... cmds) throws Exception {
  96. List<String> result = new ArrayList<String>(cmds.length);
  97. for (String cmd : cmds) {
  98. Result r = CLIGitCommand.executeRaw(cmd, db);
  99. if (r.ex instanceof TerminatedByHelpException) {
  100. result.addAll(r.errLines());
  101. } else if (r.ex != null) {
  102. throw r.ex;
  103. }
  104. result.addAll(r.outLines());
  105. }
  106. return result.toArray(new String[0]);
  107. }
  108. /**
  109. * @param link
  110. * the path of the symbolic link to create
  111. * @param target
  112. * the target of the symbolic link
  113. * @return the path to the symbolic link
  114. * @throws Exception
  115. */
  116. protected Path writeLink(String link, String target) throws Exception {
  117. return JGitTestUtil.writeLink(db, link, target);
  118. }
  119. protected File writeTrashFile(final String name, final String data)
  120. throws IOException {
  121. return JGitTestUtil.writeTrashFile(db, name, data);
  122. }
  123. protected String read(final File file) throws IOException {
  124. return JGitTestUtil.read(file);
  125. }
  126. protected void deleteTrashFile(final String name) throws IOException {
  127. JGitTestUtil.deleteTrashFile(db, name);
  128. }
  129. /**
  130. * Execute the given commands and print the output to stdout. Use this
  131. * function instead of the normal {@link #execute(String...)} when preparing
  132. * a test case: the command is executed and then its output is printed on
  133. * stdout, thus making it easier to prepare the correct command and expected
  134. * output for the test case.
  135. *
  136. * @param cmds
  137. * The commands to execute
  138. * @return the result of the command, see {@link #execute(String...)}
  139. * @throws Exception
  140. */
  141. protected String[] executeAndPrint(String... cmds) throws Exception {
  142. String[] lines = execute(cmds);
  143. for (String line : lines) {
  144. System.out.println(line);
  145. }
  146. return lines;
  147. }
  148. /**
  149. * Execute the given commands and print test code comparing expected and
  150. * actual output. Use this function instead of the normal
  151. * {@link #execute(String...)} when preparing a test case: the command is
  152. * executed and test code is generated using the command output as a
  153. * template of what is expected. The code generated is printed on stdout and
  154. * can be pasted in the test case function.
  155. *
  156. * @param cmds
  157. * The commands to execute
  158. * @return the result of the command, see {@link #execute(String...)}
  159. * @throws Exception
  160. */
  161. protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
  162. String[] lines = execute(cmds);
  163. String cmdString = cmdString(cmds);
  164. if (lines.length == 0)
  165. System.out.println("\t\tassertTrue(execute(" + cmdString
  166. + ").length == 0);");
  167. else {
  168. System.out
  169. .println("\t\tassertArrayOfLinesEquals(new String[] { //");
  170. System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
  171. for (int i=1; i<lines.length; i++) {
  172. System.out.println("\", //");
  173. System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
  174. }
  175. System.out.println("\" //");
  176. System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
  177. }
  178. return lines;
  179. }
  180. protected String cmdString(String... cmds) {
  181. if (cmds.length == 0)
  182. return "";
  183. else if (cmds.length == 1)
  184. return "\"" + escapeJava(cmds[0]) + "\"";
  185. else {
  186. StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
  187. for (int i=1; i<cmds.length; i++) {
  188. sb.append(", ");
  189. sb.append(cmdString(cmds[i]));
  190. }
  191. return sb.toString();
  192. }
  193. }
  194. protected String escapeJava(String line) {
  195. // very crude implementation but ok for generating test code
  196. return line.replaceAll("\"", "\\\\\"") //
  197. .replaceAll("\\\\", "\\\\\\")
  198. .replaceAll("\t", "\\\\t");
  199. }
  200. protected void assertStringArrayEquals(String expected, String[] actual) {
  201. // if there is more than one line, ignore last one if empty
  202. assertEquals(1,
  203. actual.length > 1 && actual[actual.length - 1].equals("")
  204. ? actual.length - 1 : actual.length);
  205. assertEquals(expected, actual[0]);
  206. }
  207. protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
  208. assertEquals(toString(expected), toString(actual));
  209. }
  210. public static String toString(String... lines) {
  211. return toString(Arrays.asList(lines));
  212. }
  213. public static String toString(List<String> lines) {
  214. StringBuilder b = new StringBuilder();
  215. for (String s : lines) {
  216. // trim indentation, to simplify tests
  217. s = s.trim();
  218. if (s != null && !s.isEmpty()) {
  219. b.append(s);
  220. b.append('\n');
  221. }
  222. }
  223. // delete last line break to allow simpler tests with one line compare
  224. if (b.length() > 0 && b.charAt(b.length() - 1) == '\n') {
  225. b.deleteCharAt(b.length() - 1);
  226. }
  227. return b.toString();
  228. }
  229. public static boolean contains(List<String> lines, String str) {
  230. for (String s : lines) {
  231. if (s.contains(str)) {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. }