~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

Lots of updates for 'bzr check'
Added get_sha1() to Weave, 
Updated RevisionTree so that it takes a Branch 
(thus it can use get_transaction()),
changed 'bzr check' so that it uses Weave.check(),
rather than having each file extract its own text

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.diff import internal_diff
24
24
from bzrlib.inventory import Inventory, ROOT_ID
25
25
import bzrlib.inventory as inventory
26
 
from bzrlib.osutils import has_symlinks, rename
27
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
26
from bzrlib.osutils import has_symlinks, rename, pathjoin
 
27
from bzrlib.tests import TestCase, TestCaseInTempDir
28
28
 
29
29
 
30
30
class TestInventory(TestCase):
32
32
    def test_is_within(self):
33
33
        from bzrlib.osutils import is_inside_any
34
34
 
35
 
        SRC_FOO_C = os.path.join('src', 'foo.c')
 
35
        SRC_FOO_C = pathjoin('src', 'foo.c')
36
36
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
37
37
                         (['src'], SRC_FOO_C),
38
38
                         (['src'], 'src'),
137
137
 
138
138
    def setUp(self):
139
139
        super(TestEntryDiffing, self).setUp()
140
 
        self.branch = Branch.initialize('.')
 
140
        self.branch = Branch.initialize(u'.')
 
141
        self.wt = self.branch.working_tree()
141
142
        print >> open('file', 'wb'), 'foo'
142
 
        self.branch.add(['file'], ['fileid'])
 
143
        self.branch.working_tree().add(['file'], ['fileid'])
143
144
        if has_symlinks():
144
145
            os.symlink('target1', 'symlink')
145
 
            self.branch.add(['symlink'], ['linkid'])
146
 
        self.branch.commit('message_1', rev_id = '1')
 
146
            self.branch.working_tree().add(['symlink'], ['linkid'])
 
147
        self.wt.commit('message_1', rev_id = '1')
147
148
        print >> open('file', 'wb'), 'bar'
148
149
        if has_symlinks():
149
150
            os.unlink('symlink')
164
165
                          "old_label", self.tree_1,
165
166
                          "/dev/null", None, None,
166
167
                          output)
167
 
        self.assertEqual(output.getvalue(), "--- old_label\n"
168
 
                                            "+++ /dev/null\n"
 
168
        self.assertEqual(output.getvalue(), "--- old_label\t\n"
 
169
                                            "+++ /dev/null\t\n"
169
170
                                            "@@ -1,1 +0,0 @@\n"
170
171
                                            "-foo\n"
171
172
                                            "\n")
176
177
                          "new_label", self.tree_1,
177
178
                          "/dev/null", None, None,
178
179
                          output, reverse=True)
179
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
180
 
                                            "+++ new_label\n"
 
180
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
181
                                            "+++ new_label\t\n"
181
182
                                            "@@ -0,0 +1,1 @@\n"
182
183
                                            "+foo\n"
183
184
                                            "\n")
188
189
                          "/dev/null", self.tree_1, 
189
190
                          "new_label", self.file_2, self.tree_2,
190
191
                          output)
191
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
192
 
                                            "+++ new_label\n"
 
192
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
193
                                            "+++ new_label\t\n"
193
194
                                            "@@ -1,1 +1,1 @@\n"
194
195
                                            "-foo\n"
195
196
                                            "+bar\n"
239
240
        # to change, and then test merge patterns
240
241
        # with fake parent entries.
241
242
        super(TestSnapshot, self).setUp()
242
 
        self.branch = Branch.initialize('.')
243
 
        self.build_tree(['subdir/', 'subdir/file'])
244
 
        self.branch.add(['subdir', 'subdir/file'], ['dirid', 'fileid'])
 
243
        self.branch = Branch.initialize(u'.')
 
244
        self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
 
245
        self.branch.working_tree().add(['subdir', 'subdir/file'],
 
246
                                       ['dirid', 'fileid'])
245
247
        if has_symlinks():
246
248
            pass
247
 
        self.branch.commit('message_1', rev_id = '1')
 
249
        self.wt = self.branch.working_tree()
 
250
        self.wt.commit('message_1', rev_id = '1')
248
251
        self.tree_1 = self.branch.revision_tree('1')
249
252
        self.inv_1 = self.branch.get_inventory('1')
250
253
        self.file_1 = self.inv_1['fileid']
324
327
        #   a descendent of the other. (B, D)
325
328
        super(TestPreviousHeads, self).setUp()
326
329
        self.build_tree(['file'])
327
 
        self.branch = Branch.initialize('.')
328
 
        self.branch.commit('new branch', allow_pointless=True, rev_id='A')
 
330
        self.branch = Branch.initialize(u'.')
 
331
        self.wt = self.branch.working_tree()
 
332
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
329
333
        self.inv_A = self.branch.get_inventory('A')
330
 
        self.branch.add(['file'], ['fileid'])
331
 
        self.branch.commit('add file', rev_id='B')
 
334
        self.branch.working_tree().add(['file'], ['fileid'])
 
335
        self.wt.commit('add file', rev_id='B')
332
336
        self.inv_B = self.branch.get_inventory('B')
333
337
        self.branch.put_controlfile('revision-history', 'A\n')
334
338
        self.assertEqual(self.branch.revision_history(), ['A'])
335
 
        self.branch.commit('another add of file', rev_id='C')
 
339
        self.wt.commit('another add of file', rev_id='C')
336
340
        self.inv_C = self.branch.get_inventory('C')
337
 
        self.branch.add_pending_merge('B')
338
 
        self.branch.commit('merge in B', rev_id='D')
 
341
        self.wt.add_pending_merge('B')
 
342
        self.wt.commit('merge in B', rev_id='D')
339
343
        self.inv_D = self.branch.get_inventory('D')
340
 
        self.file_active = self.branch.working_tree().inventory['fileid']
 
344
        self.file_active = self.wt.inventory['fileid']
341
345
        self.weave = self.branch.weave_store.get_weave('fileid',
342
346
            self.branch.get_transaction())
343
347
        
370
374
                         self.get_previous_heads([self.inv_D, self.inv_B]))
371
375
 
372
376
    # TODO: test two inventories with the same file revision 
 
377
 
 
378
 
 
379
class TestExecutable(TestCaseInTempDir):
 
380
 
 
381
    def test_stays_executable(self):
 
382
        basic_inv = """<inventory format="5">
 
383
<file file_id="a-20051208024829-849e76f7968d7a86" name="a" executable="yes" />
 
384
<file file_id="b-20051208024829-849e76f7968d7a86" name="b" />
 
385
</inventory>
 
386
"""
 
387
        os.mkdir('b1')
 
388
        b = Branch.initialize('b1')
 
389
        open('b1/a', 'wb').write('a test\n')
 
390
        open('b1/b', 'wb').write('b test\n')
 
391
        os.chmod('b1/a', 0755)
 
392
        os.chmod('b1/b', 0644)
 
393
        # Manually writing the inventory, to ensure that
 
394
        # the executable="yes" entry is set for 'a' and not for 'b'
 
395
        open('b1/.bzr/inventory', 'wb').write(basic_inv)
 
396
 
 
397
        a_id = "a-20051208024829-849e76f7968d7a86"
 
398
        b_id = "b-20051208024829-849e76f7968d7a86"
 
399
        t = b.working_tree()
 
400
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
 
401
 
 
402
        self.failUnless(t.is_executable(a_id), "'a' lost the execute bit")
 
403
        self.failIf(t.is_executable(b_id), "'b' gained an execute bit")
 
404
 
 
405
        t.commit('adding a,b', rev_id='r1')
 
406
 
 
407
        rev_tree = b.revision_tree('r1')
 
408
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
 
409
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
 
410
 
 
411
        self.failUnless(rev_tree.inventory[a_id].executable)
 
412
        self.failIf(rev_tree.inventory[b_id].executable)
 
413
 
 
414
        # Make sure the entries are gone
 
415
        os.remove('b1/a')
 
416
        os.remove('b1/b')
 
417
        self.failIf(t.has_id(a_id))
 
418
        self.failIf(t.has_filename('a'))
 
419
        self.failIf(t.has_id(b_id))
 
420
        self.failIf(t.has_filename('b'))
 
421
 
 
422
        # Make sure that revert is able to bring them back,
 
423
        # and sets 'a' back to being executable
 
424
 
 
425
        t.revert(['b1/a', 'b1/b'], rev_tree, backups=False)
 
426
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
 
427
 
 
428
        self.failUnless(t.is_executable(a_id), "'a' lost the execute bit")
 
429
        self.failIf(t.is_executable(b_id), "'b' gained an execute bit")
 
430
 
 
431
        # Now remove them again, and make sure that after a
 
432
        # commit, they are still marked correctly
 
433
        os.remove('b1/a')
 
434
        os.remove('b1/b')
 
435
        t.commit('removed', rev_id='r2')
 
436
 
 
437
        self.assertEqual([], [cn for cn,ie in t.inventory.iter_entries()])
 
438
        self.failIf(t.has_id(a_id))
 
439
        self.failIf(t.has_filename('a'))
 
440
        self.failIf(t.has_id(b_id))
 
441
        self.failIf(t.has_filename('b'))
 
442
 
 
443
        # Now revert back to the previous commit
 
444
        t.revert([], rev_tree, backups=False)
 
445
        # TODO: FIXME: For some reason, after revert, the tree does not 
 
446
        # regenerate its working inventory, so we have to manually delete
 
447
        # the working tree, and create a new one
 
448
        # This seems to happen any time you do a merge operation on the
 
449
        # working tree
 
450
        del t
 
451
        t = b.working_tree()
 
452
 
 
453
        self.assertEqual(['a', 'b'], [cn for cn,ie in t.inventory.iter_entries()])
 
454
 
 
455
        self.failUnless(t.is_executable(a_id), "'a' lost the execute bit")
 
456
        self.failIf(t.is_executable(b_id), "'b' gained an execute bit")
 
457
 
 
458
        # Now make sure that 'bzr branch' also preserves the
 
459
        # executable bit
 
460
        # TODO: Maybe this should be a blackbox test
 
461
        from bzrlib.clone import copy_branch
 
462
        copy_branch(b, 'b2', revision='r1')
 
463
        b2 = Branch.open('b2')
 
464
        self.assertEquals('r1', b2.last_revision())
 
465
        t2 = b2.working_tree()
 
466
 
 
467
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
468
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
469
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
470
 
 
471
        # Make sure pull will delete the files
 
472
        t2.pull(b)
 
473
        self.assertEquals('r2', b2.last_revision())
 
474
        # FIXME: Same thing here, t2 needs to be recreated
 
475
        del t2
 
476
        t2 = b2.working_tree()
 
477
        self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
 
478
 
 
479
        # Now commit the changes on the first branch
 
480
        # so that the second branch can pull the changes
 
481
        # and make sure that the executable bit has been copied
 
482
        t.commit('resurrected', rev_id='r3')
 
483
 
 
484
        t2.pull(b)
 
485
        # FIXME: And here
 
486
        del t2
 
487
        t2 = b2.working_tree()
 
488
        self.assertEquals('r3', b2.last_revision())
 
489
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
490
 
 
491
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
492
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")