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