Browse Source

- Naming enhancement for unified diff

Matthew Wang 11 years ago
parent
commit
7881a41f69
2 changed files with 33 additions and 33 deletions
  1. 12
    12
      cdiff.py
  2. 21
    21
      tests/test_cdiff.py

+ 12
- 12
cdiff.py View File

367
         return colorize(line, base_color)
367
         return colorize(line, base_color)
368
 
368
 
369
 
369
 
370
-class Udiff(Diff):
370
+class UnifiedDiff(Diff):
371
 
371
 
372
     def is_old_path(self, line):
372
     def is_old_path(self, line):
373
         return line.startswith('--- ')
373
         return line.startswith('--- ')
377
 
377
 
378
     def is_hunk_meta(self, line):
378
     def is_hunk_meta(self, line):
379
         """Minimal valid hunk meta is like '@@ -1 +1 @@', note extra chars
379
         """Minimal valid hunk meta is like '@@ -1 +1 @@', note extra chars
380
-        might occur after the ending @@, e.g. in git log
380
+        might occur after the ending @@, e.g. in git log.  '## ' uaually
381
+        indicates svn property changes in output from `svn log --diff`
381
         """
382
         """
382
         return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
383
         return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
383
                (line.startswith('## -') and line.find(' ##') >= 8)
384
                (line.startswith('## -') and line.find(' ##') >= 8)
467
 class DiffParser(object):
468
 class DiffParser(object):
468
 
469
 
469
     def __init__(self, stream):
470
     def __init__(self, stream):
470
-        """Detect Udiff with 3 conditions, '## ' uaually indicates svn property
471
-        changes in output from `svn log --diff`
472
-        """
473
         self._stream = stream
471
         self._stream = stream
474
 
472
 
475
-        header = self._stream.read_stream_header(100)
473
+        header = [decode(line) for line in
474
+                  self._stream.read_stream_header(100)]
476
         size = len(header)
475
         size = len(header)
476
+
477
         for n in range(size):
477
         for n in range(size):
478
-            if decode(header[n]).startswith('--- ') and (n < size - 1) and \
479
-                    decode(header[n+1]).startswith('+++ '):
480
-                self._type = 'udiff'
478
+            if header[n].startswith('--- ') and (n < size - 1) and \
479
+                    header[n+1].startswith('+++ '):
480
+                self._type = 'unified'
481
                 break
481
                 break
482
         else:
482
         else:
483
             if size < 4:
483
             if size < 4:
485
                 # more than 3 lines, happens with `git diff` on a file that
485
                 # more than 3 lines, happens with `git diff` on a file that
486
                 # only has perm bits changes
486
                 # only has perm bits changes
487
                 #
487
                 #
488
-                self._type = 'udiff'
488
+                self._type = 'unified'
489
             else:
489
             else:
490
                 raise RuntimeError('unknown diff type')
490
                 raise RuntimeError('unknown diff type')
491
 
491
 
492
     def get_diff_generator(self):
492
     def get_diff_generator(self):
493
         """parse all diff lines, construct a list of Diff objects"""
493
         """parse all diff lines, construct a list of Diff objects"""
494
-        if self._type == 'udiff':
495
-            difflet = Udiff(None, None, None, None)
494
+        if self._type == 'unified':
495
+            difflet = UnifiedDiff(None, None, None, None)
496
         else:
496
         else:
497
             raise RuntimeError('unsupported diff format')
497
             raise RuntimeError('unsupported diff format')
498
 
498
 

+ 21
- 21
tests/test_cdiff.py View File

41
         return item
41
         return item
42
 
42
 
43
 
43
 
44
-class TestPatchStream(unittest.TestCase):
44
+class PatchStreamTest(unittest.TestCase):
45
 
45
 
46
     def test_is_empty(self):
46
     def test_is_empty(self):
47
         stream = cdiff.PatchStream(Sequential([]))
47
         stream = cdiff.PatchStream(Sequential([]))
71
         self.assertEqual(out, items)
71
         self.assertEqual(out, items)
72
 
72
 
73
 
73
 
74
-class TestHunk(unittest.TestCase):
74
+class HunkTest(unittest.TestCase):
75
 
75
 
76
     def test_get_old_text(self):
76
     def test_get_old_text(self):
77
         hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@', (1, 2), (1, 2))
77
         hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@', (1, 2), (1, 2))
88
         self.assertEqual(hunk._get_new_text(), ['bar\n', 'common\n'])
88
         self.assertEqual(hunk._get_new_text(), ['bar\n', 'common\n'])
89
 
89
 
90
 
90
 
91
-class TestDiff(unittest.TestCase):
91
+class DiffTest(unittest.TestCase):
92
 
92
 
93
     def _init_diff(self):
93
     def _init_diff(self):
94
         """Return a minimal diff contains all required samples
94
         """Return a minimal diff contains all required samples
367
             '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
367
             '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
368
 
368
 
369
 
369
 
370
-class TestUdiff(unittest.TestCase):
370
+class UnifiedDiffTest(unittest.TestCase):
371
 
371
 
372
-    diff = cdiff.Udiff(None, None, None, None)
372
+    diff = cdiff.UnifiedDiff(None, None, None, None)
373
 
373
 
374
     def test_is_hunk_meta_normal(self):
374
     def test_is_hunk_meta_normal(self):
375
         self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
375
         self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
421
         self.assertFalse(self.diff.is_new('+++ considered as new path'))
421
         self.assertFalse(self.diff.is_new('+++ considered as new path'))
422
 
422
 
423
 
423
 
424
-class TestDiffParser(unittest.TestCase):
424
+class DiffParserTest(unittest.TestCase):
425
 
425
 
426
     def test_type_detect(self):
426
     def test_type_detect(self):
427
-        patch = r"""\
427
+        patch = """\
428
 spam
428
 spam
429
 --- a
429
 --- a
430
 +++ b
430
 +++ b
433
         items = patch.splitlines(True)
433
         items = patch.splitlines(True)
434
         stream = cdiff.PatchStream(Sequential(items))
434
         stream = cdiff.PatchStream(Sequential(items))
435
         parser = cdiff.DiffParser(stream)
435
         parser = cdiff.DiffParser(stream)
436
-        self.assertEqual(parser._type, 'udiff')
436
+        self.assertEqual(parser._type, 'unified')
437
 
437
 
438
     def test_type_detect_neg(self):
438
     def test_type_detect_neg(self):
439
-        patch = r"""\
439
+        patch = """\
440
 spam
440
 spam
441
 --- a
441
 --- a
442
 spam
442
 spam
447
         self.assertRaises(RuntimeError, cdiff.DiffParser, stream)
447
         self.assertRaises(RuntimeError, cdiff.DiffParser, stream)
448
 
448
 
449
     def test_parse_invalid_hunk_meta(self):
449
     def test_parse_invalid_hunk_meta(self):
450
-        patch = r"""\
450
+        patch = """\
451
 spam
451
 spam
452
 --- a
452
 --- a
453
 +++ b
453
 +++ b
460
         self.assertRaises(RuntimeError, list, parser.get_diff_generator())
460
         self.assertRaises(RuntimeError, list, parser.get_diff_generator())
461
 
461
 
462
     def test_parse_dangling_header(self):
462
     def test_parse_dangling_header(self):
463
-        patch = r"""\
463
+        patch = """\
464
 --- a
464
 --- a
465
 +++ b
465
 +++ b
466
 @@ -1,2 +1,2 @@
466
 @@ -1,2 +1,2 @@
482
         self.assertEqual(len(out[1]._hunks), 0)
482
         self.assertEqual(len(out[1]._hunks), 0)
483
 
483
 
484
     def test_parse_missing_new_path(self):
484
     def test_parse_missing_new_path(self):
485
-        patch = r"""\
485
+        patch = """\
486
 --- a
486
 --- a
487
 +++ b
487
 +++ b
488
 @@ -1,2 +1,2 @@
488
 @@ -1,2 +1,2 @@
497
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
497
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
498
 
498
 
499
     def test_parse_missing_hunk_meta(self):
499
     def test_parse_missing_hunk_meta(self):
500
-        patch = r"""\
500
+        patch = """\
501
 --- a
501
 --- a
502
 +++ b
502
 +++ b
503
 @@ -1,2 +1,2 @@
503
 @@ -1,2 +1,2 @@
519
         self.assertEqual(len(out[1]._hunks), 0)
519
         self.assertEqual(len(out[1]._hunks), 0)
520
 
520
 
521
     def test_parse_missing_hunk_list(self):
521
     def test_parse_missing_hunk_list(self):
522
-        patch = r"""\
522
+        patch = """\
523
 --- a
523
 --- a
524
 +++ b
524
 +++ b
525
 @@ -1,2 +1,2 @@
525
 @@ -1,2 +1,2 @@
536
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
536
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
537
 
537
 
538
     def test_parse_only_in_dir(self):
538
     def test_parse_only_in_dir(self):
539
-        patch = r"""\
539
+        patch = """\
540
 --- a
540
 --- a
541
 +++ b
541
 +++ b
542
 @@ -1,2 +1,2 @@
542
 @@ -1,2 +1,2 @@
563
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
563
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
564
 
564
 
565
     def test_parse_only_in_dir_at_last(self):
565
     def test_parse_only_in_dir_at_last(self):
566
-        patch = r"""\
566
+        patch = """\
567
 --- a
567
 --- a
568
 +++ b
568
 +++ b
569
 @@ -1,2 +1,2 @@
569
 @@ -1,2 +1,2 @@
582
         self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
582
         self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
583
 
583
 
584
     def test_parse_binary_differ_diff_ru(self):
584
     def test_parse_binary_differ_diff_ru(self):
585
-        patch = r"""\
585
+        patch = """\
586
 --- a
586
 --- a
587
 +++ b
587
 +++ b
588
 @@ -1,2 +1,2 @@
588
 @@ -1,2 +1,2 @@
612
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
612
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
613
 
613
 
614
     def test_parse_binary_differ_git(self):
614
     def test_parse_binary_differ_git(self):
615
-        patch = r"""\
615
+        patch = """\
616
 diff --git a/foo b/foo
616
 diff --git a/foo b/foo
617
 index 529d8a3..ad71911 100755
617
 index 529d8a3..ad71911 100755
618
 --- a/foo
618
 --- a/foo
648
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
648
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
649
 
649
 
650
     def test_parse_svn_prop(self):
650
     def test_parse_svn_prop(self):
651
-        patch = r"""\
651
+        patch = """\
652
 --- a
652
 --- a
653
 +++ b
653
 +++ b
654
 Added: svn:executable
654
 Added: svn:executable
655
 ## -0,0 +1 ##
655
 ## -0,0 +1 ##
656
 +*
656
 +*
657
-\ No newline at end of property
657
+\\ No newline at end of property
658
 Added: svn:keywords
658
 Added: svn:keywords
659
 ## -0,0 +1 ##
659
 ## -0,0 +1 ##
660
 +Id
660
 +Id
671
         self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
671
         self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
672
 
672
 
673
 
673
 
674
-class TestMain(unittest.TestCase):
674
+class MainTest(unittest.TestCase):
675
 
675
 
676
     def setUp(self):
676
     def setUp(self):
677
         self._cwd = os.getcwd()
677
         self._cwd = os.getcwd()