~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Robert Collins
  • Date: 2006-04-18 22:41:16 UTC
  • mto: (1711.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1671.
  • Revision ID: robertc@robertcollins.net-20060418224116-9b723440fa56e404
 * 'Metadir' is now the default disk format. This improves behaviour in
   SFTP using circumstances and allows binding and rebinding and easier
   use of repositories. (Robert Collins, Aaron Bentley).

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
 
from bzrlib.osutils import has_symlinks, rename
27
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
25
from bzrlib.osutils import has_symlinks, rename, pathjoin
 
26
from bzrlib.tests import TestCase, TestCaseWithTransport
 
27
from bzrlib.uncommit import uncommit
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'),
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('.')
 
140
        self.wt = self.make_branch_and_tree('.')
 
141
        self.branch = self.wt.branch
141
142
        print >> open('file', 'wb'), 'foo'
142
 
        self.branch.add(['file'], ['fileid'])
 
143
        self.wt.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.wt.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')
150
151
            os.symlink('target2', 'symlink')
151
 
        self.tree_1 = self.branch.revision_tree('1')
152
 
        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')
153
154
        self.file_1 = self.inv_1['fileid']
154
 
        self.tree_2 = self.branch.working_tree()
155
 
        self.inv_2 = self.branch.inventory
 
155
        self.tree_2 = self.wt
 
156
        self.inv_2 = self.tree_2.read_working_inventory()
156
157
        self.file_2 = self.inv_2['fileid']
157
158
        if has_symlinks():
158
159
            self.link_1 = self.inv_1['linkid']
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"
229
230
                         "=== target changed 'target1' => 'target2'\n")
230
231
 
231
232
 
232
 
class TestSnapshot(TestCaseInTempDir):
 
233
class TestSnapshot(TestCaseWithTransport):
233
234
 
234
235
    def setUp(self):
235
236
        # for full testing we'll need a branch
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.wt = self.make_branch_and_tree('.')
 
244
        self.branch = self.wt.branch
 
245
        self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
 
246
        self.wt.add(['subdir', 'subdir/file'],
 
247
                                       ['dirid', 'fileid'])
245
248
        if has_symlinks():
246
249
            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')
 
250
        self.wt.commit('message_1', rev_id = '1')
 
251
        self.tree_1 = self.branch.repository.revision_tree('1')
 
252
        self.inv_1 = self.branch.repository.get_inventory('1')
250
253
        self.file_1 = self.inv_1['fileid']
251
 
        self.work_tree = self.branch.working_tree()
252
 
        self.file_active = self.work_tree.inventory['fileid']
 
254
        self.file_active = self.wt.inventory['fileid']
253
255
 
254
256
    def test_snapshot_new_revision(self):
255
257
        # This tests that a simple commit with no parents makes a new
256
258
        # revision value in the inventory entry
257
 
        self.file_active.snapshot('2', 'subdir/file', {}, self.work_tree, 
258
 
                                  self.branch.weave_store,
 
259
        self.file_active.snapshot('2', 'subdir/file', {}, self.wt, 
 
260
                                  self.branch.repository.weave_store,
259
261
                                  self.branch.get_transaction())
260
262
        # expected outcome - file_1 has a revision id of '2', and we can get
261
263
        # its text of 'file contents' out of the weave.
262
264
        self.assertEqual(self.file_1.revision, '1')
263
265
        self.assertEqual(self.file_active.revision, '2')
264
266
        # 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())
 
267
        lines = self.branch.repository.weave_store.get_weave(
 
268
            'fileid', 
 
269
            self.branch.get_transaction()).get_lines('2')
267
270
        self.assertEqual(lines, ['contents of subdir/file\n'])
268
271
 
269
272
    def test_snapshot_unchanged(self):
270
273
        #This tests that a simple commit does not make a new entry for
271
274
        # an unchanged inventory entry
272
275
        self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
273
 
                                  self.work_tree, self.branch.weave_store,
 
276
                                  self.wt, 
 
277
                                  self.branch.repository.weave_store,
274
278
                                  self.branch.get_transaction())
275
279
        self.assertEqual(self.file_1.revision, '1')
276
280
        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())
 
281
        vf = self.branch.repository.weave_store.get_weave(
 
282
            'fileid', 
 
283
            self.branch.repository.get_transaction())
 
284
        self.assertRaises(errors.RevisionNotPresent,
 
285
                          vf.get_lines,
 
286
                          '2')
280
287
 
281
288
    def test_snapshot_merge_identical_different_revid(self):
282
289
        # This tests that a commit with two identical parents, one of which has
290
297
        self.assertEqual(self.file_1, other_ie)
291
298
        other_ie.revision = 'other'
292
299
        self.assertNotEqual(self.file_1, other_ie)
293
 
        self.branch.weave_store.add_identical_text('fileid', '1', 'other', ['1'],
294
 
            self.branch.get_transaction())
 
300
        versionfile = self.branch.repository.weave_store.get_weave(
 
301
            'fileid', self.branch.repository.get_transaction())
 
302
        versionfile.clone_text('other', '1', ['1'])
295
303
        self.file_active.snapshot('2', 'subdir/file', 
296
304
                                  {'1':self.file_1, 'other':other_ie},
297
 
                                  self.work_tree, self.branch.weave_store,
 
305
                                  self.wt, 
 
306
                                  self.branch.repository.weave_store,
298
307
                                  self.branch.get_transaction())
299
308
        self.assertEqual(self.file_active.revision, '2')
300
309
 
304
313
        self.file_active.name='newname'
305
314
        rename('subdir/file', 'subdir/newname')
306
315
        self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1}, 
307
 
                                  self.work_tree, 
308
 
                                  self.branch.weave_store,
 
316
                                  self.wt,
 
317
                                  self.branch.repository.weave_store,
309
318
                                  self.branch.get_transaction())
310
319
        # expected outcome - file_1 has a revision id of '2'
311
320
        self.assertEqual(self.file_active.revision, '2')
312
321
 
313
322
 
314
 
class TestPreviousHeads(TestCaseInTempDir):
 
323
class TestPreviousHeads(TestCaseWithTransport):
315
324
 
316
325
    def setUp(self):
317
326
        # we want several inventories, that respectively
323
332
        # D) fileid present in two inventories and one is
324
333
        #   a descendent of the other. (B, D)
325
334
        super(TestPreviousHeads, self).setUp()
 
335
        self.wt = self.make_branch_and_tree('.')
 
336
        self.branch = self.wt.branch
326
337
        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')
 
338
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
 
339
        self.inv_A = self.branch.repository.get_inventory('A')
 
340
        self.wt.add(['file'], ['fileid'])
 
341
        self.wt.commit('add file', rev_id='B')
 
342
        self.inv_B = self.branch.repository.get_inventory('B')
 
343
        uncommit(self.branch, tree=self.wt)
334
344
        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())
 
345
        self.wt.commit('another add of file', rev_id='C')
 
346
        self.inv_C = self.branch.repository.get_inventory('C')
 
347
        self.wt.add_pending_merge('B')
 
348
        self.wt.commit('merge in B', rev_id='D')
 
349
        self.inv_D = self.branch.repository.get_inventory('D')
 
350
        self.file_active = self.wt.inventory['fileid']
 
351
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
 
352
            self.branch.repository.get_transaction())
343
353
        
344
354
    def get_previous_heads(self, inventories):
345
 
        return self.file_active.find_previous_heads(inventories, self.weave)
 
355
        return self.file_active.find_previous_heads(
 
356
            inventories, 
 
357
            self.branch.repository.weave_store,
 
358
            self.branch.repository.get_transaction())
346
359
        
347
360
    def test_fileid_in_no_inventory(self):
348
361
        self.assertEqual({}, self.get_previous_heads([self.inv_A]))
370
383
                         self.get_previous_heads([self.inv_D, self.inv_B]))
371
384
 
372
385
    # TODO: test two inventories with the same file revision 
 
386
 
 
387
 
 
388
class TestExecutable(TestCaseWithTransport):
 
389
 
 
390
    def test_stays_executable(self):
 
391
        a_id = "a-20051208024829-849e76f7968d7a86"
 
392
        b_id = "b-20051208024829-849e76f7968d7a86"
 
393
        wt = self.make_branch_and_tree('b1')
 
394
        b = wt.branch
 
395
        open('b1/a', 'wb').write('a test\n')
 
396
        open('b1/b', 'wb').write('b test\n')
 
397
        os.chmod('b1/a', 0755)
 
398
        os.chmod('b1/b', 0644)
 
399
        wt.add(['a', 'b'], [a_id, b_id])
 
400
        wt.inventory[a_id].executable = True
 
401
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
402
 
 
403
        # reopen the tree and ensure it stuck.
 
404
        wt = wt.bzrdir.open_workingtree()
 
405
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
406
 
 
407
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
408
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
409
 
 
410
        wt.commit('adding a,b', rev_id='r1')
 
411
 
 
412
        rev_tree = b.repository.revision_tree('r1')
 
413
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
 
414
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
 
415
 
 
416
        self.failUnless(rev_tree.inventory[a_id].executable)
 
417
        self.failIf(rev_tree.inventory[b_id].executable)
 
418
 
 
419
        # Make sure the entries are gone
 
420
        os.remove('b1/a')
 
421
        os.remove('b1/b')
 
422
        self.failIf(wt.has_id(a_id))
 
423
        self.failIf(wt.has_filename('a'))
 
424
        self.failIf(wt.has_id(b_id))
 
425
        self.failIf(wt.has_filename('b'))
 
426
 
 
427
        # Make sure that revert is able to bring them back,
 
428
        # and sets 'a' back to being executable
 
429
 
 
430
        wt.revert(['a', 'b'], rev_tree, backups=False)
 
431
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
432
 
 
433
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
434
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
435
 
 
436
        # Now remove them again, and make sure that after a
 
437
        # commit, they are still marked correctly
 
438
        os.remove('b1/a')
 
439
        os.remove('b1/b')
 
440
        wt.commit('removed', rev_id='r2')
 
441
 
 
442
        self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
 
443
        self.failIf(wt.has_id(a_id))
 
444
        self.failIf(wt.has_filename('a'))
 
445
        self.failIf(wt.has_id(b_id))
 
446
        self.failIf(wt.has_filename('b'))
 
447
 
 
448
        # Now revert back to the previous commit
 
449
        wt.revert([], rev_tree, backups=False)
 
450
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
451
 
 
452
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
453
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
454
 
 
455
        # Now make sure that 'bzr branch' also preserves the
 
456
        # executable bit
 
457
        # TODO: Maybe this should be a blackbox test
 
458
        d2 = b.bzrdir.clone('b2', revision_id='r1')
 
459
        t2 = d2.open_workingtree()
 
460
        b2 = t2.branch
 
461
        self.assertEquals('r1', b2.last_revision())
 
462
 
 
463
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
464
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
465
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
466
 
 
467
        # Make sure pull will delete the files
 
468
        t2.pull(b)
 
469
        self.assertEquals('r2', b2.last_revision())
 
470
        self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
 
471
 
 
472
        # Now commit the changes on the first branch
 
473
        # so that the second branch can pull the changes
 
474
        # and make sure that the executable bit has been copied
 
475
        wt.commit('resurrected', rev_id='r3')
 
476
 
 
477
        t2.pull(b)
 
478
        self.assertEquals('r3', b2.last_revision())
 
479
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
480
 
 
481
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
482
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
483
 
 
484
class TestRevert(TestCaseWithTransport):
 
485
    def test_dangling_id(self):
 
486
        wt = self.make_branch_and_tree('b1')
 
487
        self.assertEqual(len(wt.inventory), 1)
 
488
        open('b1/a', 'wb').write('a test\n')
 
489
        wt.add('a')
 
490
        self.assertEqual(len(wt.inventory), 2)
 
491
        os.unlink('b1/a')
 
492
        wt.revert([])
 
493
        self.assertEqual(len(wt.inventory), 1)
 
494
 
 
495