Debian Codegraph @

plumbing/format/diff/unified_encoder_test.go

raw
1 package diff
2
3 import (
4 "bytes"
5 "testing"
6
7 "github.com/go-git/go-git/v5/plumbing"
8 "github.com/go-git/go-git/v5/plumbing/color"
9 "github.com/go-git/go-git/v5/plumbing/filemode"
10
11 . "gopkg.in/check.v1"
12 )
13
14 func Test(t *testing.T) { TestingT(t) }
15
16 type UnifiedEncoderTestSuite struct{}
17
18 var _ = Suite(&UnifiedEncoderTestSuite{})
19
20 func (s *UnifiedEncoderTestSuite) TestBothFilesEmpty(c *C) {
21 buffer := bytes.NewBuffer(nil)
22 e := NewUnifiedEncoder(buffer, 1)
23 err := e.Encode(testPatch{filePatches: []testFilePatch{{}}})
24 c.Assert(err, IsNil)
25 }
26
27 func (s *UnifiedEncoderTestSuite) TestBinaryFile(c *C) {
28 buffer := bytes.NewBuffer(nil)
29 e := NewUnifiedEncoder(buffer, 1)
30 p := testPatch{
31 message: "",
32 filePatches: []testFilePatch{{
33 from: &testFile{
34 mode: filemode.Regular,
35 path: "binary",
36 seed: "something",
37 },
38 to: &testFile{
39 mode: filemode.Regular,
40 path: "binary",
41 seed: "otherthing",
42 },
43 }},
44 }
45
46 err := e.Encode(p)
47 c.Assert(err, IsNil)
48
49 c.Assert(buffer.String(), Equals, `diff --git a/binary b/binary
50 index a459bc245bdbc45e1bca99e7fe61731da5c48da4..6879395eacf3cc7e5634064ccb617ac7aa62be7d 100644
51 Binary files a/binary and b/binary differ
52 `)
53 }
54
55 func (s *UnifiedEncoderTestSuite) TestCustomSrcDstPrefix(c *C) {
56 buffer := bytes.NewBuffer(nil)
57 e := NewUnifiedEncoder(buffer, 1).SetSrcPrefix("source/prefix/").SetDstPrefix("dest/prefix/")
58 p := testPatch{
59 message: "",
60 filePatches: []testFilePatch{{
61 from: &testFile{
62 mode: filemode.Regular,
63 path: "binary",
64 seed: "something",
65 },
66 to: &testFile{
67 mode: filemode.Regular,
68 path: "binary",
69 seed: "otherthing",
70 },
71 }},
72 }
73
74 err := e.Encode(p)
75 c.Assert(err, IsNil)
76
77 c.Assert(buffer.String(), Equals, `diff --git source/prefix/binary dest/prefix/binary
78 index a459bc245bdbc45e1bca99e7fe61731da5c48da4..6879395eacf3cc7e5634064ccb617ac7aa62be7d 100644
79 Binary files source/prefix/binary and dest/prefix/binary differ
80 `)
81 }
82
83 func (s *UnifiedEncoderTestSuite) TestEncode(c *C) {
84 for _, f := range fixtures {
85 c.Log("executing: ", f.desc)
86
87 buffer := bytes.NewBuffer(nil)
88 e := NewUnifiedEncoder(buffer, f.context).SetColor(f.color)
89
90 err := e.Encode(f.patch)
91 c.Assert(err, IsNil)
92
93 c.Assert(buffer.String(), Equals, f.diff)
94 }
95 }
96
97 var oneChunkPatch Patch = testPatch{
98 message: "",
99 filePatches: []testFilePatch{{
100 from: &testFile{
101 mode: filemode.Regular,
102 path: "onechunk.txt",
103 seed: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nÑ\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ",
104 },
105 to: &testFile{
106 mode: filemode.Regular,
107 path: "onechunk.txt",
108 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
109 },
110
111 chunks: []testChunk{{
112 content: "A\n",
113 op: Delete,
114 }, {
115 content: "B\nC\nD\nE\nF\nG\n",
116 op: Equal,
117 }, {
118 content: "H\n",
119 op: Delete,
120 }, {
121 content: "I\nJ\nK\nL\nM\nN\n",
122 op: Equal,
123 }, {
124 content: "Ñ\n",
125 op: Delete,
126 }, {
127 content: "O\nP\nQ\nR\nS\nT\n",
128 op: Equal,
129 }, {
130 content: "U\n",
131 op: Delete,
132 }, {
133 content: "V\nW\nX\nY\nZ",
134 op: Equal,
135 }},
136 }},
137 }
138
139 var oneChunkPatchInverted Patch = testPatch{
140 message: "",
141 filePatches: []testFilePatch{{
142 to: &testFile{
143 mode: filemode.Regular,
144 path: "onechunk.txt",
145 seed: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nÑ\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ",
146 },
147 from: &testFile{
148 mode: filemode.Regular,
149 path: "onechunk.txt",
150 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
151 },
152
153 chunks: []testChunk{{
154 content: "A\n",
155 op: Add,
156 }, {
157 content: "B\nC\nD\nE\nF\nG\n",
158 op: Equal,
159 }, {
160 content: "H\n",
161 op: Add,
162 }, {
163 content: "I\nJ\nK\nL\nM\nN\n",
164 op: Equal,
165 }, {
166 content: "Ñ\n",
167 op: Add,
168 }, {
169 content: "O\nP\nQ\nR\nS\nT\n",
170 op: Equal,
171 }, {
172 content: "U\n",
173 op: Add,
174 }, {
175 content: "V\nW\nX\nY\nZ",
176 op: Equal,
177 }},
178 }},
179 }
180
181 var fixtures []*fixture = []*fixture{{
182 patch: testPatch{
183 message: "",
184 filePatches: []testFilePatch{{
185 from: &testFile{
186 mode: filemode.Regular,
187 path: "README.md",
188 seed: "hello\nworld\n",
189 },
190 to: &testFile{
191 mode: filemode.Regular,
192 path: "README.md",
193 seed: "hello\nbug\n",
194 },
195 chunks: []testChunk{{
196 content: "hello\n",
197 op: Equal,
198 }, {
199 content: "world\n",
200 op: Delete,
201 }, {
202 content: "bug\n",
203 op: Add,
204 }},
205 }},
206 },
207 desc: "positive negative number",
208 context: 2,
209 diff: `diff --git a/README.md b/README.md
210 index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644
211 --- a/README.md
212 +++ b/README.md
213 @@ -1,2 +1,2 @@
214 hello
215 -world
216 +bug
217 `,
218 }, {
219 patch: testPatch{
220 message: "",
221 filePatches: []testFilePatch{{
222 from: &testFile{
223 mode: filemode.Regular,
224 path: "test.txt",
225 seed: "test",
226 },
227 to: &testFile{
228 mode: filemode.Executable,
229 path: "test.txt",
230 seed: "test",
231 },
232 chunks: nil,
233 }},
234 },
235 desc: "make executable",
236 context: 1,
237 diff: `diff --git a/test.txt b/test.txt
238 old mode 100644
239 new mode 100755
240 `,
241 }, {
242 patch: testPatch{
243 message: "",
244 filePatches: []testFilePatch{{
245 from: &testFile{
246 mode: filemode.Regular,
247 path: "test.txt",
248 seed: "test",
249 },
250 to: &testFile{
251 mode: filemode.Regular,
252 path: "test1.txt",
253 seed: "test",
254 },
255 chunks: nil,
256 }},
257 },
258 desc: "rename file",
259 context: 1,
260 diff: `diff --git a/test.txt b/test1.txt
261 rename from test.txt
262 rename to test1.txt
263 `,
264 }, {
265 patch: testPatch{
266 message: "",
267 filePatches: []testFilePatch{{
268 from: &testFile{
269 mode: filemode.Regular,
270 path: "test.txt",
271 seed: "test\n",
272 },
273 to: &testFile{
274 mode: filemode.Regular,
275 path: "test1.txt",
276 seed: "test1\n",
277 },
278 chunks: []testChunk{{
279 content: "test\n",
280 op: Delete,
281 }, {
282 content: "test1\n",
283 op: Add,
284 }},
285 }},
286 },
287 desc: "rename file with changes",
288 context: 1,
289 diff: `diff --git a/test.txt b/test1.txt
290 rename from test.txt
291 rename to test1.txt
292 index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..a5bce3fd2565d8f458555a0c6f42d0504a848bd5 100644
293 --- a/test.txt
294 +++ b/test1.txt
295 @@ -1 +1 @@
296 -test
297 +test1
298 `,
299 }, {
300 patch: testPatch{
301 message: "",
302 filePatches: []testFilePatch{{
303 from: &testFile{
304 mode: filemode.Regular,
305 path: "test.txt",
306 seed: "test",
307 },
308 to: &testFile{
309 mode: filemode.Executable,
310 path: "test1.txt",
311 seed: "test",
312 },
313 chunks: nil,
314 }},
315 },
316 desc: "rename with file mode change",
317 context: 1,
318 diff: `diff --git a/test.txt b/test1.txt
319 old mode 100644
320 new mode 100755
321 rename from test.txt
322 rename to test1.txt
323 `,
324 }, {
325 patch: testPatch{
326 message: "",
327 filePatches: []testFilePatch{{
328 from: &testFile{
329 mode: filemode.Regular,
330 path: "test.txt",
331 seed: "test\n",
332 },
333 to: &testFile{
334 mode: filemode.Regular,
335 path: "test.txt",
336 seed: "test2\n",
337 },
338
339 chunks: []testChunk{{
340 content: "test\n",
341 op: Delete,
342 }, {
343 content: "test2\n",
344 op: Add,
345 }},
346 }},
347 },
348
349 desc: "one line change",
350 context: 1,
351 diff: `diff --git a/test.txt b/test.txt
352 index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..180cf8328022becee9aaa2577a8f84ea2b9f3827 100644
353 --- a/test.txt
354 +++ b/test.txt
355 @@ -1 +1 @@
356 -test
357 +test2
358 `,
359 }, {
360 patch: testPatch{
361 message: "this is the message\n",
362 filePatches: []testFilePatch{{
363 from: &testFile{
364 mode: filemode.Regular,
365 path: "test.txt",
366 seed: "test\n",
367 },
368 to: &testFile{
369 mode: filemode.Regular,
370 path: "test.txt",
371 seed: "test2\n",
372 },
373
374 chunks: []testChunk{{
375 content: "test\n",
376 op: Delete,
377 }, {
378 content: "test2\n",
379 op: Add,
380 }},
381 }},
382 },
383
384 desc: "one line change with message",
385 context: 1,
386 diff: `this is the message
387 diff --git a/test.txt b/test.txt
388 index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..180cf8328022becee9aaa2577a8f84ea2b9f3827 100644
389 --- a/test.txt
390 +++ b/test.txt
391 @@ -1 +1 @@
392 -test
393 +test2
394 `,
395 }, {
396 patch: testPatch{
397 message: "this is the message",
398 filePatches: []testFilePatch{{
399 from: &testFile{
400 mode: filemode.Regular,
401 path: "test.txt",
402 seed: "test",
403 },
404 to: &testFile{
405 mode: filemode.Regular,
406 path: "test.txt",
407 seed: "test2",
408 },
409
410 chunks: []testChunk{{
411 content: "test",
412 op: Delete,
413 }, {
414 content: "test2",
415 op: Add,
416 }},
417 }},
418 },
419
420 desc: "one line change with message and no end of line",
421 context: 1,
422 diff: `this is the message
423 diff --git a/test.txt b/test.txt
424 index 30d74d258442c7c65512eafab474568dd706c430..d606037cb232bfda7788a8322492312d55b2ae9d 100644
425 --- a/test.txt
426 +++ b/test.txt
427 @@ -1 +1 @@
428 -test
429 \ No newline at end of file
430 +test2
431 \ No newline at end of file
432 `,
433 }, {
434 patch: testPatch{
435 message: "",
436 filePatches: []testFilePatch{{
437 from: nil,
438 to: &testFile{
439 mode: filemode.Regular,
440 path: "new.txt",
441 seed: "test\ntest2\ntest3",
442 },
443
444 chunks: []testChunk{{
445 content: "test\ntest2\ntest3",
446 op: Add,
447 }},
448 }},
449 },
450
451 desc: "new file",
452 context: 1,
453 diff: `diff --git a/new.txt b/new.txt
454 new file mode 100644
455 index 0000000000000000000000000000000000000000..3ceaab5442b64a0c2b33dd25fae67ccdb4fd1ea8
456 --- /dev/null
457 +++ b/new.txt
458 @@ -0,0 +1,3 @@
459 +test
460 +test2
461 +test3
462 \ No newline at end of file
463 `,
464 }, {
465 patch: testPatch{
466 message: "",
467 filePatches: []testFilePatch{{
468 from: &testFile{
469 mode: filemode.Regular,
470 path: "old.txt",
471 seed: "test",
472 },
473 to: nil,
474
475 chunks: []testChunk{{
476 content: "test",
477 op: Delete,
478 }},
479 }},
480 },
481
482 desc: "delete file",
483 context: 1,
484 diff: `diff --git a/old.txt b/old.txt
485 deleted file mode 100644
486 index 30d74d258442c7c65512eafab474568dd706c430..0000000000000000000000000000000000000000
487 --- a/old.txt
488 +++ /dev/null
489 @@ -1 +0,0 @@
490 -test
491 \ No newline at end of file
492 `,
493 }, {
494 patch: oneChunkPatch,
495 desc: "modified deleting lines file with context to 1",
496 context: 1,
497 diff: `diff --git a/onechunk.txt b/onechunk.txt
498 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
499 --- a/onechunk.txt
500 +++ b/onechunk.txt
501 @@ -1,2 +1 @@
502 -A
503 B
504 @@ -7,3 +6,2 @@ F
505 G
506 -H
507 I
508 @@ -14,3 +12,2 @@ M
509 N
510 -Ñ
511 O
512 @@ -21,3 +18,2 @@ S
513 T
514 -U
515 V
516 `,
517 }, {
518 patch: oneChunkPatch,
519 desc: "modified deleting lines file with context to 2",
520 context: 2,
521 diff: `diff --git a/onechunk.txt b/onechunk.txt
522 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
523 --- a/onechunk.txt
524 +++ b/onechunk.txt
525 @@ -1,3 +1,2 @@
526 -A
527 B
528 C
529 @@ -6,5 +5,4 @@ E
530 F
531 G
532 -H
533 I
534 J
535 @@ -13,5 +11,4 @@ L
536 M
537 N
538 -Ñ
539 O
540 P
541 @@ -20,5 +17,4 @@ R
542 S
543 T
544 -U
545 V
546 W
547 `,
548 }, {
549 patch: oneChunkPatch,
550 desc: "modified deleting lines file with context to 6",
551 context: 6,
552 diff: `diff --git a/onechunk.txt b/onechunk.txt
553 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
554 --- a/onechunk.txt
555 +++ b/onechunk.txt
556 @@ -1,27 +1,23 @@
557 -A
558 B
559 C
560 D
561 E
562 F
563 G
564 -H
565 I
566 J
567 K
568 L
569 M
570 N
571 -Ñ
572 O
573 P
574 Q
575 R
576 S
577 T
578 -U
579 V
580 W
581 X
582 Y
583 Z
584 \ No newline at end of file
585 `,
586 }, {
587 patch: oneChunkPatch,
588
589 desc: "modified deleting lines file with context to 3",
590 context: 3,
591 diff: `diff --git a/onechunk.txt b/onechunk.txt
592 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
593 --- a/onechunk.txt
594 +++ b/onechunk.txt
595 @@ -1,25 +1,21 @@
596 -A
597 B
598 C
599 D
600 E
601 F
602 G
603 -H
604 I
605 J
606 K
607 L
608 M
609 N
610 -Ñ
611 O
612 P
613 Q
614 R
615 S
616 T
617 -U
618 V
619 W
620 X
621 `,
622 }, {
623 patch: oneChunkPatch,
624 desc: "modified deleting lines file with context to 4",
625 context: 4,
626 diff: `diff --git a/onechunk.txt b/onechunk.txt
627 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
628 --- a/onechunk.txt
629 +++ b/onechunk.txt
630 @@ -1,26 +1,22 @@
631 -A
632 B
633 C
634 D
635 E
636 F
637 G
638 -H
639 I
640 J
641 K
642 L
643 M
644 N
645 -Ñ
646 O
647 P
648 Q
649 R
650 S
651 T
652 -U
653 V
654 W
655 X
656 Y
657 `,
658 }, {
659 patch: oneChunkPatch,
660 desc: "modified deleting lines file with context to 0",
661 context: 0,
662 diff: `diff --git a/onechunk.txt b/onechunk.txt
663 index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644
664 --- a/onechunk.txt
665 +++ b/onechunk.txt
666 @@ -1 +0,0 @@
667 -A
668 @@ -8 +6,0 @@ G
669 -H
670 @@ -15 +12,0 @@ N
671 -Ñ
672 @@ -22 +18,0 @@ T
673 -U
674 `,
675 }, {
676 patch: oneChunkPatchInverted,
677 desc: "modified adding lines file with context to 1",
678 context: 1,
679 diff: `diff --git a/onechunk.txt b/onechunk.txt
680 index 0adddcde4fd38042c354518351820eb06c417c82..ab5eed5d4a2c33aeef67e0188ee79bed666bde6f 100644
681 --- a/onechunk.txt
682 +++ b/onechunk.txt
683 @@ -1 +1,2 @@
684 +A
685 B
686 @@ -6,2 +7,3 @@ F
687 G
688 +H
689 I
690 @@ -12,2 +14,3 @@ M
691 N
692 +Ñ
693 O
694 @@ -18,2 +21,3 @@ S
695 T
696 +U
697 V
698 `,
699 }, {
700 patch: oneChunkPatchInverted,
701 desc: "modified adding lines file with context to 2",
702 context: 2,
703 diff: `diff --git a/onechunk.txt b/onechunk.txt
704 index 0adddcde4fd38042c354518351820eb06c417c82..ab5eed5d4a2c33aeef67e0188ee79bed666bde6f 100644
705 --- a/onechunk.txt
706 +++ b/onechunk.txt
707 @@ -1,2 +1,3 @@
708 +A
709 B
710 C
711 @@ -5,4 +6,5 @@ E
712 F
713 G
714 +H
715 I
716 J
717 @@ -11,4 +13,5 @@ L
718 M
719 N
720 +Ñ
721 O
722 P
723 @@ -17,4 +20,5 @@ R
724 S
725 T
726 +U
727 V
728 W
729 `,
730 }, {
731 patch: oneChunkPatchInverted,
732 desc: "modified adding lines file with context to 3",
733 context: 3,
734 diff: `diff --git a/onechunk.txt b/onechunk.txt
735 index 0adddcde4fd38042c354518351820eb06c417c82..ab5eed5d4a2c33aeef67e0188ee79bed666bde6f 100644
736 --- a/onechunk.txt
737 +++ b/onechunk.txt
738 @@ -1,21 +1,25 @@
739 +A
740 B
741 C
742 D
743 E
744 F
745 G
746 +H
747 I
748 J
749 K
750 L
751 M
752 N
753 +Ñ
754 O
755 P
756 Q
757 R
758 S
759 T
760 +U
761 V
762 W
763 X
764 `,
765 }, {
766 patch: oneChunkPatchInverted,
767 desc: "modified adding lines file with context to 4",
768 context: 4,
769 diff: `diff --git a/onechunk.txt b/onechunk.txt
770 index 0adddcde4fd38042c354518351820eb06c417c82..ab5eed5d4a2c33aeef67e0188ee79bed666bde6f 100644
771 --- a/onechunk.txt
772 +++ b/onechunk.txt
773 @@ -1,22 +1,26 @@
774 +A
775 B
776 C
777 D
778 E
779 F
780 G
781 +H
782 I
783 J
784 K
785 L
786 M
787 N
788 +Ñ
789 O
790 P
791 Q
792 R
793 S
794 T
795 +U
796 V
797 W
798 X
799 Y
800 `,
801 }, {
802 patch: oneChunkPatchInverted,
803 desc: "modified adding lines file with context to 0",
804 context: 0,
805 diff: `diff --git a/onechunk.txt b/onechunk.txt
806 index 0adddcde4fd38042c354518351820eb06c417c82..ab5eed5d4a2c33aeef67e0188ee79bed666bde6f 100644
807 --- a/onechunk.txt
808 +++ b/onechunk.txt
809 @@ -0,0 +1 @@
810 +A
811 @@ -6,0 +8 @@ G
812 +H
813 @@ -12,0 +15 @@ N
814 +Ñ
815 @@ -18,0 +22 @@ T
816 +U
817 `,
818 }, {
819 patch: testPatch{
820 message: "",
821 filePatches: []testFilePatch{{
822 from: &testFile{
823 mode: filemode.Regular,
824 path: "onechunk.txt",
825 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
826 },
827 to: &testFile{
828 mode: filemode.Regular,
829 path: "onechunk.txt",
830 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\n",
831 },
832
833 chunks: []testChunk{{
834 content: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\n",
835 op: Equal,
836 }, {
837 content: "Z",
838 op: Delete,
839 }},
840 }},
841 },
842 desc: "remove last letter",
843 context: 0,
844 diff: `diff --git a/onechunk.txt b/onechunk.txt
845 index 0adddcde4fd38042c354518351820eb06c417c82..553ae669c7a9303cf848fcc749a2569228ac5309 100644
846 --- a/onechunk.txt
847 +++ b/onechunk.txt
848 @@ -23 +22,0 @@ Y
849 -Z
850 \ No newline at end of file
851 `,
852 }, {
853 patch: testPatch{
854 message: "",
855 filePatches: []testFilePatch{{
856 from: &testFile{
857 mode: filemode.Regular,
858 path: "onechunk.txt",
859 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
860 },
861 to: &testFile{
862 mode: filemode.Regular,
863 path: "onechunk.txt",
864 seed: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY",
865 },
866
867 chunks: []testChunk{{
868 content: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\n",
869 op: Equal,
870 }, {
871 content: "Y\nZ",
872 op: Delete,
873 }, {
874 content: "Y",
875 op: Add,
876 }},
877 }},
878 },
879 desc: "remove last letter and no newline at end of file",
880 context: 0,
881 diff: `diff --git a/onechunk.txt b/onechunk.txt
882 index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f26c9218d 100644
883 --- a/onechunk.txt
884 +++ b/onechunk.txt
885 @@ -22,2 +21 @@ X
886 -Y
887 -Z
888 \ No newline at end of file
889 +Y
890 \ No newline at end of file
891 `,
892 }, {
893 patch: testPatch{
894 message: "",
895 filePatches: []testFilePatch{{
896 from: &testFile{
897 mode: filemode.Regular,
898 path: "README.md",
899 seed: "hello\nworld\n",
900 },
901 to: &testFile{
902 mode: filemode.Regular,
903 path: "README.md",
904 seed: "hello\nbug\n",
905 },
906 chunks: []testChunk{{
907 content: "hello\n",
908 op: Equal,
909 }, {
910 content: "world\n",
911 op: Delete,
912 }, {
913 content: "bug\n",
914 op: Add,
915 }},
916 }},
917 },
918 desc: "positive negative number with color",
919 context: 2,
920 color: NewColorConfig(),
921 diff: "" +
922 color.Bold + "diff --git a/README.md b/README.md\n" +
923 "index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644\n" +
924 "--- a/README.md\n" +
925 "+++ b/README.md" + color.Reset + "\n" +
926 color.Cyan + "@@ -1,2 +1,2 @@" + color.Reset + "\n" +
927 " hello\n" +
928 color.Red + "-world" + color.Reset + "\n" +
929 color.Green + "+bug" + color.Reset + "\n",
930 }, {
931 patch: testPatch{
932 message: "",
933 filePatches: []testFilePatch{{
934 from: &testFile{
935 mode: filemode.Regular,
936 path: "test.txt",
937 seed: "test\n",
938 },
939 to: &testFile{
940 mode: filemode.Regular,
941 path: "test.txt",
942 seed: "test2\n",
943 },
944
945 chunks: []testChunk{{
946 content: "test\n",
947 op: Delete,
948 }, {
949 content: "test2\n",
950 op: Add,
951 }},
952 }},
953 },
954
955 desc: "one line change with color",
956 context: 1,
957 color: NewColorConfig(
958 WithColor(Func, color.Reverse),
959 ),
960 diff: "" +
961 color.Bold + "diff --git a/test.txt b/test.txt\n" +
962 "index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..180cf8328022becee9aaa2577a8f84ea2b9f3827 100644\n" +
963 "--- a/test.txt\n" +
964 "+++ b/test.txt" + color.Reset + "\n" +
965 color.Cyan + "@@ -1 +1 @@" + color.Reset + "\n" +
966 color.Red + "-test" + color.Reset + "\n" +
967 color.Green + "+test2" + color.Reset + "\n",
968 }, {
969 patch: oneChunkPatch,
970 desc: "modified deleting lines file with context to 1 with color",
971 context: 1,
972 color: NewColorConfig(
973 WithColor(Func, color.Reverse),
974 ),
975 diff: "" +
976 color.Bold + "diff --git a/onechunk.txt b/onechunk.txt\n" +
977 "index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644\n" +
978 "--- a/onechunk.txt\n" +
979 "+++ b/onechunk.txt" + color.Reset + "\n" +
980 color.Cyan + "@@ -1,2 +1 @@" + color.Reset + "\n" +
981 color.Red + "-A" + color.Reset + "\n" +
982 " B\n" +
983 color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" +
984 " G\n" +
985 color.Red + "-H" + color.Reset + "\n" +
986 " I\n" +
987 color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" +
988 " N\n" +
989 color.Red + "-Ñ" + color.Reset + "\n" +
990 " O\n" +
991 color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" +
992 " T\n" +
993 color.Red + "-U" + color.Reset + "\n" +
994 " V\n",
995 }}
996
997 type testPatch struct {
998 message string
999 filePatches []testFilePatch
1000 }
1001
1002 func (t testPatch) FilePatches() []FilePatch {
1003 var result []FilePatch
1004 for _, f := range t.filePatches {
1005 result = append(result, f)
1006 }
1007
1008 return result
1009 }
1010
1011 func (t testPatch) Message() string {
1012 return t.message
1013 }
1014
1015 type testFilePatch struct {
1016 from, to *testFile
1017 chunks []testChunk
1018 }
1019
1020 func (t testFilePatch) IsBinary() bool {
1021 return len(t.chunks) == 0
1022 }
1023 func (t testFilePatch) Files() (File, File) {
1024 // Go is amazing
1025 switch {
1026 case t.from == nil && t.to == nil:
1027 return nil, nil
1028 case t.from == nil:
1029 return nil, t.to
1030 case t.to == nil:
1031 return t.from, nil
1032 }
1033
1034 return t.from, t.to
1035 }
1036
1037 func (t testFilePatch) Chunks() []Chunk {
1038 var result []Chunk
1039 for _, c := range t.chunks {
1040 result = append(result, c)
1041 }
1042 return result
1043 }
1044
1045 type testFile struct {
1046 path string
1047 mode filemode.FileMode
1048 seed string
1049 }
1050
1051 func (t testFile) Hash() plumbing.Hash {
1052 return plumbing.ComputeHash(plumbing.BlobObject, []byte(t.seed))
1053 }
1054
1055 func (t testFile) Mode() filemode.FileMode {
1056 return t.mode
1057 }
1058
1059 func (t testFile) Path() string {
1060 return t.path
1061 }
1062
1063 type testChunk struct {
1064 content string
1065 op Operation
1066 }
1067
1068 func (t testChunk) Content() string {
1069 return t.content
1070 }
1071
1072 func (t testChunk) Type() Operation {
1073 return t.op
1074 }
1075
1076 type fixture struct {
1077 desc string
1078 context int
1079 color ColorConfig
1080 diff string
1081 patch Patch
1082 }