Kaynağa Gözat

- Naming enhancement for unified diff

Matthew Wang 11 yıl önce
ebeveyn
işleme
7881a41f69
2 değiştirilmiş dosya ile 33 ekleme ve 33 silme
  1. 12
    12
      cdiff.py
  2. 21
    21
      tests/test_cdiff.py

+ 12
- 12
cdiff.py Dosyayı Görüntüle

@@ -367,7 +367,7 @@ class Diff(DiffOps):
367 367
         return colorize(line, base_color)
368 368
 
369 369
 
370
-class Udiff(Diff):
370
+class UnifiedDiff(Diff):
371 371
 
372 372
     def is_old_path(self, line):
373 373
         return line.startswith('--- ')
@@ -377,7 +377,8 @@ class Udiff(Diff):
377 377
 
378 378
     def is_hunk_meta(self, line):
379 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 383
         return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
383 384
                (line.startswith('## -') and line.find(' ##') >= 8)
@@ -467,17 +468,16 @@ class PatchStream(object):
467 468
 class DiffParser(object):
468 469
 
469 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 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 475
         size = len(header)
476
+
477 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 481
                 break
482 482
         else:
483 483
             if size < 4:
@@ -485,14 +485,14 @@ class DiffParser(object):
485 485
                 # more than 3 lines, happens with `git diff` on a file that
486 486
                 # only has perm bits changes
487 487
                 #
488
-                self._type = 'udiff'
488
+                self._type = 'unified'
489 489
             else:
490 490
                 raise RuntimeError('unknown diff type')
491 491
 
492 492
     def get_diff_generator(self):
493 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 496
         else:
497 497
             raise RuntimeError('unsupported diff format')
498 498
 

+ 21
- 21
tests/test_cdiff.py Dosyayı Görüntüle

@@ -41,7 +41,7 @@ class Sequential(object):
41 41
         return item
42 42
 
43 43
 
44
-class TestPatchStream(unittest.TestCase):
44
+class PatchStreamTest(unittest.TestCase):
45 45
 
46 46
     def test_is_empty(self):
47 47
         stream = cdiff.PatchStream(Sequential([]))
@@ -71,7 +71,7 @@ class TestPatchStream(unittest.TestCase):
71 71
         self.assertEqual(out, items)
72 72
 
73 73
 
74
-class TestHunk(unittest.TestCase):
74
+class HunkTest(unittest.TestCase):
75 75
 
76 76
     def test_get_old_text(self):
77 77
         hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@', (1, 2), (1, 2))
@@ -88,7 +88,7 @@ class TestHunk(unittest.TestCase):
88 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 93
     def _init_diff(self):
94 94
         """Return a minimal diff contains all required samples
@@ -367,9 +367,9 @@ class TestDiff(unittest.TestCase):
367 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 374
     def test_is_hunk_meta_normal(self):
375 375
         self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
@@ -421,10 +421,10 @@ class TestUdiff(unittest.TestCase):
421 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 426
     def test_type_detect(self):
427
-        patch = r"""\
427
+        patch = """\
428 428
 spam
429 429
 --- a
430 430
 +++ b
@@ -433,10 +433,10 @@ spam
433 433
         items = patch.splitlines(True)
434 434
         stream = cdiff.PatchStream(Sequential(items))
435 435
         parser = cdiff.DiffParser(stream)
436
-        self.assertEqual(parser._type, 'udiff')
436
+        self.assertEqual(parser._type, 'unified')
437 437
 
438 438
     def test_type_detect_neg(self):
439
-        patch = r"""\
439
+        patch = """\
440 440
 spam
441 441
 --- a
442 442
 spam
@@ -447,7 +447,7 @@ spam
447 447
         self.assertRaises(RuntimeError, cdiff.DiffParser, stream)
448 448
 
449 449
     def test_parse_invalid_hunk_meta(self):
450
-        patch = r"""\
450
+        patch = """\
451 451
 spam
452 452
 --- a
453 453
 +++ b
@@ -460,7 +460,7 @@ spam
460 460
         self.assertRaises(RuntimeError, list, parser.get_diff_generator())
461 461
 
462 462
     def test_parse_dangling_header(self):
463
-        patch = r"""\
463
+        patch = """\
464 464
 --- a
465 465
 +++ b
466 466
 @@ -1,2 +1,2 @@
@@ -482,7 +482,7 @@ spam
482 482
         self.assertEqual(len(out[1]._hunks), 0)
483 483
 
484 484
     def test_parse_missing_new_path(self):
485
-        patch = r"""\
485
+        patch = """\
486 486
 --- a
487 487
 +++ b
488 488
 @@ -1,2 +1,2 @@
@@ -497,7 +497,7 @@ spam
497 497
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
498 498
 
499 499
     def test_parse_missing_hunk_meta(self):
500
-        patch = r"""\
500
+        patch = """\
501 501
 --- a
502 502
 +++ b
503 503
 @@ -1,2 +1,2 @@
@@ -519,7 +519,7 @@ spam
519 519
         self.assertEqual(len(out[1]._hunks), 0)
520 520
 
521 521
     def test_parse_missing_hunk_list(self):
522
-        patch = r"""\
522
+        patch = """\
523 523
 --- a
524 524
 +++ b
525 525
 @@ -1,2 +1,2 @@
@@ -536,7 +536,7 @@ spam
536 536
         self.assertRaises(AssertionError, list, parser.get_diff_generator())
537 537
 
538 538
     def test_parse_only_in_dir(self):
539
-        patch = r"""\
539
+        patch = """\
540 540
 --- a
541 541
 +++ b
542 542
 @@ -1,2 +1,2 @@
@@ -563,7 +563,7 @@ Only in foo: foo
563 563
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
564 564
 
565 565
     def test_parse_only_in_dir_at_last(self):
566
-        patch = r"""\
566
+        patch = """\
567 567
 --- a
568 568
 +++ b
569 569
 @@ -1,2 +1,2 @@
@@ -582,7 +582,7 @@ Only in foo: foo
582 582
         self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
583 583
 
584 584
     def test_parse_binary_differ_diff_ru(self):
585
-        patch = r"""\
585
+        patch = """\
586 586
 --- a
587 587
 +++ b
588 588
 @@ -1,2 +1,2 @@
@@ -612,7 +612,7 @@ Binary files a/1.pdf and b/1.pdf differ
612 612
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
613 613
 
614 614
     def test_parse_binary_differ_git(self):
615
-        patch = r"""\
615
+        patch = """\
616 616
 diff --git a/foo b/foo
617 617
 index 529d8a3..ad71911 100755
618 618
 --- a/foo
@@ -648,13 +648,13 @@ index 529e8a3..ad71921 100755
648 648
         self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
649 649
 
650 650
     def test_parse_svn_prop(self):
651
-        patch = r"""\
651
+        patch = """\
652 652
 --- a
653 653
 +++ b
654 654
 Added: svn:executable
655 655
 ## -0,0 +1 ##
656 656
 +*
657
-\ No newline at end of property
657
+\\ No newline at end of property
658 658
 Added: svn:keywords
659 659
 ## -0,0 +1 ##
660 660
 +Id
@@ -671,7 +671,7 @@ Added: svn:keywords
671 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 676
     def setUp(self):
677 677
         self._cwd = os.getcwd()