-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitletPublicTest.java
239 lines (219 loc) · 7.44 KB
/
GitletPublicTest.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Test;
/**
* Class that provides JUnit tests for Gitlet, as well as a couple of utility
* methods.
*
* @author Joseph Moghadam
*
* Some code adapted from StackOverflow:
*
* http://stackoverflow.com/questions
* /779519/delete-files-recursively-in-java
*
* http://stackoverflow.com/questions/326390/how-to-create-a-java-string
* -from-the-contents-of-a-file
*
* http://stackoverflow.com/questions/1119385/junit-test-for-system-out-
* println
*
*/
public class GitletPublicTest {
private static final String GITLET_DIR = ".gitlet/";
private static final String TESTING_DIR = "test_files/";
/* matches either unix/mac or windows line separators */
private static final String LINE_SEPARATOR = "\r\n|[\r\n]";
/**
* Deletes existing gitlet system, resets the folder that stores files used
* in testing.
*
* This method runs before every @Test method. This is important to enforce
* that all tests are independent and do not interact with one another.
*/
@Before
public void setUp() {
File f = new File(GITLET_DIR);
if (f.exists()) {
recursiveDelete(f);
}
f = new File(TESTING_DIR);
if (f.exists()) {
recursiveDelete(f);
}
f.mkdirs();
}
/**
* Tests that init creates a .gitlet directory. Does NOT test that init
* creates an initial commit, which is the other functionality of init.
*/
@Test
public void testBasicInitialize() {
gitlet("init");
File f = new File(GITLET_DIR);
assertTrue(f.exists());
}
/**
* Tests that checking out a file name will restore the version of the file
* from the previous commit. Involves init, add, commit, and checkout.
*/
@Test
public void testBasicCheckout() {
String wugFileName = TESTING_DIR + "wug.txt";
String wugText = "This is a wug.";
createFile(wugFileName, wugText);
gitlet("init");
gitlet("add", wugFileName);
gitlet("commit", "added wug");
writeFile(wugFileName, "This is not a wug.");
gitlet("checkout", wugFileName);
assertEquals(wugText, getText(wugFileName));
}
/**
* Tests that log prints out commit messages in the right order. Involves
* init, add, commit, and log.
*/
@Test
public void testBasicLog() {
gitlet("init");
String commitMessage1 = "initial commit";
String wugFileName = TESTING_DIR + "wug.txt";
String wugText = "This is a wug.";
createFile(wugFileName, wugText);
gitlet("add", wugFileName);
String commitMessage2 = "added wug";
gitlet("commit", commitMessage2);
String logContent = gitlet("log");
assertArrayEquals(new String[] { commitMessage2, commitMessage1 },
extractCommitMessages(logContent));
}
/**
* Convenience method for calling Gitlet's main. Anything that is printed
* out during this call to main will NOT actually be printed out, but will
* instead be returned as a string from this method.
*
* Prepares a 'yes' answer on System.in so as to automatically pass through
* dangerous commands.
*
* The '...' syntax allows you to pass in an arbitrary number of String
* arguments, which are packaged into a String[].
*/
private static String gitlet(String... args) {
PrintStream originalOut = System.out;
InputStream originalIn = System.in;
ByteArrayOutputStream printingResults = new ByteArrayOutputStream();
try {
/*
* Below we change System.out, so that when you call
* System.out.println(), it won't print to the screen, but will
* instead be added to the printingResults object.
*/
System.setOut(new PrintStream(printingResults));
/*
* Prepares the answer "yes" on System.In, to pretend as if a user
* will type "yes". You won't be able to take user input during this
* time.
*/
String answer = "yes";
InputStream is = new ByteArrayInputStream(answer.getBytes());
System.setIn(is);
/* Calls the main method using the input arguments. */
Gitlet.main(args);
} finally {
/*
* Restores System.out and System.in (So you can print normally and
* take user input normally again).
*/
System.setOut(originalOut);
System.setIn(originalIn);
}
return printingResults.toString();
}
/**
* Returns the text from a standard text file (won't work with special
* characters).
*/
private static String getText(String fileName) {
try {
byte[] encoded = Files.readAllBytes(Paths.get(fileName));
return new String(encoded, StandardCharsets.UTF_8);
} catch (IOException e) {
return "";
}
}
/**
* Creates a new file with the given fileName and gives it the text
* fileText.
*/
private static void createFile(String fileName, String fileText) {
File f = new File(fileName);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
writeFile(fileName, fileText);
}
/**
* Replaces all text in the existing file with the given text.
*/
private static void writeFile(String fileName, String fileText) {
FileWriter fw = null;
try {
File f = new File(fileName);
fw = new FileWriter(f, false);
fw.write(fileText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Deletes the file and all files inside it, if it is a directory.
*/
private static void recursiveDelete(File d) {
if (d.isDirectory()) {
for (File f : d.listFiles()) {
recursiveDelete(f);
}
}
d.delete();
}
/**
* Returns an array of commit messages associated with what log has printed
* out.
*/
private static String[] extractCommitMessages(String logOutput) {
String[] logChunks = logOutput.split("====");
int numMessages = logChunks.length - 1;
String[] messages = new String[numMessages];
for (int i = 0; i < numMessages; i++) {
System.out.println(logChunks[i + 1]);
String[] logLines = logChunks[i + 1].split(LINE_SEPARATOR);
messages[i] = logLines[3];
}
return messages;
}
public static void main(String[] args) {
jh61b.junit.textui.runClasses(GitletPublicTest.class);
}
}