-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommitTester.java
More file actions
723 lines (588 loc) · 29 KB
/
CommitTester.java
File metadata and controls
723 lines (588 loc) · 29 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class CommitTester {
@BeforeAll
static void setUpBeforeClass() throws Exception {
File index = new File ("index");
File objects = new File ("objects");
File head = new File ("HEAD");
index.delete ();
index.createNewFile ();
objects.mkdir ();
File[] contents = objects.listFiles();
if (contents != null) {
for (File f : contents) {
f.delete ();
}
}
objects.delete ();
objects.mkdir ();
head.delete ();
}
@Test
@AfterAll
static void tearDownAfterClass() throws Exception {
File index = new File ("index");
File objects = new File ("objects");
File head = new File ("HEAD");
objects.mkdir ();
File[] contents = objects.listFiles();
if (contents != null) {
for (File f : contents) {
f.delete ();
}
}
objects.delete ();
index.createNewFile ();
head.delete ();
}
@Test
void testGetCommitSHA1andUpdateHEAD() throws Exception {
File head = new File ("HEAD");
head.delete ();
Commit commit = new Commit("Chris", "My name is Chris");
String output = commit.getCommitSHA1();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d, uuuu");
LocalDateTime now = LocalDateTime.now();
String date = now.format(dtf);
String hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
String author = "Chris";
String summary = "My name is Chris";
String compare = hash + "\n" + "" + "\n" + "" + "\n" + author + "\n" + date + "\n" + summary;
compare = commit.getSHA1fromString(compare);
assertTrue("The Correct Hash is Returned", compare.equals(output));
//test HEAD creation functionality
//makes sure HEAD exists
assertTrue (head.exists ());
Scanner scan = new Scanner(head);
scan.useDelimiter("\\Z");
String scannedContent = scan.next();
scan.close ();
//makes sure HEAD contents consists of correct SHA1 value (SHA1 of commit)
assertTrue (scannedContent.equals (compare));
//test HEAD updating functionality
Commit secondCommit = new Commit ("Andrew", "My name is Andrew Khacha");
Scanner scan2 = new Scanner(head);
scan2.useDelimiter("\\Z");
String scannedContent2 = scan2.next();
scan2.close ();
assertTrue (scannedContent2.equals (secondCommit.getCommitSHA1 ()));
}
@Test
void testCreateTree() throws Exception {//not detailed
Commit commit = new Commit("Chris", "My name is Chris");
commit.createTree();
String hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
Path path = Paths.get("./objects/" + hash);
assertTrue("createTree Method creates a tree", path.toFile().exists());
}
@Test
void testGetDate() throws Exception {
Commit commit = new Commit("Chris", "My name is Chris");
String time = commit.getDate();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d, uuuu");
LocalDateTime now = LocalDateTime.now();
String text = now.format(dtf);
assertTrue("Date is Correct", text.equals(time));
}
@Test
void testGetSHA1fromString() throws Exception {
Commit commit = new Commit("Chris", "My name is Chris");
String output = commit.getSHA1fromString("");
assertTrue("Sha1 returned is Correct", output.equals("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
}
@Test
void testSetNextCommit() throws Exception {
Commit commit = new Commit("Chris", "My name is Chris");
String oldHash = commit.getCommitSHA1();
commit.setNextCommit("next");
assertTrue("Hash doesn't change when next commit is changed", commit.getCommitSHA1().equals(oldHash));
Path path = Paths.get("./objects/" + oldHash);
String content = Files.readString(path);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d, uuuu");
LocalDateTime now = LocalDateTime.now();
String date = now.format(dtf);
String hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
String author = "Chris";
String summary = "My name is Chris";
String compare = hash + "\n" + "" + "\n" + "next" + "\n" + author + "\n" + date + "\n" + summary;
assertTrue("Commit File updates with next commit", compare.equals(content));
}
@Test
void testGetTreeSHA1FromCommit() throws Exception {
File head = new File ("./HEAD");
head.delete ();
Index i = new Index ();
i.initialize(".");
File pookie = new File ("pookieDoodle");
pookie.createNewFile ();
FileWriter writer = new FileWriter(pookie,false);
PrintWriter print = new PrintWriter(writer);
print.print ("pookieIsAFunnyName");
writer.close ();
print.close ();
i.indexAddFile ("pookieDoodle");
Commit myCommit = new Commit ("Chris", "isCool");
//makes sure gets correct tree sha1
assertEquals (Commit.getTreeSHA1FromCommit (myCommit.getCommitSHA1 ()), "92750b8d5d1901f627dd556cfc0152c1d2405878");
}
@Test
void oneCommitTest () throws Exception {
Index i = new Index ();
i.initialize (".");
File file1 = new File ("file1.txt");
file1.createNewFile ();
FileWriter writer = new FileWriter(file1,false);
PrintWriter print = new PrintWriter(writer);
print.print ("this is my first file!");
writer.close ();
print.close ();
File file2 = new File ("file2.txt");
file2.createNewFile ();
FileWriter writer2 = new FileWriter(file2,false);
PrintWriter print2 = new PrintWriter(writer2);
print2.print ("this is my second file!\nisn't it great?");
writer2.close ();
print2.close ();
i.indexAddFile ("file1.txt");
i.indexAddFile ("file2.txt");
Commit myCommit = new Commit ("Chris", "justCommittedTwoFiles");
System.out.println (Commit.getTreeSHA1FromCommit (myCommit.getCommitSHA1 ()));
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (myCommit.getCommitSHA1 ()));
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 1),"fab8a5d74f8b8c70c7983c86509a5ac226a77330");
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 2), "");
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 3), "");
file1.delete ();
file2.delete ();
}
@Test
void twoCommitTest () throws Exception {
Index i = new Index ();
i.initialize (".");
//first commit
File file1 = new File ("file1.txt");
file1.createNewFile ();
FileWriter writer = new FileWriter(file1,false);
PrintWriter print = new PrintWriter(writer);
print.print ("this is my first file!");
writer.close ();
print.close ();
File file2 = new File ("file2.txt");
file2.createNewFile ();
FileWriter writer2 = new FileWriter(file1,false);
PrintWriter print2 = new PrintWriter(writer2);
print2.print ("this is my second file!\nisn't it great?");
writer2.close ();
print2.close ();
i.indexAddFile ("file1.txt");
i.indexAddFile ("file2.txt");
Commit myCommit = new Commit ("Chris", "justCommittedTwoFiles");
//second commit
File file3 = new File ("file3.txt");
file3.createNewFile ();
FileWriter writer3 = new FileWriter(file3,false);
PrintWriter print3 = new PrintWriter(writer3);
print3.print ("this is my third file!");
writer3.close ();
print3.close ();
File file4 = new File ("file4.txt");
file4.createNewFile ();
FileWriter writer4 = new FileWriter(file4,false);
PrintWriter print4 = new PrintWriter(writer4);
print4.print ("this is my fourth file!\nisn't it great?");
writer4.close ();
print4.close ();
File folder1 = new File ("folder1");
folder1.mkdir ();
i.indexAddFile ("file3.txt");
i.indexAddFile ("file4.txt");
i.indexAddDirectory ("folder1");
Commit mySecondCommit = new Commit (myCommit.getCommitSHA1 (), "Chris", "justMadeASecondCommit");
//first commmit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (myCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 2), "");
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 3), mySecondCommit.getCommitSHA1 ());
//second commit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (mySecondCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 2), myCommit.getCommitSHA1 ());
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 3), "");
//cleanup
file1.delete ();
file2.delete ();
file3.delete ();
file4.delete ();
folder1.delete ();
}
@Test
void fourCommitTest () throws Exception {
Index i = new Index ();
i.initialize (".");
//first commit
File file1 = new File ("file1.txt");
file1.createNewFile ();
FileWriter writer = new FileWriter(file1,false);
PrintWriter print = new PrintWriter(writer);
print.print ("this is my first file!");
writer.close ();
print.close ();
File file2 = new File ("file2.txt");
file2.createNewFile ();
FileWriter writer2 = new FileWriter(file2,false);
PrintWriter print2 = new PrintWriter(writer2);
print2.print ("this is my second file!\nisn't it great?");
writer2.close ();
print2.close ();
i.indexAddFile ("file1.txt");
i.indexAddFile ("file2.txt");
Commit myCommit = new Commit ("Chris", "justCommittedTwoFiles");
//second commit
File file3 = new File ("file3.txt");
file3.createNewFile ();
FileWriter writer3 = new FileWriter(file3,false);
PrintWriter print3 = new PrintWriter(writer3);
print3.print ("this is my third file!");
writer3.close ();
print3.close ();
File file4 = new File ("file4.txt");
file4.createNewFile ();
FileWriter writer4 = new FileWriter(file4,false);
PrintWriter print4 = new PrintWriter(writer4);
print4.print ("this is my fourth file!\nisn't it great?");
writer4.close ();
print4.close ();
File folder1 = new File ("folder1");
folder1.mkdir ();
i.indexAddFile ("file3.txt");
i.indexAddFile ("file4.txt");
i.indexAddDirectory ("folder1");
Commit mySecondCommit = new Commit (myCommit.getCommitSHA1 (), "Chris", "justMadeASecondCommit");
//third commit
File file5 = new File ("file5.txt");
file5.createNewFile ();
FileWriter writer5 = new FileWriter(file5,false);
PrintWriter print5 = new PrintWriter(writer5);
print5.print ("this is my fifth file!");
writer5.close ();
print5.close ();
File file6 = new File ("file6.txt");
file6.createNewFile ();
FileWriter writer6 = new FileWriter(file6,false);
PrintWriter print6 = new PrintWriter(writer6);
print6.print ("this is my sixth file!\nisn't it great?");
writer6.close ();
print6.close ();
i.indexAddFile ("file5.txt");
i.indexAddFile ("file6.txt");
Commit myThirdCommit = new Commit (mySecondCommit.getCommitSHA1 (), "Chris", "justCommittedThreeFiles");
//fourth commit
File file7 = new File ("file7.txt");
file7.createNewFile ();
FileWriter writer7 = new FileWriter(file7,false);
PrintWriter print7 = new PrintWriter(writer7);
print7.print ("this is my seventh file!");
writer7.close ();
print7.close ();
File file8 = new File ("file8.txt");
file8.createNewFile ();
FileWriter writer8 = new FileWriter(file8,false);
PrintWriter print8 = new PrintWriter(writer8);
print8.print ("this is my eighth file!\nisn't it great?");
writer8.close ();
print8.close ();
File folder2 = new File ("folder2");
folder2.mkdir ();
i.indexAddFile ("file7.txt");
i.indexAddFile ("file8.txt");
i.indexAddDirectory ("folder1");
Commit myFourthCommit = new Commit (myThirdCommit.getCommitSHA1 (), "Chris", "justMadeAFourthCommit");
//first commmit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (myCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 2), "");
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + myCommit.getCommitSHA1 (), 3), mySecondCommit.getCommitSHA1 ());
//second commit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (mySecondCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 2), myCommit.getCommitSHA1 ());
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + mySecondCommit.getCommitSHA1 (), 3), myThirdCommit.getCommitSHA1 ());
//third commmit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + myThirdCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (myThirdCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + myThirdCommit.getCommitSHA1 (), 2), mySecondCommit.getCommitSHA1 ());
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + myThirdCommit.getCommitSHA1 (), 3), myFourthCommit.getCommitSHA1 ());
//fourth commit
//makes sure commit has correct tree
assertEquals (getLineOfFile ("./objects/" + myFourthCommit.getCommitSHA1 (), 1), Commit.getTreeSHA1FromCommit (myFourthCommit.getCommitSHA1 ()));
//makes sure commit has correct previous sha1
assertEquals (getLineOfFile ("./objects/" + myFourthCommit.getCommitSHA1 (), 2), myThirdCommit.getCommitSHA1 ());
//makes sure commit has correct next sha1
assertEquals (getLineOfFile ("./objects/" + myFourthCommit.getCommitSHA1 (), 3), "");
//cleanup
file1.delete ();
file2.delete ();
file3.delete ();
file4.delete ();
file5.delete ();
file6.delete ();
file7.delete ();
file8.delete ();
folder1.delete ();
folder2.delete ();
}
@Test
void editDeleteTest () throws Exception {
Index i = new Index ();
i.initialize (".");
//first commit
File file1 = new File ("file1.txt");
file1.createNewFile ();
FileWriter writer = new FileWriter(file1,false);
PrintWriter print = new PrintWriter(writer);
print.print ("this is my first file!");
writer.close ();
print.close ();
File file2 = new File ("file2.txt");
file2.createNewFile ();
FileWriter writer2 = new FileWriter(file2,false);
PrintWriter print2 = new PrintWriter(writer2);
print2.print ("this is my second file!\nisn't it great?");
writer2.close ();
print2.close ();
i.indexAddFile ("file1.txt");
i.indexAddFile ("file2.txt");
Commit myCommit = new Commit ("Chris", "justCommittedTwoFiles");
Scanner scanner = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(myCommit.getCommitSHA1())));
String firstTreeContents = scanner.useDelimiter("\\A").next();
scanner.close();
//making sure tree now contains file1 and correct version of file2
assertTrue (firstTreeContents.contains ("blob : d5cefe442c53a7d892866900d2504fa32e4f9fba : file1.txt"));
assertTrue (firstTreeContents.contains ("blob : 4fa61574c8588875afe99cad76f05508b9f9f0b9 : file2.txt"));
//editing second file and making new one
FileWriter editWriter = new FileWriter(file2,false);
PrintWriter editPrint = new PrintWriter(editWriter);
editPrint.print ("this is my edited second file!\ntell me it isn't wonderful!");
editWriter.close ();
editPrint.close ();
File file30 = new File ("file30.txt");
file30.createNewFile ();
FileWriter writer30 = new FileWriter(file30,false);
PrintWriter print30 = new PrintWriter(writer30);
print30.print ("this is my third file\nbut i gotta call it 30 cuz i made 3 already");
writer30.close ();
print30.close ();
i.indexAddFile ("file30.txt");
i.editExistingSavedFile ("file2.txt");
Commit editCommit1 = new Commit (myCommit.getCommitSHA1 (), "Chris", "just edited second file and made 30th");
Scanner scanner2 = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(editCommit1.getCommitSHA1())));
String editedTreeContents = scanner2.useDelimiter("\\A").next();
scanner2.close();
//making sure tree now contains file1, file 30, and correct version of file2
assertTrue (editedTreeContents.contains ("blob : d5cefe442c53a7d892866900d2504fa32e4f9fba : file1.txt"));
assertTrue (editedTreeContents.contains ("blob : 67ecd74e77682c89e41edf70e7a0a7a6133237ea : file2.txt"));
assertTrue (editedTreeContents.contains ("blob : 42a99771e3c8b9648532d61097ba83f4bfd75497 : file30.txt"));
//making sure those are the only contents
assertEquals (editedTreeContents.length (), 180);
i.deleteSavedFile ("file1.txt");
Commit deleteCommit1 = new Commit (editCommit1.getCommitSHA1 (), "Chris", "just deleted first file");
Scanner scanner1 = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(deleteCommit1.getCommitSHA1())));
String deletedTreeContents = scanner1.useDelimiter("\\A").next();
scanner1.close();
//makes sure commit tree contents are now just the edited version of file2 and file30
assertTrue (deletedTreeContents.contains ("blob : 67ecd74e77682c89e41edf70e7a0a7a6133237ea : file2.txt"));
assertTrue (deletedTreeContents.contains ("blob : 42a99771e3c8b9648532d61097ba83f4bfd75497 : file30.txt"));
assertEquals (deletedTreeContents.length (), 120);
File file3 = new File ("file3.txt");
file3.createNewFile ();
FileWriter writer3 = new FileWriter(file3,false);
PrintWriter print3 = new PrintWriter(writer3);
print3.print ("this is my third file!");
writer3.close ();
print3.close ();
File file4 = new File ("file4.txt");
file4.createNewFile ();
FileWriter writer4 = new FileWriter(file4,false);
PrintWriter print4 = new PrintWriter(writer4);
print4.print ("this is my fourth file!\nisn't it great?");
writer4.close ();
print4.close ();
i.indexAddFile ("file3.txt");
i.indexAddFile ("file4.txt");
Commit myCommit2 = new Commit (deleteCommit1.getCommitSHA1 (), "Chris", "added two more files");
Scanner scanner3 = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(myCommit2.getCommitSHA1())));
String fourthCommitTreeContents = scanner3.useDelimiter("\\A").next();
scanner3.close();
//making sure tree now contains only two new files, correct version of file2, and file30
assertTrue (fourthCommitTreeContents.contains ("blob : 945f0885e70a058b6dc8325535505ad2524bccf9 : file3.txt"));
assertTrue (fourthCommitTreeContents.contains ("blob : f9ff71475f089aa7b11b5954aa26f7b311c8d159 : file4.txt"));
assertTrue (fourthCommitTreeContents.contains ("tree : " + Commit.getTreeSHA1FromCommit(deleteCommit1.getCommitSHA1())));
assertEquals (fourthCommitTreeContents.length (), 167);
i.deleteSavedFile ("file3.txt");
Commit myCommit3 = new Commit (myCommit2.getCommitSHA1 (), "Chris", "deleted file 3");
Scanner scanner4 = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(myCommit3.getCommitSHA1())));
String fifthCommitTreeContents = scanner4.useDelimiter("\\A").next();
scanner4.close();
//making sure tree only contains file four and the tree before that
assertTrue (fifthCommitTreeContents.contains ("blob : f9ff71475f089aa7b11b5954aa26f7b311c8d159 : file4.txt"));
assertTrue (fifthCommitTreeContents.contains ("tree : " + Commit.getTreeSHA1FromCommit(deleteCommit1.getCommitSHA1())));
assertEquals (fifthCommitTreeContents.length (), 107);
FileWriter editWriter2 = new FileWriter(file4,false);
PrintWriter editPrint2 = new PrintWriter(editWriter2);
editPrint2.print ("this is my edited fourth file!\ntell me it isn't tremendously fantabulous!");
editWriter2.close ();
editPrint2.close ();
i.editExistingSavedFile("file4.txt");
Commit finalCommit = new Commit (myCommit3.getCommitSHA1 (), "Chris", "made a last commit with a deletion");
Scanner scanner5 = new Scanner(new File ("./objects/" + Commit.getTreeSHA1FromCommit(finalCommit.getCommitSHA1())));
String finalCommitTreeContents = scanner5.useDelimiter("\\A").next();
scanner5.close();
//making sure tree now contains only third tree and edited version of file 4
assertTrue (finalCommitTreeContents.contains ("blob : e21f5c496552f14600f9ce96fb1395b30a1569f4 : file4.txt"));
assertTrue (finalCommitTreeContents.contains ("tree : " + Commit.getTreeSHA1FromCommit(deleteCommit1.getCommitSHA1())));
assertEquals (finalCommitTreeContents.length (), 107);
//cleanup
file1.delete ();
file2.delete ();
file3.delete ();
file4.delete ();
}
@Test
void testCommitTraversal () throws Exception {
File head = new File ("./HEAD");
head.delete ();
Index i = new Index ();
i.initialize(".");
Commit firstCommit = new Commit ("Chris", "made a first commit");
Commit secondCommit = new Commit (firstCommit.getCommitSHA1 (), "Chris", "made a second commit");
Commit thirdCommit = new Commit (secondCommit.getCommitSHA1 (), "Chris", "made a third commit");
Commit fourthCommit = new Commit (thirdCommit.getCommitSHA1 (), "Chris", "made a fourth commit");
Commit fifthCommit = new Commit (fourthCommit.getCommitSHA1 (), "Chris", "made a fifth commit");
//checks to see if fifth commit sha is equal to the one obtained through traversing 5 commits
assertEquals (fifthCommit.getCommitSHA1 (), getNextCommitSHA("./objects/" + getNextCommitSHA ("./objects/" + getNextCommitSHA("./objects/" + getNextCommitSHA ("./objects/" + firstCommit.getCommitSHA1 ())))));
}
@Test
void testCheckout () throws Exception {
File head = new File ("./HEAD");
head.delete ();
Index i = new Index ();
i.initialize(".");
//making test directories
File advancedDirectory = new File ("advancedDirectory");
advancedDirectory.mkdir();
File directoryEmpty = new File ("advancedDirectory/directoryEmpty");
directoryEmpty.mkdir();
File directoryFull = new File ("advancedDirectory/directoryFull");
directoryFull.mkdir();
//making test files
makeTextFile("./advancedDirectory/textFileOuter1", "fileOuter1");
makeTextFile("./advancedDirectory/textFileOuter2", "fileOuter2");
makeTextFile("./advancedDirectory/textFileOuter3", "fileOuter3");
makeTextFile("./advancedDirectory/directoryFull/textFileInner", "this file is inner");
i.indexAddDirectory("./advancedDirectory");
Commit myCommit = new Commit ("Chris", "correct commit for reversion");
File firstFile = new File ("./advancedDirectory/textFileOuter1");
File secondFile = new File ("./advancedDirectory/textFileOuter2");
File thirdFile = new File ("./advancedDirectory/textFileOuter3");
File fourthFile = new File ("./advancedDirectory/directoryFull/textFileInner");
firstFile.delete ();
secondFile.delete ();
thirdFile.delete ();
fourthFile.delete ();
directoryEmpty.delete ();
directoryFull.delete ();
advancedDirectory.delete ();
Commit secondCommit = new Commit (myCommit.getCommitSHA1(), "Chris", "ew i messed up with this commit");
secondCommit.checkout (myCommit.getCommitSHA1());
File firstFileForTesting = new File ("./checkoutFolder/advancedDirectory/textFileOuter1");
File secondFileForTesting = new File ("./checkoutFolder/advancedDirectory/textFileOuter2");
File thirdFileForTesting = new File ("./checkoutFolder/advancedDirectory/textFileOuter3");
File fourthFileForTesting = new File ("./checkoutFolder/advancedDirectory/directoryFull/textFileInner");
File directoryEmptyForTesting = new File ("./checkoutFolder/advancedDirectory/directoryEmpty");
File directoryFullForTesting = new File ("./checkoutFolder/advancedDirectory/directoryFull");
File advancedDirectoryForTesting = new File ("./checkoutFolder/advancedDirectory");
//checking everything is correct now
assertTrue (firstFileForTesting.exists ());
assertTrue (secondFileForTesting.exists ());
assertTrue (thirdFileForTesting.exists ());
assertTrue (fourthFileForTesting.exists ());
assertTrue (directoryEmptyForTesting.exists ());
assertTrue (directoryFullForTesting.exists ());
assertTrue (advancedDirectoryForTesting.exists ());
Scanner scan1 = new Scanner(firstFileForTesting);
scan1.useDelimiter("\\A");
String scannedContent1 = scan1.next();
scan1.close ();
Scanner scan2 = new Scanner(secondFileForTesting);
scan2.useDelimiter("\\A");
String scannedContent2 = scan2.next();
scan2.close ();
Scanner scan3 = new Scanner(thirdFileForTesting);
scan3.useDelimiter("\\A");
String scannedContent3 = scan3.next();
scan3.close ();
Scanner scan4 = new Scanner(fourthFileForTesting);
scan4.useDelimiter("\\A");
String scannedContent4 = scan4.next();
scan4.close ();
assertEquals (scannedContent1,"fileOuter1");
assertEquals (scannedContent2,"fileOuter2");
assertEquals (scannedContent3,"fileOuter3");
assertEquals (scannedContent4,"this file is inner");
//cleaning up
firstFileForTesting.delete ();
secondFileForTesting.delete ();
thirdFileForTesting.delete ();
fourthFileForTesting.delete ();
directoryEmptyForTesting.delete ();
directoryFullForTesting.delete ();
advancedDirectoryForTesting.delete ();
}
private void makeTextFile (String path, String contents) throws IOException
{
File theFile = new File (path);
theFile.createNewFile ();
FileWriter writer = new FileWriter(path,false);
PrintWriter out = new PrintWriter(writer);
out.print (contents);
writer.close ();
out.close ();
}
public String getNextCommitSHA (String thisCommitPath) throws IOException
{
return getLineOfFile (thisCommitPath, 3);
}
public String getLineOfFile (String filePath, int lineNum) throws IOException
{
Path path = Paths.get(filePath);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
return (lines.get (lineNum - 1));
}
}