~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

Late bind to PatienceSequenceMatcher to allow plugin to override.

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
 
from bzrlib.inventory import Inventory, ROOT_ID
 
23
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
 
24
    InventoryDirectory, InventoryEntry)
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, TestCaseWithTransport
 
28
from bzrlib.transform import TreeTransform
 
29
from bzrlib.uncommit import uncommit
28
30
 
29
31
 
30
32
class TestInventory(TestCase):
32
34
    def test_is_within(self):
33
35
        from bzrlib.osutils import is_inside_any
34
36
 
35
 
        SRC_FOO_C = os.path.join('src', 'foo.c')
 
37
        SRC_FOO_C = pathjoin('src', 'foo.c')
36
38
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
37
39
                         (['src'], SRC_FOO_C),
38
40
                         (['src'], 'src'),
59
61
        
60
62
        self.assert_('src-id' in inv)
61
63
 
62
 
 
63
64
    def test_version(self):
64
65
        """Inventory remembers the text's version."""
65
66
        inv = Inventory()
132
133
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
133
134
        self.failIf(link.has_text())
134
135
 
 
136
    def test_make_entry(self):
 
137
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
 
138
            inventory.InventoryFile)
 
139
        self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
 
140
            inventory.InventoryLink)
 
141
        self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
 
142
            inventory.InventoryDirectory)
135
143
 
136
 
class TestEntryDiffing(TestCaseInTempDir):
 
144
class TestEntryDiffing(TestCaseWithTransport):
137
145
 
138
146
    def setUp(self):
139
147
        super(TestEntryDiffing, self).setUp()
140
 
        self.branch = Branch.initialize('.')
 
148
        self.wt = self.make_branch_and_tree('.')
 
149
        self.branch = self.wt.branch
141
150
        print >> open('file', 'wb'), 'foo'
142
 
        self.branch.add(['file'], ['fileid'])
 
151
        print >> open('binfile', 'wb'), 'foo'
 
152
        self.wt.add(['file'], ['fileid'])
 
153
        self.wt.add(['binfile'], ['binfileid'])
143
154
        if has_symlinks():
144
155
            os.symlink('target1', 'symlink')
145
 
            self.branch.add(['symlink'], ['linkid'])
146
 
        self.branch.commit('message_1', rev_id = '1')
 
156
            self.wt.add(['symlink'], ['linkid'])
 
157
        self.wt.commit('message_1', rev_id = '1')
147
158
        print >> open('file', 'wb'), 'bar'
 
159
        print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
148
160
        if has_symlinks():
149
161
            os.unlink('symlink')
150
162
            os.symlink('target2', 'symlink')
151
 
        self.tree_1 = self.branch.revision_tree('1')
152
 
        self.inv_1 = self.branch.get_inventory('1')
 
163
        self.tree_1 = self.branch.repository.revision_tree('1')
 
164
        self.inv_1 = self.branch.repository.get_inventory('1')
153
165
        self.file_1 = self.inv_1['fileid']
154
 
        self.tree_2 = self.branch.working_tree()
155
 
        self.inv_2 = self.branch.inventory
 
166
        self.file_1b = self.inv_1['binfileid']
 
167
        self.tree_2 = self.wt
 
168
        self.inv_2 = self.tree_2.read_working_inventory()
156
169
        self.file_2 = self.inv_2['fileid']
 
170
        self.file_2b = self.inv_2['binfileid']
157
171
        if has_symlinks():
158
172
            self.link_1 = self.inv_1['linkid']
159
173
            self.link_2 = self.inv_2['linkid']
164
178
                          "old_label", self.tree_1,
165
179
                          "/dev/null", None, None,
166
180
                          output)
167
 
        self.assertEqual(output.getvalue(), "--- old_label\n"
168
 
                                            "+++ /dev/null\n"
 
181
        self.assertEqual(output.getvalue(), "--- old_label\t\n"
 
182
                                            "+++ /dev/null\t\n"
169
183
                                            "@@ -1,1 +0,0 @@\n"
170
184
                                            "-foo\n"
171
185
                                            "\n")
176
190
                          "new_label", self.tree_1,
177
191
                          "/dev/null", None, None,
178
192
                          output, reverse=True)
179
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
180
 
                                            "+++ new_label\n"
 
193
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
194
                                            "+++ new_label\t\n"
181
195
                                            "@@ -0,0 +1,1 @@\n"
182
196
                                            "+foo\n"
183
197
                                            "\n")
188
202
                          "/dev/null", self.tree_1, 
189
203
                          "new_label", self.file_2, self.tree_2,
190
204
                          output)
191
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
192
 
                                            "+++ new_label\n"
 
205
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
206
                                            "+++ new_label\t\n"
193
207
                                            "@@ -1,1 +1,1 @@\n"
194
208
                                            "-foo\n"
195
209
                                            "+bar\n"
196
210
                                            "\n")
197
211
        
 
212
    def test_file_diff_binary(self):
 
213
        output = StringIO()
 
214
        self.file_1.diff(internal_diff, 
 
215
                          "/dev/null", self.tree_1, 
 
216
                          "new_label", self.file_2b, self.tree_2,
 
217
                          output)
 
218
        self.assertEqual(output.getvalue(), 
 
219
                         "Binary files /dev/null and new_label differ\n")
198
220
    def test_link_diff_deleted(self):
199
221
        if not has_symlinks():
200
222
            return
229
251
                         "=== target changed 'target1' => 'target2'\n")
230
252
 
231
253
 
232
 
class TestSnapshot(TestCaseInTempDir):
 
254
class TestSnapshot(TestCaseWithTransport):
233
255
 
234
256
    def setUp(self):
235
257
        # for full testing we'll need a branch
239
261
        # to change, and then test merge patterns
240
262
        # with fake parent entries.
241
263
        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'])
 
264
        self.wt = self.make_branch_and_tree('.')
 
265
        self.branch = self.wt.branch
 
266
        self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
 
267
        self.wt.add(['subdir', 'subdir/file'],
 
268
                                       ['dirid', 'fileid'])
245
269
        if has_symlinks():
246
270
            pass
247
 
        self.branch.commit('message_1', rev_id = '1')
248
 
        self.tree_1 = self.branch.revision_tree('1')
249
 
        self.inv_1 = self.branch.get_inventory('1')
 
271
        self.wt.commit('message_1', rev_id = '1')
 
272
        self.tree_1 = self.branch.repository.revision_tree('1')
 
273
        self.inv_1 = self.branch.repository.get_inventory('1')
250
274
        self.file_1 = self.inv_1['fileid']
251
 
        self.work_tree = self.branch.working_tree()
252
 
        self.file_active = self.work_tree.inventory['fileid']
 
275
        self.file_active = self.wt.inventory['fileid']
253
276
 
254
277
    def test_snapshot_new_revision(self):
255
278
        # This tests that a simple commit with no parents makes a new
256
279
        # revision value in the inventory entry
257
 
        self.file_active.snapshot('2', 'subdir/file', {}, self.work_tree, 
258
 
                                  self.branch.weave_store,
 
280
        self.file_active.snapshot('2', 'subdir/file', {}, self.wt, 
 
281
                                  self.branch.repository.weave_store,
259
282
                                  self.branch.get_transaction())
260
283
        # expected outcome - file_1 has a revision id of '2', and we can get
261
284
        # its text of 'file contents' out of the weave.
262
285
        self.assertEqual(self.file_1.revision, '1')
263
286
        self.assertEqual(self.file_active.revision, '2')
264
287
        # this should be a separate test probably, but lets check it once..
265
 
        lines = self.branch.weave_store.get_lines('fileid','2',
266
 
            self.branch.get_transaction())
 
288
        lines = self.branch.repository.weave_store.get_weave(
 
289
            'fileid', 
 
290
            self.branch.get_transaction()).get_lines('2')
267
291
        self.assertEqual(lines, ['contents of subdir/file\n'])
268
292
 
269
293
    def test_snapshot_unchanged(self):
270
294
        #This tests that a simple commit does not make a new entry for
271
295
        # an unchanged inventory entry
272
296
        self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
273
 
                                  self.work_tree, self.branch.weave_store,
 
297
                                  self.wt, 
 
298
                                  self.branch.repository.weave_store,
274
299
                                  self.branch.get_transaction())
275
300
        self.assertEqual(self.file_1.revision, '1')
276
301
        self.assertEqual(self.file_active.revision, '1')
277
 
        self.assertRaises(errors.WeaveError,
278
 
                          self.branch.weave_store.get_lines, 'fileid', '2',
279
 
                          self.branch.get_transaction())
 
302
        vf = self.branch.repository.weave_store.get_weave(
 
303
            'fileid', 
 
304
            self.branch.repository.get_transaction())
 
305
        self.assertRaises(errors.RevisionNotPresent,
 
306
                          vf.get_lines,
 
307
                          '2')
280
308
 
281
309
    def test_snapshot_merge_identical_different_revid(self):
282
310
        # This tests that a commit with two identical parents, one of which has
290
318
        self.assertEqual(self.file_1, other_ie)
291
319
        other_ie.revision = 'other'
292
320
        self.assertNotEqual(self.file_1, other_ie)
293
 
        self.branch.weave_store.add_identical_text('fileid', '1', 'other', ['1'],
294
 
            self.branch.get_transaction())
 
321
        versionfile = self.branch.repository.weave_store.get_weave(
 
322
            'fileid', self.branch.repository.get_transaction())
 
323
        versionfile.clone_text('other', '1', ['1'])
295
324
        self.file_active.snapshot('2', 'subdir/file', 
296
325
                                  {'1':self.file_1, 'other':other_ie},
297
 
                                  self.work_tree, self.branch.weave_store,
 
326
                                  self.wt, 
 
327
                                  self.branch.repository.weave_store,
298
328
                                  self.branch.get_transaction())
299
329
        self.assertEqual(self.file_active.revision, '2')
300
330
 
304
334
        self.file_active.name='newname'
305
335
        rename('subdir/file', 'subdir/newname')
306
336
        self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1}, 
307
 
                                  self.work_tree, 
308
 
                                  self.branch.weave_store,
 
337
                                  self.wt,
 
338
                                  self.branch.repository.weave_store,
309
339
                                  self.branch.get_transaction())
310
340
        # expected outcome - file_1 has a revision id of '2'
311
341
        self.assertEqual(self.file_active.revision, '2')
312
342
 
313
343
 
314
 
class TestPreviousHeads(TestCaseInTempDir):
 
344
class TestPreviousHeads(TestCaseWithTransport):
315
345
 
316
346
    def setUp(self):
317
347
        # we want several inventories, that respectively
323
353
        # D) fileid present in two inventories and one is
324
354
        #   a descendent of the other. (B, D)
325
355
        super(TestPreviousHeads, self).setUp()
 
356
        self.wt = self.make_branch_and_tree('.')
 
357
        self.branch = self.wt.branch
326
358
        self.build_tree(['file'])
327
 
        self.branch = Branch.initialize('.')
328
 
        self.branch.commit('new branch', allow_pointless=True, rev_id='A')
329
 
        self.inv_A = self.branch.get_inventory('A')
330
 
        self.branch.add(['file'], ['fileid'])
331
 
        self.branch.commit('add file', rev_id='B')
332
 
        self.inv_B = self.branch.get_inventory('B')
333
 
        self.branch.put_controlfile('revision-history', 'A\n')
 
359
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
 
360
        self.inv_A = self.branch.repository.get_inventory('A')
 
361
        self.wt.add(['file'], ['fileid'])
 
362
        self.wt.commit('add file', rev_id='B')
 
363
        self.inv_B = self.branch.repository.get_inventory('B')
 
364
        uncommit(self.branch, tree=self.wt)
334
365
        self.assertEqual(self.branch.revision_history(), ['A'])
335
 
        self.branch.commit('another add of file', rev_id='C')
336
 
        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')
339
 
        self.inv_D = self.branch.get_inventory('D')
340
 
        self.file_active = self.branch.working_tree().inventory['fileid']
341
 
        self.weave = self.branch.weave_store.get_weave('fileid',
342
 
            self.branch.get_transaction())
 
366
        self.wt.commit('another add of file', rev_id='C')
 
367
        self.inv_C = self.branch.repository.get_inventory('C')
 
368
        self.wt.add_pending_merge('B')
 
369
        self.wt.commit('merge in B', rev_id='D')
 
370
        self.inv_D = self.branch.repository.get_inventory('D')
 
371
        self.file_active = self.wt.inventory['fileid']
 
372
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
 
373
            self.branch.repository.get_transaction())
343
374
        
344
375
    def get_previous_heads(self, inventories):
345
 
        return self.file_active.find_previous_heads(inventories, self.weave)
 
376
        return self.file_active.find_previous_heads(
 
377
            inventories, 
 
378
            self.branch.repository.weave_store,
 
379
            self.branch.repository.get_transaction())
346
380
        
347
381
    def test_fileid_in_no_inventory(self):
348
382
        self.assertEqual({}, self.get_previous_heads([self.inv_A]))
370
404
                         self.get_previous_heads([self.inv_D, self.inv_B]))
371
405
 
372
406
    # TODO: test two inventories with the same file revision 
 
407
 
 
408
 
 
409
class TestDescribeChanges(TestCase):
 
410
 
 
411
    def test_describe_change(self):
 
412
        # we need to test the following change combinations:
 
413
        # rename
 
414
        # reparent
 
415
        # modify
 
416
        # gone
 
417
        # added
 
418
        # renamed/reparented and modified
 
419
        # change kind (perhaps can't be done yet?)
 
420
        # also, merged in combination with all of these?
 
421
        old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
422
        old_a.text_sha1 = '123132'
 
423
        old_a.text_size = 0
 
424
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
425
        new_a.text_sha1 = '123132'
 
426
        new_a.text_size = 0
 
427
 
 
428
        self.assertChangeDescription('unchanged', old_a, new_a)
 
429
 
 
430
        new_a.text_size = 10
 
431
        new_a.text_sha1 = 'abcabc'
 
432
        self.assertChangeDescription('modified', old_a, new_a)
 
433
 
 
434
        self.assertChangeDescription('added', None, new_a)
 
435
        self.assertChangeDescription('removed', old_a, None)
 
436
        # perhaps a bit questionable but seems like the most reasonable thing...
 
437
        self.assertChangeDescription('unchanged', None, None)
 
438
 
 
439
        # in this case it's both renamed and modified; show a rename and 
 
440
        # modification:
 
441
        new_a.name = 'newfilename'
 
442
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
443
 
 
444
        # reparenting is 'renaming'
 
445
        new_a.name = old_a.name
 
446
        new_a.parent_id = 'somedir-id'
 
447
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
448
 
 
449
        # reset the content values so its not modified
 
450
        new_a.text_size = old_a.text_size
 
451
        new_a.text_sha1 = old_a.text_sha1
 
452
        new_a.name = old_a.name
 
453
 
 
454
        new_a.name = 'newfilename'
 
455
        self.assertChangeDescription('renamed', old_a, new_a)
 
456
 
 
457
        # reparenting is 'renaming'
 
458
        new_a.name = old_a.name
 
459
        new_a.parent_id = 'somedir-id'
 
460
        self.assertChangeDescription('renamed', old_a, new_a)
 
461
 
 
462
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
 
463
        change = InventoryEntry.describe_change(old_ie, new_ie)
 
464
        self.assertEqual(expected_change, change)
 
465
 
 
466
 
 
467
class TestExecutable(TestCaseWithTransport):
 
468
 
 
469
    def test_stays_executable(self):
 
470
        a_id = "a-20051208024829-849e76f7968d7a86"
 
471
        b_id = "b-20051208024829-849e76f7968d7a86"
 
472
        wt = self.make_branch_and_tree('b1')
 
473
        b = wt.branch
 
474
        tt = TreeTransform(wt)
 
475
        tt.new_file('a', tt.root, 'a test\n', a_id, True)
 
476
        tt.new_file('b', tt.root, 'b test\n', b_id, False)
 
477
        tt.apply()
 
478
 
 
479
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
480
 
 
481
        # reopen the tree and ensure it stuck.
 
482
        wt = wt.bzrdir.open_workingtree()
 
483
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
484
 
 
485
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
486
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
487
 
 
488
        wt.commit('adding a,b', rev_id='r1')
 
489
 
 
490
        rev_tree = b.repository.revision_tree('r1')
 
491
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
 
492
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
 
493
 
 
494
        self.failUnless(rev_tree.inventory[a_id].executable)
 
495
        self.failIf(rev_tree.inventory[b_id].executable)
 
496
 
 
497
        # Make sure the entries are gone
 
498
        os.remove('b1/a')
 
499
        os.remove('b1/b')
 
500
        self.failIf(wt.has_id(a_id))
 
501
        self.failIf(wt.has_filename('a'))
 
502
        self.failIf(wt.has_id(b_id))
 
503
        self.failIf(wt.has_filename('b'))
 
504
 
 
505
        # Make sure that revert is able to bring them back,
 
506
        # and sets 'a' back to being executable
 
507
 
 
508
        wt.revert(['a', 'b'], rev_tree, backups=False)
 
509
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
510
 
 
511
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
512
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
513
 
 
514
        # Now remove them again, and make sure that after a
 
515
        # commit, they are still marked correctly
 
516
        os.remove('b1/a')
 
517
        os.remove('b1/b')
 
518
        wt.commit('removed', rev_id='r2')
 
519
 
 
520
        self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
 
521
        self.failIf(wt.has_id(a_id))
 
522
        self.failIf(wt.has_filename('a'))
 
523
        self.failIf(wt.has_id(b_id))
 
524
        self.failIf(wt.has_filename('b'))
 
525
 
 
526
        # Now revert back to the previous commit
 
527
        wt.revert([], rev_tree, backups=False)
 
528
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
529
 
 
530
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
531
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
532
 
 
533
        # Now make sure that 'bzr branch' also preserves the
 
534
        # executable bit
 
535
        # TODO: Maybe this should be a blackbox test
 
536
        d2 = b.bzrdir.clone('b2', revision_id='r1')
 
537
        t2 = d2.open_workingtree()
 
538
        b2 = t2.branch
 
539
        self.assertEquals('r1', b2.last_revision())
 
540
 
 
541
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
542
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
543
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
544
 
 
545
        # Make sure pull will delete the files
 
546
        t2.pull(b)
 
547
        self.assertEquals('r2', b2.last_revision())
 
548
        self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
 
549
 
 
550
        # Now commit the changes on the first branch
 
551
        # so that the second branch can pull the changes
 
552
        # and make sure that the executable bit has been copied
 
553
        wt.commit('resurrected', rev_id='r3')
 
554
 
 
555
        t2.pull(b)
 
556
        self.assertEquals('r3', b2.last_revision())
 
557
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
558
 
 
559
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
560
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
561
 
 
562
 
 
563
class TestRevert(TestCaseWithTransport):
 
564
 
 
565
    def test_dangling_id(self):
 
566
        wt = self.make_branch_and_tree('b1')
 
567
        self.assertEqual(len(wt.inventory), 1)
 
568
        open('b1/a', 'wb').write('a test\n')
 
569
        wt.add('a')
 
570
        self.assertEqual(len(wt.inventory), 2)
 
571
        os.unlink('b1/a')
 
572
        wt.revert([])
 
573
        self.assertEqual(len(wt.inventory), 1)