132
133
link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
133
134
self.failIf(link.has_text())
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)
136
class TestEntryDiffing(TestCaseInTempDir):
144
class TestEntryDiffing(TestCaseWithTransport):
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']
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'],
245
269
if has_symlinks():
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']
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(
290
self.branch.get_transaction()).get_lines('2')
267
291
self.assertEqual(lines, ['contents of subdir/file\n'])
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,
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(
304
self.branch.repository.get_transaction())
305
self.assertRaises(errors.RevisionNotPresent,
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
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())
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(
378
self.branch.repository.weave_store,
379
self.branch.repository.get_transaction())
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]))
372
406
# TODO: test two inventories with the same file revision
409
class TestDescribeChanges(TestCase):
411
def test_describe_change(self):
412
# we need to test the following change combinations:
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'
424
new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
425
new_a.text_sha1 = '123132'
428
self.assertChangeDescription('unchanged', old_a, new_a)
431
new_a.text_sha1 = 'abcabc'
432
self.assertChangeDescription('modified', old_a, new_a)
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)
439
# in this case it's both renamed and modified; show a rename and
441
new_a.name = 'newfilename'
442
self.assertChangeDescription('modified and renamed', old_a, new_a)
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)
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
454
new_a.name = 'newfilename'
455
self.assertChangeDescription('renamed', old_a, new_a)
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)
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)
467
class TestExecutable(TestCaseWithTransport):
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')
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)
479
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
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()])
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")
488
wt.commit('adding a,b', rev_id='r1')
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")
494
self.failUnless(rev_tree.inventory[a_id].executable)
495
self.failIf(rev_tree.inventory[b_id].executable)
497
# Make sure the entries are gone
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'))
505
# Make sure that revert is able to bring them back,
506
# and sets 'a' back to being executable
508
wt.revert(['a', 'b'], rev_tree, backups=False)
509
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
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")
514
# Now remove them again, and make sure that after a
515
# commit, they are still marked correctly
518
wt.commit('removed', rev_id='r2')
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'))
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()])
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")
533
# Now make sure that 'bzr branch' also preserves the
535
# TODO: Maybe this should be a blackbox test
536
d2 = b.bzrdir.clone('b2', revision_id='r1')
537
t2 = d2.open_workingtree()
539
self.assertEquals('r1', b2.last_revision())
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")
545
# Make sure pull will delete the files
547
self.assertEquals('r2', b2.last_revision())
548
self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
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')
556
self.assertEquals('r3', b2.last_revision())
557
self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
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")
563
class TestRevert(TestCaseWithTransport):
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')
570
self.assertEqual(len(wt.inventory), 2)
573
self.assertEqual(len(wt.inventory), 1)