~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

[merge] update from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import os
19
19
 
20
20
from bzrlib.branch import Branch
21
 
from bzrlib.clone import copy_branch
22
21
import bzrlib.errors as errors
23
22
from bzrlib.diff import internal_diff
24
23
from bzrlib.inventory import Inventory, ROOT_ID
25
24
import bzrlib.inventory as inventory
26
25
from bzrlib.osutils import has_symlinks, rename, pathjoin
27
 
from bzrlib.tests import TestCase, TestCaseInTempDir
 
26
from bzrlib.tests import TestCase, TestCaseWithTransport
 
27
from bzrlib.workingtree import WorkingTree
28
28
 
29
29
 
30
30
class TestInventory(TestCase):
133
133
        self.failIf(link.has_text())
134
134
 
135
135
 
136
 
class TestEntryDiffing(TestCaseInTempDir):
 
136
class TestEntryDiffing(TestCaseWithTransport):
137
137
 
138
138
    def setUp(self):
139
139
        super(TestEntryDiffing, self).setUp()
140
 
        self.branch = Branch.initialize(u'.')
141
 
        self.wt = self.branch.working_tree()
 
140
        self.wt = self.make_branch_and_tree('.')
 
141
        self.branch = self.wt.branch
142
142
        print >> open('file', 'wb'), 'foo'
143
 
        self.branch.working_tree().add(['file'], ['fileid'])
 
143
        self.wt.add(['file'], ['fileid'])
144
144
        if has_symlinks():
145
145
            os.symlink('target1', 'symlink')
146
 
            self.branch.working_tree().add(['symlink'], ['linkid'])
 
146
            self.wt.add(['symlink'], ['linkid'])
147
147
        self.wt.commit('message_1', rev_id = '1')
148
148
        print >> open('file', 'wb'), 'bar'
149
149
        if has_symlinks():
150
150
            os.unlink('symlink')
151
151
            os.symlink('target2', 'symlink')
152
 
        self.tree_1 = self.branch.revision_tree('1')
153
 
        self.inv_1 = self.branch.get_inventory('1')
 
152
        self.tree_1 = self.branch.repository.revision_tree('1')
 
153
        self.inv_1 = self.branch.repository.get_inventory('1')
154
154
        self.file_1 = self.inv_1['fileid']
155
155
        self.tree_2 = self.branch.working_tree()
156
156
        self.inv_2 = self.tree_2.read_working_inventory()
230
230
                         "=== target changed 'target1' => 'target2'\n")
231
231
 
232
232
 
233
 
class TestSnapshot(TestCaseInTempDir):
 
233
class TestSnapshot(TestCaseWithTransport):
234
234
 
235
235
    def setUp(self):
236
236
        # for full testing we'll need a branch
240
240
        # to change, and then test merge patterns
241
241
        # with fake parent entries.
242
242
        super(TestSnapshot, self).setUp()
243
 
        self.branch = Branch.initialize(u'.')
 
243
        self.wt = self.make_branch_and_tree('.')
 
244
        self.branch = self.wt.branch
244
245
        self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
245
 
        self.branch.working_tree().add(['subdir', 'subdir/file'],
 
246
        self.wt.add(['subdir', 'subdir/file'],
246
247
                                       ['dirid', 'fileid'])
247
248
        if has_symlinks():
248
249
            pass
249
 
        self.wt = self.branch.working_tree()
250
250
        self.wt.commit('message_1', rev_id = '1')
251
 
        self.tree_1 = self.branch.revision_tree('1')
252
 
        self.inv_1 = self.branch.get_inventory('1')
 
251
        self.tree_1 = self.branch.repository.revision_tree('1')
 
252
        self.inv_1 = self.branch.repository.get_inventory('1')
253
253
        self.file_1 = self.inv_1['fileid']
254
 
        self.work_tree = self.branch.working_tree()
255
 
        self.file_active = self.work_tree.inventory['fileid']
 
254
        self.file_active = self.wt.inventory['fileid']
256
255
 
257
256
    def test_snapshot_new_revision(self):
258
257
        # This tests that a simple commit with no parents makes a new
259
258
        # revision value in the inventory entry
260
 
        self.file_active.snapshot('2', 'subdir/file', {}, self.work_tree, 
261
 
                                  self.branch.weave_store,
 
259
        self.file_active.snapshot('2', 'subdir/file', {}, self.wt, 
 
260
                                  self.branch.repository.weave_store,
262
261
                                  self.branch.get_transaction())
263
262
        # expected outcome - file_1 has a revision id of '2', and we can get
264
263
        # its text of 'file contents' out of the weave.
265
264
        self.assertEqual(self.file_1.revision, '1')
266
265
        self.assertEqual(self.file_active.revision, '2')
267
266
        # this should be a separate test probably, but lets check it once..
268
 
        lines = self.branch.weave_store.get_lines('fileid','2',
 
267
        lines = self.branch.repository.weave_store.get_lines('fileid','2',
269
268
            self.branch.get_transaction())
270
269
        self.assertEqual(lines, ['contents of subdir/file\n'])
271
270
 
273
272
        #This tests that a simple commit does not make a new entry for
274
273
        # an unchanged inventory entry
275
274
        self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
276
 
                                  self.work_tree, self.branch.weave_store,
 
275
                                  self.wt, 
 
276
                                  self.branch.repository.weave_store,
277
277
                                  self.branch.get_transaction())
278
278
        self.assertEqual(self.file_1.revision, '1')
279
279
        self.assertEqual(self.file_active.revision, '1')
280
280
        self.assertRaises(errors.WeaveError,
281
 
                          self.branch.weave_store.get_lines, 'fileid', '2',
282
 
                          self.branch.get_transaction())
 
281
                          self.branch.repository.weave_store.get_lines, 
 
282
                          'fileid', '2', self.branch.get_transaction())
283
283
 
284
284
    def test_snapshot_merge_identical_different_revid(self):
285
285
        # This tests that a commit with two identical parents, one of which has
293
293
        self.assertEqual(self.file_1, other_ie)
294
294
        other_ie.revision = 'other'
295
295
        self.assertNotEqual(self.file_1, other_ie)
296
 
        self.branch.weave_store.add_identical_text('fileid', '1', 'other', ['1'],
297
 
            self.branch.get_transaction())
 
296
        self.branch.repository.weave_store.add_identical_text('fileid', '1', 
 
297
            'other', ['1'], self.branch.get_transaction())
298
298
        self.file_active.snapshot('2', 'subdir/file', 
299
299
                                  {'1':self.file_1, 'other':other_ie},
300
 
                                  self.work_tree, self.branch.weave_store,
 
300
                                  self.wt, 
 
301
                                  self.branch.repository.weave_store,
301
302
                                  self.branch.get_transaction())
302
303
        self.assertEqual(self.file_active.revision, '2')
303
304
 
307
308
        self.file_active.name='newname'
308
309
        rename('subdir/file', 'subdir/newname')
309
310
        self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1}, 
310
 
                                  self.work_tree, 
311
 
                                  self.branch.weave_store,
 
311
                                  self.wt,
 
312
                                  self.branch.repository.weave_store,
312
313
                                  self.branch.get_transaction())
313
314
        # expected outcome - file_1 has a revision id of '2'
314
315
        self.assertEqual(self.file_active.revision, '2')
315
316
 
316
317
 
317
 
class TestPreviousHeads(TestCaseInTempDir):
 
318
class TestPreviousHeads(TestCaseWithTransport):
318
319
 
319
320
    def setUp(self):
320
321
        # we want several inventories, that respectively
326
327
        # D) fileid present in two inventories and one is
327
328
        #   a descendent of the other. (B, D)
328
329
        super(TestPreviousHeads, self).setUp()
 
330
        self.wt = self.make_branch_and_tree('.')
 
331
        self.branch = self.wt.branch
329
332
        self.build_tree(['file'])
330
 
        self.branch = Branch.initialize(u'.')
331
 
        self.wt = self.branch.working_tree()
332
333
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
333
 
        self.inv_A = self.branch.get_inventory('A')
334
 
        self.branch.working_tree().add(['file'], ['fileid'])
 
334
        self.inv_A = self.branch.repository.get_inventory('A')
 
335
        self.wt.add(['file'], ['fileid'])
335
336
        self.wt.commit('add file', rev_id='B')
336
 
        self.inv_B = self.branch.get_inventory('B')
337
 
        self.branch.put_controlfile('revision-history', 'A\n')
 
337
        self.inv_B = self.branch.repository.get_inventory('B')
 
338
        self.branch.lock_write()
 
339
        try:
 
340
            self.branch.control_files.put_utf8('revision-history', 'A\n')
 
341
        finally:
 
342
            self.branch.unlock()
338
343
        self.assertEqual(self.branch.revision_history(), ['A'])
339
344
        self.wt.commit('another add of file', rev_id='C')
340
 
        self.inv_C = self.branch.get_inventory('C')
 
345
        self.inv_C = self.branch.repository.get_inventory('C')
341
346
        self.wt.add_pending_merge('B')
342
347
        self.wt.commit('merge in B', rev_id='D')
343
 
        self.inv_D = self.branch.get_inventory('D')
344
 
        self.file_active = self.wt.inventory['fileid']
345
 
        self.weave = self.branch.weave_store.get_weave('fileid',
 
348
        self.inv_D = self.branch.repository.get_inventory('D')
 
349
        self.file_active = self.branch.working_tree().inventory['fileid']
 
350
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
346
351
            self.branch.get_transaction())
347
352
        
348
353
    def get_previous_heads(self, inventories):
376
381
    # TODO: test two inventories with the same file revision 
377
382
 
378
383
 
379
 
class TestExecutable(TestCaseInTempDir):
 
384
class TestExecutable(TestCaseWithTransport):
380
385
 
381
386
    def test_stays_executable(self):
382
387
        basic_inv = """<inventory format="5">
384
389
<file file_id="b-20051208024829-849e76f7968d7a86" name="b" />
385
390
</inventory>
386
391
"""
387
 
        os.mkdir('b1')
388
 
        b = Branch.initialize('b1')
 
392
        wt = self.make_branch_and_tree('b1')
 
393
        b = wt.branch
389
394
        open('b1/a', 'wb').write('a test\n')
390
395
        open('b1/b', 'wb').write('b test\n')
391
396
        os.chmod('b1/a', 0755)
396
401
 
397
402
        a_id = "a-20051208024829-849e76f7968d7a86"
398
403
        b_id = "b-20051208024829-849e76f7968d7a86"
399
 
        t = b.working_tree()
 
404
        t = WorkingTree('b1', b)
400
405
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
401
406
 
402
407
        self.failUnless(t.is_executable(a_id), "'a' lost the execute bit")
404
409
 
405
410
        t.commit('adding a,b', rev_id='r1')
406
411
 
407
 
        rev_tree = b.revision_tree('r1')
 
412
        rev_tree = b.repository.revision_tree('r1')
408
413
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
409
414
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
410
415
 
422
427
        # Make sure that revert is able to bring them back,
423
428
        # and sets 'a' back to being executable
424
429
 
425
 
        t.revert(['b1/a', 'b1/b'], rev_tree, backups=False)
 
430
        t.revert(['a', 'b'], rev_tree, backups=False)
426
431
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
427
432
 
428
433
        self.failUnless(t.is_executable(a_id), "'a' lost the execute bit")
448
453
        # This seems to happen any time you do a merge operation on the
449
454
        # working tree
450
455
        del t
451
 
        t = b.working_tree()
 
456
        t = WorkingTree('b1', b)
452
457
 
453
458
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
454
459
 
458
463
        # Now make sure that 'bzr branch' also preserves the
459
464
        # executable bit
460
465
        # TODO: Maybe this should be a blackbox test
461
 
        from bzrlib.clone import copy_branch
462
 
        copy_branch(b, 'b2', revision='r1')
 
466
        b.clone('b2', revision='r1')
463
467
        b2 = Branch.open('b2')
464
468
        self.assertEquals('r1', b2.last_revision())
465
469
        t2 = b2.working_tree()