/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.selftest import InTempDir
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
6
7
from bzrlib.diff import internal_diff
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
8
from read_changeset import ChangesetTree
9
10
class MockTree(object):
11
    def __init__(self):
12
        from bzrlib.inventory import RootEntry, ROOT_ID
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
13
        object.__init__(self)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
14
        self.paths = {ROOT_ID: ""}
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
15
        self.ids = {"": ROOT_ID}
16
        self.contents = {}
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
17
        self.root = RootEntry(ROOT_ID)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
18
19
    inventory = property(lambda x:x)
20
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
21
    def __iter__(self):
22
        return self.paths.iterkeys()
23
24
    def __getitem__(self, file_id):
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
25
        if file_id == self.root.file_id:
26
            return self.root
27
        else:
28
            return self.make_entry(file_id, self.paths[file_id])
29
30
    def parent_id(self, file_id):
31
        from os.path import dirname
32
        parent_dir = dirname(self.paths[file_id])
33
        if parent_dir == "":
34
            return None
35
        return self.ids[parent_dir]
36
37
    def iter_entries(self):
38
        for path, file_id in self.ids.iteritems():
39
            yield path, self[file_id]
40
41
    def get_file_kind(self, file_id):
42
        if file_id in self.contents:
43
            kind = 'file'
44
        else:
45
            kind = 'directory'
46
        return kind
47
48
    def make_entry(self, file_id, path):
49
        from os.path import basename
50
        from bzrlib.inventory import InventoryEntry
51
        name = basename(path)
52
        kind = self.get_file_kind(file_id)
53
        parent_id = self.parent_id(file_id)
54
        text_sha_1, text_size = self.contents_stats(file_id)
55
        ie = InventoryEntry(file_id, name, kind, parent_id)
56
        ie.text_sha_1 = text_sha_1
57
        ie.text_size = text_size
58
        return ie
59
60
    def add_dir(self, file_id, path):
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
61
        self.paths[file_id] = path
62
        self.ids[path] = file_id
63
    
64
    def add_file(self, file_id, path, contents):
65
        self.add_dir(file_id, path)
66
        self.contents[file_id] = contents
67
68
    def path2id(self, path):
69
        return self.ids.get(path)
70
71
    def id2path(self, file_id):
72
        return self.paths.get(file_id)
73
74
    def has_id(self, file_id):
75
        return self.id2path(file_id) is not None
76
77
    def get_file(self, file_id):
78
        result = StringIO()
79
        result.write(self.contents[file_id])
80
        result.seek(0,0)
81
        return result
82
83
    def contents_stats(self, file_id):
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
84
        from bzrlib.osutils import sha_file
85
        if file_id not in self.contents:
86
            return None, None
87
        text_sha1 = sha_file(self.get_file(file_id))
88
        return text_sha1, len(self.contents[file_id])
89
90
91
class CTreeTester(unittest.TestCase):
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
92
    """A simple unittest tester for the ChangesetTree class."""
93
94
    def make_tree_1(self):
95
        mtree = MockTree()
96
        mtree.add_dir("a", "grandparent")
97
        mtree.add_dir("b", "grandparent/parent")
98
        mtree.add_file("c", "grandparent/parent/file", "Hello\n")
99
        mtree.add_dir("d", "grandparent/alt_parent")
100
        return ChangesetTree(mtree), mtree
101
        
102
    def test_renames(self):
103
        """Ensure that file renames have the proper effect on children"""
104
        ctree = self.make_tree_1()[0]
105
        self.assertEqual(ctree.old_path("grandparent"), "grandparent")
106
        self.assertEqual(ctree.old_path("grandparent/parent"), "grandparent/parent")
107
        self.assertEqual(ctree.old_path("grandparent/parent/file"),
108
            "grandparent/parent/file")
109
110
        self.assertEqual(ctree.id2path("a"), "grandparent")
111
        self.assertEqual(ctree.id2path("b"), "grandparent/parent")
112
        self.assertEqual(ctree.id2path("c"), "grandparent/parent/file")
113
114
        self.assertEqual(ctree.path2id("grandparent"), "a")
115
        self.assertEqual(ctree.path2id("grandparent/parent"), "b")
116
        self.assertEqual(ctree.path2id("grandparent/parent/file"), "c")
117
118
        assert ctree.path2id("grandparent2") is None
119
        assert ctree.path2id("grandparent2/parent") is None
120
        assert ctree.path2id("grandparent2/parent/file") is None
121
122
        ctree.note_rename("grandparent", "grandparent2")
123
        assert ctree.old_path("grandparent") is None
124
        assert ctree.old_path("grandparent/parent") is None
125
        assert ctree.old_path("grandparent/parent/file") is None
126
127
        self.assertEqual(ctree.id2path("a"), "grandparent2")
128
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent")
129
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent/file")
130
131
        self.assertEqual(ctree.path2id("grandparent2"), "a")
132
        self.assertEqual(ctree.path2id("grandparent2/parent"), "b")
133
        self.assertEqual(ctree.path2id("grandparent2/parent/file"), "c")
134
135
        assert ctree.path2id("grandparent") is None
136
        assert ctree.path2id("grandparent/parent") is None
137
        assert ctree.path2id("grandparent/parent/file") is None
138
139
        ctree.note_rename("grandparent/parent", "grandparent2/parent2")
140
        self.assertEqual(ctree.id2path("a"), "grandparent2")
141
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent2")
142
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent2/file")
143
144
        self.assertEqual(ctree.path2id("grandparent2"), "a")
145
        self.assertEqual(ctree.path2id("grandparent2/parent2"), "b")
146
        self.assertEqual(ctree.path2id("grandparent2/parent2/file"), "c")
147
148
        assert ctree.path2id("grandparent2/parent") is None
149
        assert ctree.path2id("grandparent2/parent/file") is None
150
151
        ctree.note_rename("grandparent/parent/file", 
152
                          "grandparent2/parent2/file2")
153
        self.assertEqual(ctree.id2path("a"), "grandparent2")
154
        self.assertEqual(ctree.id2path("b"), "grandparent2/parent2")
155
        self.assertEqual(ctree.id2path("c"), "grandparent2/parent2/file2")
156
157
        self.assertEqual(ctree.path2id("grandparent2"), "a")
158
        self.assertEqual(ctree.path2id("grandparent2/parent2"), "b")
159
        self.assertEqual(ctree.path2id("grandparent2/parent2/file2"), "c")
160
161
        assert ctree.path2id("grandparent2/parent2/file") is None
162
163
    def test_moves(self):
164
        """Ensure that file moves have the proper effect on children"""
165
        ctree = self.make_tree_1()[0]
166
        ctree.note_rename("grandparent/parent/file", 
167
                          "grandparent/alt_parent/file")
168
        self.assertEqual(ctree.id2path("c"), "grandparent/alt_parent/file")
169
        self.assertEqual(ctree.path2id("grandparent/alt_parent/file"), "c")
170
        assert ctree.path2id("grandparent/parent/file") is None
171
172
    def unified_diff(self, old, new):
173
        out = StringIO()
174
        internal_diff("old", old, "new", new, out)
175
        out.seek(0,0)
176
        return out.read()
177
178
    def make_tree_2(self):
179
        ctree = self.make_tree_1()[0]
180
        ctree.note_rename("grandparent/parent/file", 
181
                          "grandparent/alt_parent/file")
182
        assert ctree.id2path("e") is None
183
        assert ctree.path2id("grandparent/parent/file") is None
184
        ctree.note_id("e", "grandparent/parent/file")
185
        return ctree
186
187
    def test_adds(self):
188
        """File/inventory adds"""
189
        ctree = self.make_tree_2()
190
        add_patch = self.unified_diff([], ["Extra cheese\n"])
191
        ctree.note_patch("grandparent/parent/file", add_patch)
192
        self.adds_test(ctree)
193
194
    def adds_test(self, ctree):
195
        self.assertEqual(ctree.id2path("e"), "grandparent/parent/file")
196
        self.assertEqual(ctree.path2id("grandparent/parent/file"), "e")
197
        self.assertEqual(ctree.get_file("e").read(), "Extra cheese\n")
198
199
    def test_adds2(self):
200
        """File/inventory adds, with patch-compatibile renames"""
201
        ctree = self.make_tree_2()
202
        ctree.contents_by_id = False
203
        add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
204
        ctree.note_patch("grandparent/parent/file", add_patch)
205
        self.adds_test(ctree)
206
207
    def make_tree_3(self):
208
        ctree, mtree = self.make_tree_1()
209
        mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
210
        ctree.note_rename("grandparent/parent/file", 
211
                          "grandparent/alt_parent/file")
212
        ctree.note_rename("grandparent/parent/topping", 
213
                          "grandparent/alt_parent/stopping")
214
        return ctree
215
216
    def get_file_test(self, ctree):
217
        self.assertEqual(ctree.get_file("e").read(), "Lemon\n")
218
        self.assertEqual(ctree.get_file("c").read(), "Hello\n")
219
220
    def test_get_file(self):
221
        """Get file contents"""
222
        ctree = self.make_tree_3()
223
        mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
224
        ctree.note_patch("grandparent/alt_parent/stopping", mod_patch)
225
        self.get_file_test(ctree)
226
227
    def test_get_file2(self):
228
        """Get file contents, with patch-compatibile renames"""
229
        ctree = self.make_tree_3()
230
        ctree.contents_by_id = False
231
        mod_patch = self.unified_diff([], ["Lemon\n"])
232
        ctree.note_patch("grandparent/alt_parent/stopping", mod_patch)
233
        mod_patch = self.unified_diff([], ["Hello\n"])
234
        ctree.note_patch("grandparent/alt_parent/file", mod_patch)
235
        self.get_file_test(ctree)
236
237
    def test_delete(self):
238
        "Deletion by changeset"
239
        ctree = self.make_tree_1()[0]
240
        self.assertEqual(ctree.get_file("c").read(), "Hello\n")
241
        ctree.note_deletion("grandparent/parent/file")
242
        assert ctree.id2path("c") is None
243
        assert ctree.path2id("grandparent/parent/file") is None
244
245
    def sorted_ids(self, tree):
246
        ids = list(tree)
247
        ids.sort()
248
        return ids
249
250
    def test_iteration(self):
251
        """Ensure that iteration through ids works properly"""
252
        ctree = self.make_tree_1()[0]
253
        self.assertEqual(self.sorted_ids(ctree), ['a', 'b', 'c', 'd'])
254
        ctree.note_deletion("grandparent/parent/file")
255
        ctree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
256
        self.assertEqual(self.sorted_ids(ctree), ['a', 'b', 'd', 'e'])
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
257
258
class CSetTester(InTempDir):
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
259
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
260
    def get_valid_cset(self, base_rev_id, rev_id):
261
        """Create a changeset from base_rev_id -> rev_id in built-in branch.
262
        Make sure that the text generated is valid, and that it
263
        can be applied against the base, and generate the same information.
264
        
265
        :return: The in-memory changeset
266
        """
267
        from cStringIO import StringIO
268
        from gen_changeset import show_changeset
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
269
        from read_changeset import read_changeset
270
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
271
        cset_txt = StringIO()
272
        show_changeset(self.b1, base_rev_id, self.b1, rev_id, to_file=cset_txt)
273
        cset_txt.seek(0)
274
        self.assertEqual(cset_txt.readline(), '# Bazaar-NG changeset v0.0.5\n')
275
        self.assertEqual(cset_txt.readline(), '# \n')
276
277
        rev = self.b1.get_revision(rev_id)
278
        self.assertEqual(cset_txt.readline(), '# committer: %s\n' % rev.committer)
279
280
        open(',,cset', 'wb').write(cset_txt.getvalue())
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
281
        cset_txt.seek(0)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
282
        # This should also validate the generate changeset
283
        cset = read_changeset(cset_txt, self.b1)
284
        info, tree = cset
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
285
        for cset_rev in info.real_revisions:
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
286
            # These really should have already been checked in read_changeset
287
            # since it computes the sha1 hash for the revision, which
288
            # only will match if everything is okay, but lets be
289
            # explicit about it
290
            branch_rev = self.b1.get_revision(cset_rev.revision_id)
291
            for a in ('inventory_id', 'inventory_sha1', 'revision_id',
292
                    'timestamp', 'timezone', 'message', 'committer'):
293
                self.assertEqual(getattr(branch_rev, a), getattr(cset_rev, a))
294
            self.assertEqual(len(branch_rev.parents), len(cset_rev.parents))
295
            for b_par, c_par in zip(branch_rev.parents, cset_rev.parents):
296
                self.assertEqual(b_par.revision_id, c_par.revision_id)
297
                # Foolishly, pending-merges generates parents which
298
                # may not have revision entries
299
                if b_par.revision_sha1 is None:
300
                    if b_par.revision_id in self.b1.revision_store:
301
                        sha1 = self.b1.get_revision_sha1(b_par.revision_id)
302
                    else:
303
                        sha1 = None
304
                else:
305
                    sha1 = b_par.revision_sha1
306
                if sha1 is not None:
307
                    self.assertEqual(sha1, c_par.revision_sha1)
308
309
        self.valid_apply_changeset(base_rev_id, cset)
310
311
        return cset
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
312
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
313
    def get_checkout(self, rev_id):
314
        """Get a new tree, with the specified revision in it.
315
        """
316
        from bzrlib.branch import find_branch
317
        import tempfile
318
        from bzrlib.merge import merge
319
320
        dirname = tempfile.mkdtemp(prefix='test-branch-', dir='.')
321
        to_branch = find_branch(dirname, init=True)
322
        # TODO: Once root ids are established, remove this if
323
        if hasattr(self.b1, 'get_root_id'):
324
            to_branch.set_root_id(self.b1.get_root_id())
325
        if rev_id is not None:
326
            # TODO Worry about making the root id of the branch
327
            # the same
328
            rh = self.b1.revision_history()
329
            self.assert_(rev_id in rh, 'Missing revision %s in base tree' % rev_id)
330
            revno = self.b1.revision_history().index(rev_id) + 1
331
            to_branch.update_revisions(self.b1, stop_revision=revno)
332
            merge((dirname, -1), (dirname, 0), this_dir=dirname,
333
                    check_clean=False, ignore_zero=True)
334
        return to_branch
335
336
    def valid_apply_changeset(self, base_rev_id, cset):
337
        """Get the base revision, apply the changes, and make
338
        sure everything matches the builtin branch.
339
        """
340
        from apply_changeset import _apply_cset
341
342
        to_branch = self.get_checkout(base_rev_id)
343
        _apply_cset(to_branch, cset)
344
345
        info = cset[0]
346
        for rev in info.real_revisions:
347
            self.assert_(rev.revision_id in to_branch.revision_store,
348
                'Missing revision {%s} after applying changeset' 
349
                % rev.revision_id)
350
351
        rev = info.real_revisions[-1]
352
        base_tree = self.b1.revision_tree(rev.revision_id)
353
        to_tree = to_branch.revision_tree(rev.revision_id)
354
        
355
        # TODO: make sure the target tree is identical to base tree
356
357
    def runTest(self):
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
358
        from bzrlib.branch import find_branch
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
359
        import common
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
360
361
        import os, sys
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
362
        pjoin = os.path.join
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
363
364
        os.mkdir('b1')
365
        self.b1 = find_branch('b1', init=True)
366
367
        open(pjoin('b1/one'), 'wb').write('one\n')
368
        self.b1.add('one')
369
        self.b1.commit('add one', rev_id='a@cset-0-1')
370
371
        cset = self.get_valid_cset(None, 'a@cset-0-1')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
372
373
        # Make sure we can handle files with spaces, tabs, other
374
        # bogus characters
375
        self.build_tree([
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
376
                'b1/with space.txt'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
377
                , 'b1/dir/'
378
                , 'b1/dir/filein subdir.c'
379
                , 'b1/dir/WithCaps.txt'
380
                , 'b1/sub/'
381
                , 'b1/sub/sub/'
382
                , 'b1/sub/sub/nonempty.txt'
383
                # Tabs are not valid in filenames on windows
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
384
                #'b1/with\ttab.txt'
385
                ])
386
        open('b1/sub/sub/emptyfile.txt', 'wb').close()
0.5.84 by John Arbash Meinel
(broken) problem with removes.
387
        self.b1.add([
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
388
                'with space.txt'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
389
                , 'dir'
390
                , 'dir/filein subdir.c'
391
                , 'dir/WithCaps.txt'
392
                , 'sub'
393
                , 'sub/sub'
394
                , 'sub/sub/nonempty.txt'
395
                , 'sub/sub/emptyfile.txt'
396
                ])
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
397
        self.b1.commit('add whitespace', rev_id='a@cset-0-2')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
398
399
        cset = self.get_valid_cset('a@cset-0-1', 'a@cset-0-2')
400
        # Check a rollup changeset
401
        cset = self.get_valid_cset(None, 'a@cset-0-2')
402
0.5.84 by John Arbash Meinel
(broken) problem with removes.
403
        # Now delete entries
404
        self.b1.remove(['sub/sub/nonempty.txt'
405
                , 'sub/sub/emptyfile.txt'
406
                , 'sub/sub'])
407
        self.b1.commit('removed', rev_id='a@cset-0-3')
408
        
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
409
        cset = self.get_valid_cset('a@cset-0-2', 'a@cset-0-3')
0.5.84 by John Arbash Meinel
(broken) problem with removes.
410
        # Check a rollup changeset
411
        cset = self.get_valid_cset(None, 'a@cset-0-3')
412
413
414
        # Now move the directory
415
        self.b1.rename_one('dir', 'sub/dir')
416
        self.b1.commit('rename dir', rev_id='a@cset-0-4')
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
417
0.5.84 by John Arbash Meinel
(broken) problem with removes.
418
        cset = self.get_valid_cset('a@cset-0-3', 'a@cset-0-4')
419
        # Check a rollup changeset
420
        cset = self.get_valid_cset(None, 'a@cset-0-4')
421
422
TEST_CLASSES = [
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
423
    CTreeTester,
424
    CSetTester
425
]
426
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
427