/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
1
import bzrlib
2
import unittest
3
from StringIO import StringIO
4
5
from bzrlib.diff import internal_diff
6
from read_changeset import ChangesetTree
7
8
class MockTree(object):
9
    def __init__(self):
10
        object.__init__(self)
11
        self.paths = {}
12
        self.ids = {}
13
        self.contents = {}
14
15
    def __iter__(self):
16
        return self.paths.iterkeys()
17
18
    def add_dir(self, file_id, path):
19
        self.paths[file_id] = path
20
        self.ids[path] = file_id
21
    
22
    def add_file(self, file_id, path, contents):
23
        self.add_dir(file_id, path)
24
        self.contents[file_id] = contents
25
26
    def path2id(self, path):
27
        return self.ids.get(path)
28
29
    def id2path(self, file_id):
30
        return self.paths.get(file_id)
31
32
    def has_id(self, file_id):
33
        return self.id2path(file_id) is not None
34
35
    def get_file(self, file_id):
36
        result = StringIO()
37
        result.write(self.contents[file_id])
38
        result.seek(0,0)
39
        return result
40
41
class CTreeTester(unittest.TestCase):
42
    """A simple unittest tester for the ChangesetTree class."""
43
44
    def make_tree_1(self):
45
        mtree = MockTree()
46
        mtree.add_dir("a", "grandparent")
47
        mtree.add_dir("b", "grandparent/parent")
48
        mtree.add_file("c", "grandparent/parent/file", "Hello\n")
49
        mtree.add_dir("d", "grandparent/alt_parent")
50
        return ChangesetTree(mtree), mtree
51
        
52
    def test_renames(self):
53
        """Ensure that file renames have the proper effect on children"""
54
        ctree = self.make_tree_1()[0]
55
        self.assertEqual(ctree.old_path("grandparent"), "grandparent")
56
        self.assertEqual(ctree.old_path("grandparent/parent"), "grandparent/parent")
57
        self.assertEqual(ctree.old_path("grandparent/parent/file"),
58
            "grandparent/parent/file")
59
60
        self.assertEqual(ctree.id2path("a"), "grandparent")
61
        self.assertEqual(ctree.id2path("b"), "grandparent/parent")
62
        self.assertEqual(ctree.id2path("c"), "grandparent/parent/file")
63
64
        self.assertEqual(ctree.path2id("grandparent"), "a")
65
        self.assertEqual(ctree.path2id("grandparent/parent"), "b")
66
        self.assertEqual(ctree.path2id("grandparent/parent/file"), "c")
67
68
        assert ctree.path2id("grandparent2") is None
69
        assert ctree.path2id("grandparent2/parent") is None
70
        assert ctree.path2id("grandparent2/parent/file") is None
71
72
        ctree.note_rename("grandparent", "grandparent2")
73
        assert ctree.old_path("grandparent") is None
74
        assert ctree.old_path("grandparent/parent") is None
75
        assert ctree.old_path("grandparent/parent/file") is None
76
77
        self.assertEqual(ctree.id2path("a"), "grandparent2")
78
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent")
79
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent/file")
80
81
        self.assertEqual(ctree.path2id("grandparent2"), "a")
82
        self.assertEqual(ctree.path2id("grandparent2/parent"), "b")
83
        self.assertEqual(ctree.path2id("grandparent2/parent/file"), "c")
84
85
        assert ctree.path2id("grandparent") is None
86
        assert ctree.path2id("grandparent/parent") is None
87
        assert ctree.path2id("grandparent/parent/file") is None
88
89
        ctree.note_rename("grandparent/parent", "grandparent2/parent2")
90
        self.assertEqual(ctree.id2path("a"), "grandparent2")
91
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent2")
92
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent2/file")
93
94
        self.assertEqual(ctree.path2id("grandparent2"), "a")
95
        self.assertEqual(ctree.path2id("grandparent2/parent2"), "b")
96
        self.assertEqual(ctree.path2id("grandparent2/parent2/file"), "c")
97
98
        assert ctree.path2id("grandparent2/parent") is None
99
        assert ctree.path2id("grandparent2/parent/file") is None
100
101
        ctree.note_rename("grandparent/parent/file", 
102
                          "grandparent2/parent2/file2")
103
        self.assertEqual(ctree.id2path("a"), "grandparent2")
104
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent2")
105
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent2/file2")
106
107
        self.assertEqual(ctree.path2id("grandparent2"), "a")
108
        self.assertEqual(ctree.path2id("grandparent2/parent2"), "b")
109
        self.assertEqual(ctree.path2id("grandparent2/parent2/file2"), "c")
110
111
        assert ctree.path2id("grandparent2/parent2/file") is None
112
113
    def test_moves(self):
114
        """Ensure that file moves have the proper effect on children"""
115
        ctree = self.make_tree_1()[0]
116
        ctree.note_rename("grandparent/parent/file", 
117
                          "grandparent/alt_parent/file")
118
        self.assertEqual(ctree.id2path("c"), "grandparent/alt_parent/file")
119
        self.assertEqual(ctree.path2id("grandparent/alt_parent/file"), "c")
120
        assert ctree.path2id("grandparent/parent/file") is None
121
122
    def unified_diff(self, old, new):
123
        out = StringIO()
124
        internal_diff("old", old, "new", new, out)
125
        out.seek(0,0)
126
        return out.read()
127
128
    def make_tree_2(self):
129
        ctree = self.make_tree_1()[0]
130
        ctree.note_rename("grandparent/parent/file", 
131
                          "grandparent/alt_parent/file")
132
        assert ctree.id2path("e") is None
133
        assert ctree.path2id("grandparent/parent/file") is None
134
        ctree.note_id("e", "grandparent/parent/file")
135
        return ctree
136
137
    def test_adds(self):
138
        """File/inventory adds"""
139
        ctree = self.make_tree_2()
140
        add_patch = self.unified_diff([], ["Extra cheese\n"])
141
        ctree.note_patch("grandparent/parent/file", add_patch)
142
        self.adds_test(ctree)
143
144
    def adds_test(self, ctree):
145
        self.assertEqual(ctree.id2path("e"), "grandparent/parent/file")
146
        self.assertEqual(ctree.path2id("grandparent/parent/file"), "e")
147
        self.assertEqual(ctree.get_file("e").read(), "Extra cheese\n")
148
149
    def test_adds2(self):
150
        """File/inventory adds, with patch-compatibile renames"""
151
        ctree = self.make_tree_2()
152
        ctree.contents_by_id = False
153
        add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
154
        ctree.note_patch("grandparent/parent/file", add_patch)
155
        self.adds_test(ctree)
156
157
    def make_tree_3(self):
158
        ctree, mtree = self.make_tree_1()
159
        mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
160
        ctree.note_rename("grandparent/parent/file", 
161
                          "grandparent/alt_parent/file")
162
        ctree.note_rename("grandparent/parent/topping", 
163
                          "grandparent/alt_parent/stopping")
164
        return ctree
165
166
    def get_file_test(self, ctree):
167
        self.assertEqual(ctree.get_file("e").read(), "Lemon\n")
168
        self.assertEqual(ctree.get_file("c").read(), "Hello\n")
169
170
    def test_get_file(self):
171
        """Get file contents"""
172
        ctree = self.make_tree_3()
173
        mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
174
        ctree.note_patch("grandparent/alt_parent/stopping", mod_patch)
175
        self.get_file_test(ctree)
176
177
    def test_get_file2(self):
178
        """Get file contents, with patch-compatibile renames"""
179
        ctree = self.make_tree_3()
180
        ctree.contents_by_id = False
181
        mod_patch = self.unified_diff([], ["Lemon\n"])
182
        ctree.note_patch("grandparent/alt_parent/stopping", mod_patch)
183
        mod_patch = self.unified_diff([], ["Hello\n"])
184
        ctree.note_patch("grandparent/alt_parent/file", mod_patch)
185
        self.get_file_test(ctree)
186
187
    def test_delete(self):
188
        "Deletion by changeset"
189
        ctree = self.make_tree_1()[0]
190
        self.assertEqual(ctree.get_file("c").read(), "Hello\n")
191
        ctree.note_deletion("grandparent/parent/file")
192
        assert ctree.id2path("c") is None
193
        assert ctree.path2id("grandparent/parent/file") is None
194
195
    def sorted_ids(self, tree):
196
        ids = list(tree)
197
        ids.sort()
198
        return ids
199
200
    def test_iteration(self):
201
        """Ensure that iteration through ids works properly"""
202
        ctree = self.make_tree_1()[0]
203
        self.assertEqual(self.sorted_ids(ctree), ['a', 'b', 'c', 'd'])
204
        ctree.note_deletion("grandparent/parent/file")
205
        ctree.note_id("e", "grandparent/alt_parent/fool")
206
        self.assertEqual(self.sorted_ids(ctree), ['a', 'b', 'd', 'e'])
207
208
def test():
209
    patchesTestSuite = unittest.makeSuite(CTreeTester,'test_')
210
    runner = unittest.TextTestRunner()
211
    runner.run(patchesTestSuite)
212
213
214