|
8 | 8 | from git import *
|
9 | 9 |
|
10 | 10 | class TestDiff(TestBase):
|
11 |
| - |
12 |
| - def _assert_diff_format(self, diffs): |
13 |
| - # verify that the format of the diff is sane |
14 |
| - for diff in diffs: |
15 |
| - if diff.a_blob: |
16 |
| - assert not diff.a_blob.path.endswith('\n') |
17 |
| - if diff.b_blob: |
18 |
| - assert not diff.b_blob.path.endswith('\n') |
19 |
| - # END for each diff |
20 |
| - return diffs |
21 |
| - |
22 |
| - def test_list_from_string_new_mode(self): |
23 |
| - output = StringProcessAdapter(fixture('diff_new_mode')) |
24 |
| - diffs = Diff._index_from_patch_format(self.rorepo, output.stdout) |
25 |
| - self._assert_diff_format(diffs) |
26 |
| - |
27 |
| - assert_equal(1, len(diffs)) |
28 |
| - assert_equal(10, len(diffs[0].diff.splitlines())) |
| 11 | + |
| 12 | + def _assert_diff_format(self, diffs): |
| 13 | + # verify that the format of the diff is sane |
| 14 | + for diff in diffs: |
| 15 | + if diff.a_mode: |
| 16 | + assert isinstance(diff.a_mode, int) |
| 17 | + if diff.b_mode: |
| 18 | + assert isinstance(diff.b_mode, int) |
| 19 | + |
| 20 | + if diff.a_blob: |
| 21 | + assert not diff.a_blob.path.endswith('\n') |
| 22 | + if diff.b_blob: |
| 23 | + assert not diff.b_blob.path.endswith('\n') |
| 24 | + # END for each diff |
| 25 | + return diffs |
| 26 | + |
| 27 | + def test_list_from_string_new_mode(self): |
| 28 | + output = StringProcessAdapter(fixture('diff_new_mode')) |
| 29 | + diffs = Diff._index_from_patch_format(self.rorepo, output.stdout) |
| 30 | + self._assert_diff_format(diffs) |
| 31 | + |
| 32 | + assert_equal(1, len(diffs)) |
| 33 | + assert_equal(10, len(diffs[0].diff.splitlines())) |
29 | 34 |
|
30 |
| - def test_diff_with_rename(self): |
31 |
| - output = StringProcessAdapter(fixture('diff_rename')) |
32 |
| - diffs = Diff._index_from_patch_format(self.rorepo, output.stdout) |
33 |
| - self._assert_diff_format(diffs) |
34 |
| - |
35 |
| - assert_equal(1, len(diffs)) |
| 35 | + def test_diff_with_rename(self): |
| 36 | + output = StringProcessAdapter(fixture('diff_rename')) |
| 37 | + diffs = Diff._index_from_patch_format(self.rorepo, output.stdout) |
| 38 | + self._assert_diff_format(diffs) |
| 39 | + |
| 40 | + assert_equal(1, len(diffs)) |
36 | 41 |
|
37 |
| - diff = diffs[0] |
38 |
| - assert_true(diff.renamed) |
39 |
| - assert_equal(diff.rename_from, 'AUTHORS') |
40 |
| - assert_equal(diff.rename_to, 'CONTRIBUTORS') |
| 42 | + diff = diffs[0] |
| 43 | + assert_true(diff.renamed) |
| 44 | + assert_equal(diff.rename_from, 'AUTHORS') |
| 45 | + assert_equal(diff.rename_to, 'CONTRIBUTORS') |
41 | 46 |
|
42 |
| - def test_diff_patch_format(self): |
43 |
| - # test all of the 'old' format diffs for completness - it should at least |
44 |
| - # be able to deal with it |
45 |
| - fixtures = ("diff_2", "diff_2f", "diff_f", "diff_i", "diff_mode_only", |
46 |
| - "diff_new_mode", "diff_numstat", "diff_p", "diff_rename", |
47 |
| - "diff_tree_numstat_root" ) |
48 |
| - |
49 |
| - for fixture_name in fixtures: |
50 |
| - diff_proc = StringProcessAdapter(fixture(fixture_name)) |
51 |
| - diffs = Diff._index_from_patch_format(self.rorepo, diff_proc.stdout) |
52 |
| - # END for each fixture |
| 47 | + def test_diff_patch_format(self): |
| 48 | + # test all of the 'old' format diffs for completness - it should at least |
| 49 | + # be able to deal with it |
| 50 | + fixtures = ("diff_2", "diff_2f", "diff_f", "diff_i", "diff_mode_only", |
| 51 | + "diff_new_mode", "diff_numstat", "diff_p", "diff_rename", |
| 52 | + "diff_tree_numstat_root" ) |
| 53 | + |
| 54 | + for fixture_name in fixtures: |
| 55 | + diff_proc = StringProcessAdapter(fixture(fixture_name)) |
| 56 | + diffs = Diff._index_from_patch_format(self.rorepo, diff_proc.stdout) |
| 57 | + # END for each fixture |
53 | 58 |
|
54 |
| - def test_diff_interface(self): |
55 |
| - # test a few variations of the main diff routine |
56 |
| - assertion_map = dict() |
57 |
| - for i, commit in enumerate(self.rorepo.iter_commits('0.1.6', max_count=2)): |
58 |
| - diff_item = commit |
59 |
| - if i%2 == 0: |
60 |
| - diff_item = commit.tree |
61 |
| - # END use tree every second item |
62 |
| - |
63 |
| - for other in (None, commit.Index, commit.parents[0]): |
64 |
| - for paths in (None, "CHANGES", ("CHANGES", "lib")): |
65 |
| - for create_patch in range(2): |
66 |
| - diff_index = diff_item.diff(other, paths, create_patch) |
67 |
| - assert isinstance(diff_index, DiffIndex) |
68 |
| - |
69 |
| - if diff_index: |
70 |
| - self._assert_diff_format(diff_index) |
71 |
| - for ct in DiffIndex.change_type: |
72 |
| - key = 'ct_%s'%ct |
73 |
| - assertion_map.setdefault(key, 0) |
74 |
| - assertion_map[key] = assertion_map[key]+len(list(diff_index.iter_change_type(ct))) |
75 |
| - # END for each changetype |
76 |
| - |
77 |
| - # check entries |
78 |
| - diff_set = set() |
79 |
| - diff_set.add(diff_index[0]) |
80 |
| - diff_set.add(diff_index[0]) |
81 |
| - assert len(diff_set) == 1 |
82 |
| - assert diff_index[0] == diff_index[0] |
83 |
| - assert not (diff_index[0] != diff_index[0]) |
84 |
| - # END diff index checking |
85 |
| - # END for each patch option |
86 |
| - # END for each path option |
87 |
| - # END for each other side |
88 |
| - # END for each commit |
89 |
| - |
90 |
| - # assert we could always find at least one instance of the members we |
91 |
| - # can iterate in the diff index - if not this indicates its not working correctly |
92 |
| - # or our test does not span the whole range of possibilities |
93 |
| - for key,value in assertion_map.items(): |
94 |
| - assert value, "Did not find diff for %s" % key |
95 |
| - # END for each iteration type |
96 |
| - |
97 |
| - # test path not existing in the index - should be ignored |
98 |
| - c = self.rorepo.head.commit |
99 |
| - cp = c.parents[0] |
100 |
| - diff_index = c.diff(cp, ["does/not/exist"]) |
101 |
| - assert len(diff_index) == 0 |
102 |
| - |
103 |
| - |
| 59 | + def test_diff_interface(self): |
| 60 | + # test a few variations of the main diff routine |
| 61 | + assertion_map = dict() |
| 62 | + for i, commit in enumerate(self.rorepo.iter_commits('0.1.6', max_count=2)): |
| 63 | + diff_item = commit |
| 64 | + if i%2 == 0: |
| 65 | + diff_item = commit.tree |
| 66 | + # END use tree every second item |
| 67 | + |
| 68 | + for other in (None, commit.Index, commit.parents[0]): |
| 69 | + for paths in (None, "CHANGES", ("CHANGES", "lib")): |
| 70 | + for create_patch in range(2): |
| 71 | + diff_index = diff_item.diff(other, paths, create_patch) |
| 72 | + assert isinstance(diff_index, DiffIndex) |
| 73 | + |
| 74 | + if diff_index: |
| 75 | + self._assert_diff_format(diff_index) |
| 76 | + for ct in DiffIndex.change_type: |
| 77 | + key = 'ct_%s'%ct |
| 78 | + assertion_map.setdefault(key, 0) |
| 79 | + assertion_map[key] = assertion_map[key]+len(list(diff_index.iter_change_type(ct))) |
| 80 | + # END for each changetype |
| 81 | + |
| 82 | + # check entries |
| 83 | + diff_set = set() |
| 84 | + diff_set.add(diff_index[0]) |
| 85 | + diff_set.add(diff_index[0]) |
| 86 | + assert len(diff_set) == 1 |
| 87 | + assert diff_index[0] == diff_index[0] |
| 88 | + assert not (diff_index[0] != diff_index[0]) |
| 89 | + # END diff index checking |
| 90 | + # END for each patch option |
| 91 | + # END for each path option |
| 92 | + # END for each other side |
| 93 | + # END for each commit |
| 94 | + |
| 95 | + # assert we could always find at least one instance of the members we |
| 96 | + # can iterate in the diff index - if not this indicates its not working correctly |
| 97 | + # or our test does not span the whole range of possibilities |
| 98 | + for key,value in assertion_map.items(): |
| 99 | + assert value, "Did not find diff for %s" % key |
| 100 | + # END for each iteration type |
| 101 | + |
| 102 | + # test path not existing in the index - should be ignored |
| 103 | + c = self.rorepo.head.commit |
| 104 | + cp = c.parents[0] |
| 105 | + diff_index = c.diff(cp, ["does/not/exist"]) |
| 106 | + assert len(diff_index) == 0 |
| 107 | + |
| 108 | + |
0 commit comments