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.util.ArrayList;
50 import java.util.List;
51
52 import org.eclipse.jgit.junit.JGitTestUtil;
53 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
54 import org.eclipse.jgit.pgm.CLIGitCommand;
55 import org.junit.Before;
56
57 public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
58
59 protected Repository db;
60
61
62 protected File trash;
63
64 @Override
65 @Before
66 public void setUp() throws Exception {
67 super.setUp();
68 db = createWorkRepository();
69 trash = db.getWorkTree();
70 }
71
72 protected String[] execute(String... cmds) throws Exception {
73 List<String> result = new ArrayList<String>(cmds.length);
74 for (String cmd : cmds)
75 result.addAll(CLIGitCommand.execute(cmd, db));
76 return result.toArray(new String[0]);
77 }
78
79 protected File writeTrashFile(final String name, final String data)
80 throws IOException {
81 return JGitTestUtil.writeTrashFile(db, name, data);
82 }
83
84 protected String read(final File file) throws IOException {
85 return JGitTestUtil.read(file);
86 }
87
88 protected void deleteTrashFile(final String name) throws IOException {
89 JGitTestUtil.deleteTrashFile(db, name);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104 protected String[] executeAndPrint(String... cmds) throws Exception {
105 String[] lines = execute(cmds);
106 for (String line : lines) {
107 System.out.println(line);
108 }
109 return lines;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
126 String[] lines = execute(cmds);
127 String cmdString = cmdString(cmds);
128 if (lines.length == 0)
129 System.out.println("\t\tassertTrue(execute(" + cmdString
130 + ").length == 0);");
131 else {
132 System.out
133 .println("\t\tassertArrayOfLinesEquals(new String[] { //");
134 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
135 for (int i=1; i<lines.length; i++) {
136 System.out.println("\", //");
137 System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
138 }
139 System.out.println("\" //");
140 System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
141 }
142 return lines;
143 }
144
145 protected String cmdString(String... cmds) {
146 if (cmds.length == 0)
147 return "";
148 else if (cmds.length == 1)
149 return "\"" + escapeJava(cmds[0]) + "\"";
150 else {
151 StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
152 for (int i=1; i<cmds.length; i++) {
153 sb.append(", ");
154 sb.append(cmdString(cmds[i]));
155 }
156 return sb.toString();
157 }
158 }
159
160 protected String escapeJava(String line) {
161
162 return line.replaceAll("\"", "\\\\\"")
163 .replaceAll("\\\\", "\\\\\\")
164 .replaceAll("\t", "\\\\t");
165 }
166
167 protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
168 assertEquals(toText(expected), toText(actual));
169 }
170
171 private static String toText(String[] lines) {
172 StringBuilder b = new StringBuilder();
173 for (String s : lines) {
174 b.append(s);
175 b.append('\n');
176 }
177 return b.toString();
178 }
179 }