/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_add.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-09 19:53:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1927.
  • Revision ID: john@arbash-meinel.com-20060809195302-38b349ff2e1473b4
Adding the AddFromBaseAction, which tries to reuse file ids from another tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import unittest
20
20
 
21
21
from bzrlib import errors, ignores, osutils
22
 
from bzrlib.add import smart_add, smart_add_tree, AddAction
 
22
from bzrlib.add import (
 
23
    AddAction,
 
24
    AddFromBaseAction,
 
25
    smart_add,
 
26
    smart_add_tree,
 
27
    )
23
28
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
24
29
from bzrlib.errors import NoSuchFile
25
30
from bzrlib.inventory import InventoryFile, Inventory
145
150
        self.assertSubset(('inertiatic/foo.pyc',), ignored['*.py[co]'])
146
151
 
147
152
 
148
 
class CustomIDAddAction(AddAction):
 
153
class AddCustomIDAction(AddAction):
149
154
 
150
155
    def __call__(self, inv, parent_ie, path, kind):
151
156
        # The first part just logs if appropriate
242
247
 
243
248
    def test_custom_ids(self):
244
249
        sio = StringIO()
245
 
        action = CustomIDAddAction(to_file=sio, should_print=True)
 
250
        action = AddCustomIDAction(to_file=sio, should_print=True)
246
251
        self.build_tree(['file1', 'dir1/', 'dir1/file2'])
247
252
 
248
253
        wt = self.make_branch_and_tree('.')
259
264
                                in wt.inventory.iter_entries()])
260
265
 
261
266
 
 
267
class TestAddFrom(TestCaseWithTransport):
 
268
    """Tests for AddFromBaseAction"""
 
269
 
 
270
    def make_base_tree(self):
 
271
        self.base_tree = self.make_branch_and_tree('base')
 
272
        self.build_tree(['base/a', 'base/b',
 
273
                         'base/dir/', 'base/dir/a',
 
274
                         'base/dir/subdir/',
 
275
                         'base/dir/subdir/b',
 
276
                        ])
 
277
        self.base_tree.add(['a', 'b', 'dir', 'dir/a',
 
278
                            'dir/subdir', 'dir/subdir/b'])
 
279
        self.base_tree.commit('creating initial tree.')
 
280
 
 
281
    def add_helper(self, base_tree, base_path, new_tree, file_list,
 
282
                   should_print=False):
 
283
        to_file = StringIO()
 
284
        base_tree.lock_read()
 
285
        try:
 
286
            new_tree.lock_write()
 
287
            try:
 
288
                action = AddFromBaseAction(base_tree, base_path,
 
289
                                           to_file=to_file,
 
290
                                           should_print=should_print)
 
291
                smart_add_tree(new_tree, file_list, action=action)
 
292
            finally:
 
293
                new_tree.unlock()
 
294
        finally:
 
295
            base_tree.unlock()
 
296
        return to_file.getvalue()
 
297
 
 
298
    def test_copy_all(self):
 
299
        self.make_base_tree()
 
300
        new_tree = self.make_branch_and_tree('new')
 
301
        files = ['a', 'b',
 
302
                 'dir/', 'dir/a',
 
303
                 'dir/subdir/',
 
304
                 'dir/subdir/b',
 
305
                ]
 
306
        self.build_tree(['new/' + fn for fn in files])
 
307
        self.add_helper(self.base_tree, '', new_tree, ['new'])
 
308
 
 
309
        for fn in files:
 
310
            base_file_id = self.base_tree.path2id(fn)
 
311
            new_file_id = new_tree.path2id(fn)
 
312
            self.assertEqual(base_file_id, new_file_id)
 
313
 
 
314
    def test_copy_from_dir(self):
 
315
        self.make_base_tree()
 
316
        new_tree = self.make_branch_and_tree('new')
 
317
 
 
318
        self.build_tree(['new/a', 'new/b', 'new/c',
 
319
                         'new/subdir/', 'new/subdir/b', 'new/subdir/d'])
 
320
        self.add_helper(self.base_tree, 'dir', new_tree, ['new'])
 
321
 
 
322
        # We 'a' and 'b' exist in the root, and they are being added
 
323
        # in a new 'root'. Since ROOT ids are not unique, we will
 
324
        # use those ids
 
325
        # TODO: This will probably change once trees have a unique root id
 
326
        # It is definitely arguable that 'a' should get the id of
 
327
        # 'dir/a' not of 'a'.
 
328
        self.assertEqual(self.base_tree.path2id('a'),
 
329
                         new_tree.path2id('a'))
 
330
        self.assertEqual(self.base_tree.path2id('b'),
 
331
                         new_tree.path2id('b'))
 
332
 
 
333
        # Because we specified 'dir/' as the base path, and we have
 
334
        # nothing named 'subdir' in the base tree, we should grab the
 
335
        # ids from there
 
336
        self.assertEqual(self.base_tree.path2id('dir/subdir'),
 
337
                         new_tree.path2id('subdir'))
 
338
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
 
339
                         new_tree.path2id('subdir/b'))
 
340
 
 
341
        # These should get newly generated ids
 
342
        c_id = new_tree.path2id('c')
 
343
        self.assertNotEqual(None, c_id)
 
344
        self.failIf(c_id in self.base_tree)
 
345
 
 
346
        d_id = new_tree.path2id('subdir/d')
 
347
        self.assertNotEqual(None, d_id)
 
348
        self.failIf(d_id in self.base_tree)
 
349
 
 
350
    def test_copy_existing_dir(self):
 
351
        self.make_base_tree()
 
352
        new_tree = self.make_branch_and_tree('new')
 
353
        self.build_tree(['new/subby/', 'new/subby/a', 'new/subby/b'])
 
354
 
 
355
        subdir_file_id = self.base_tree.path2id('dir/subdir')
 
356
        new_tree.add(['subby'], [subdir_file_id])
 
357
        self.add_helper(self.base_tree, '', new_tree, ['new'])
 
358
        # Because 'subby' already points to subdir, we should add
 
359
        # 'b' with the same id
 
360
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
 
361
                         new_tree.path2id('subby/b'))
 
362
 
 
363
        # 'subby/a' should be added with a new id because there is no
 
364
        # matching path or child of 'subby'.
 
365
        a_id = new_tree.path2id('subby/a')
 
366
        self.assertNotEqual(None, a_id)
 
367
        self.failIf(a_id in self.base_tree)
 
368
 
 
369
 
262
370
class TestAddNonNormalized(TestCaseWithTransport):
263
371
 
264
372
    def make(self):