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 package org.eclipse.jgit.lib;
44
45 import static org.junit.Assert.assertEquals;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.nio.file.Path;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.List;
53
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
56 import org.eclipse.jgit.pgm.CLIGitCommand;
57 import org.eclipse.jgit.pgm.CLIGitCommand.Result;
58 import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException;
59 import org.junit.Before;
60
61 public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
62
63 protected Repository db;
64
65
66 protected File trash;
67
68 @Override
69 @Before
70 public void setUp() throws Exception {
71 super.setUp();
72 db = createWorkRepository();
73 trash = db.getWorkTree();
74 }
75
76
77
78
79
80
81
82
83
84
85 protected String[] executeUnchecked(String... cmds) throws Exception {
86 List<String> result = new ArrayList<String>(cmds.length);
87 for (String cmd : cmds) {
88 result.addAll(CLIGitCommand.executeUnchecked(cmd, db));
89 }
90 return result.toArray(new String[0]);
91 }
92
93
94
95
96
97
98
99
100
101
102
103 protected String[] execute(String... cmds) throws Exception {
104 List<String> result = new ArrayList<String>(cmds.length);
105 for (String cmd : cmds) {
106 Result r = CLIGitCommand.executeRaw(cmd, db);
107 if (r.ex instanceof TerminatedByHelpException) {
108 result.addAll(r.errLines());
109 } else if (r.ex != null) {
110 throw r.ex;
111 }
112 result.addAll(r.outLines());
113 }
114 return result.toArray(new String[0]);
115 }
116
117
118
119
120
121
122
123
124
125 protected Path writeLink(String link, String target) throws Exception {
126 return JGitTestUtil.writeLink(db, link, target);
127 }
128
129 protected File writeTrashFile(final String name, final String data)
130 throws IOException {
131 return JGitTestUtil.writeTrashFile(db, name, data);
132 }
133
134 protected String read(final File file) throws IOException {
135 return JGitTestUtil.read(file);
136 }
137
138 protected void deleteTrashFile(final String name) throws IOException {
139 JGitTestUtil.deleteTrashFile(db, name);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 protected String[] executeAndPrint(String... cmds) throws Exception {
155 String[] lines = execute(cmds);
156 for (String line : lines) {
157 System.out.println(line);
158 }
159 return lines;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
176 String[] lines = execute(cmds);
177 String cmdString = cmdString(cmds);
178 if (lines.length == 0)
179 System.out.println("\t\tassertTrue(execute(" + cmdString
180 + ").length == 0);");
181 else {
182 System.out
183 .println("\t\tassertArrayOfLinesEquals(new String[] { //");
184 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
185 for (int i=1; i<lines.length; i++) {
186 System.out.println("\", //");
187 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
188 }
189 System.out.println("\" //");
190 System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
191 }
192 return lines;
193 }
194
195 protected String cmdString(String... cmds) {
196 if (cmds.length == 0)
197 return "";
198 else if (cmds.length == 1)
199 return "\"" + escapeJava(cmds[0]) + "\"";
200 else {
201 StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
202 for (int i=1; i<cmds.length; i++) {
203 sb.append(", ");
204 sb.append(cmdString(cmds[i]));
205 }
206 return sb.toString();
207 }
208 }
209
210 protected String escapeJava(String line) {
211
212 return line.replaceAll("\"", "\\\\\"")
213 .replaceAll("\\\\", "\\\\\\")
214 .replaceAll("\t", "\\\\t");
215 }
216
217 protected void assertStringArrayEquals(String expected, String[] actual) {
218
219 assertEquals(1,
220 actual.length > 1 && actual[actual.length - 1].equals("")
221 ? actual.length - 1 : actual.length);
222 assertEquals(expected, actual[0]);
223 }
224
225 protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
226 assertEquals(toString(expected), toString(actual));
227 }
228
229 public static String toString(String... lines) {
230 return toString(Arrays.asList(lines));
231 }
232
233 public static String toString(List<String> lines) {
234 StringBuilder b = new StringBuilder();
235 for (String s : lines) {
236
237 s = s.trim();
238 if (s != null && !s.isEmpty()) {
239 b.append(s);
240 b.append('\n');
241 }
242 }
243
244 if (b.length() > 0 && b.charAt(b.length() - 1) == '\n') {
245 b.deleteCharAt(b.length() - 1);
246 }
247 return b.toString();
248 }
249
250 public static boolean contains(List<String> lines, String str) {
251 for (String s : lines) {
252 if (s.contains(str)) {
253 return true;
254 }
255 }
256 return false;
257 }
258 }