1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lib;
11
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.eclipse.jgit.junit.JGitTestUtil;
22 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
23 import org.eclipse.jgit.pgm.CLIGitCommand;
24 import org.eclipse.jgit.pgm.CLIGitCommand.Result;
25 import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException;
26 import org.junit.Before;
27
28 public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
29
30 protected Repository db;
31
32 @Override
33 @Before
34 public void setUp() throws Exception {
35 super.setUp();
36 db = createWorkRepository();
37 }
38
39
40
41
42
43
44
45
46
47
48 protected String[] executeUnchecked(String... cmds) throws Exception {
49 List<String> result = new ArrayList<>(cmds.length);
50 for (String cmd : cmds) {
51 result.addAll(CLIGitCommand.executeUnchecked(cmd, db));
52 }
53 return result.toArray(new String[0]);
54 }
55
56
57
58
59
60
61
62
63
64
65
66 protected String[] execute(String... cmds) throws Exception {
67 List<String> result = new ArrayList<>(cmds.length);
68 for (String cmd : cmds) {
69 Result r = CLIGitCommand.executeRaw(cmd, db);
70 if (r.ex instanceof TerminatedByHelpException) {
71 result.addAll(r.errLines());
72 } else if (r.ex != null) {
73 throw r.ex;
74 }
75 result.addAll(r.outLines());
76 }
77 return result.toArray(new String[0]);
78 }
79
80
81
82
83
84
85
86
87
88 protected Path writeLink(String link, String target) throws Exception {
89 return JGitTestUtil.writeLink(db, link, target);
90 }
91
92 protected File writeTrashFile(String name, String data)
93 throws IOException {
94 return JGitTestUtil.writeTrashFile(db, name, data);
95 }
96
97 @Override
98 protected String read(File file) throws IOException {
99 return JGitTestUtil.read(file);
100 }
101
102 protected void deleteTrashFile(String name) throws IOException {
103 JGitTestUtil.deleteTrashFile(db, name);
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118 protected String[] executeAndPrint(String... cmds) throws Exception {
119 String[] lines = execute(cmds);
120 for (String line : lines) {
121 System.out.println(line);
122 }
123 return lines;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
140 String[] lines = execute(cmds);
141 String cmdString = cmdString(cmds);
142 if (lines.length == 0)
143 System.out.println("\t\tassertTrue(execute(" + cmdString
144 + ").length == 0);");
145 else {
146 System.out
147 .println("\t\tassertArrayOfLinesEquals(new String[] { //");
148 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
149 for (int i=1; i<lines.length; i++) {
150 System.out.println("\", //");
151 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
152 }
153 System.out.println("\" //");
154 System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
155 }
156 return lines;
157 }
158
159 protected String cmdString(String... cmds) {
160 switch (cmds.length) {
161 case 0:
162 return "";
163 case 1:
164 return "\"" + escapeJava(cmds[0]) + "\"";
165 default:
166 StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
167 for (int i=1; i<cmds.length; i++) {
168 sb.append(", ");
169 sb.append(cmdString(cmds[i]));
170 }
171 return sb.toString();
172 }
173 }
174
175 protected String escapeJava(String line) {
176
177 return line.replaceAll("\"", "\\\\\"")
178 .replaceAll("\\\\", "\\\\\\")
179 .replaceAll("\t", "\\\\t");
180 }
181
182 protected String shellQuote(String s) {
183 return "'" + s.replace("'", "'\\''") + "'";
184 }
185
186 protected String shellQuote(File f) {
187 return "'" + f.getPath().replace("'", "'\\''") + "'";
188 }
189
190 protected void assertStringArrayEquals(String expected, String[] actual) {
191
192 assertEquals(1,
193 actual.length > 1 && actual[actual.length - 1].isEmpty()
194 ? actual.length - 1 : actual.length);
195 assertEquals(expected, actual[0]);
196 }
197
198 protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
199 assertEquals(toString(expected), toString(actual));
200 }
201
202 public static String toString(String... lines) {
203 return toString(Arrays.asList(lines));
204 }
205
206 public static String toString(List<String> lines) {
207 StringBuilder b = new StringBuilder();
208 for (String s : lines) {
209
210 s = s.trim();
211 if (s != null && !s.isEmpty()) {
212 b.append(s);
213 b.append('\n');
214 }
215 }
216
217 if (b.length() > 0 && b.charAt(b.length() - 1) == '\n') {
218 b.deleteCharAt(b.length() - 1);
219 }
220 return b.toString();
221 }
222
223 public static boolean contains(List<String> lines, String str) {
224 for (String s : lines) {
225 if (s.contains(str)) {
226 return true;
227 }
228 }
229 return false;
230 }
231 }