20
20
from bzrlib.branch import Branch
21
21
import bzrlib.errors as errors
22
22
from bzrlib.diff import internal_diff
23
from bzrlib.inventory import Inventory, ROOT_ID
23
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
24
InventoryDirectory, InventoryEntry)
24
25
import bzrlib.inventory as inventory
25
26
from bzrlib.osutils import has_symlinks, rename, pathjoin
26
27
from bzrlib.tests import TestCase, TestCaseWithTransport
28
from bzrlib.transform import TreeTransform
29
from bzrlib.uncommit import uncommit
29
32
class TestInventory(TestCase):
139
142
self.wt = self.make_branch_and_tree('.')
140
143
self.branch = self.wt.branch
141
144
print >> open('file', 'wb'), 'foo'
145
print >> open('binfile', 'wb'), 'foo'
142
146
self.wt.add(['file'], ['fileid'])
147
self.wt.add(['binfile'], ['binfileid'])
143
148
if has_symlinks():
144
149
os.symlink('target1', 'symlink')
145
150
self.wt.add(['symlink'], ['linkid'])
146
151
self.wt.commit('message_1', rev_id = '1')
147
152
print >> open('file', 'wb'), 'bar'
153
print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
148
154
if has_symlinks():
149
155
os.unlink('symlink')
150
156
os.symlink('target2', 'symlink')
151
157
self.tree_1 = self.branch.repository.revision_tree('1')
152
158
self.inv_1 = self.branch.repository.get_inventory('1')
153
159
self.file_1 = self.inv_1['fileid']
160
self.file_1b = self.inv_1['binfileid']
154
161
self.tree_2 = self.wt
155
162
self.inv_2 = self.tree_2.read_working_inventory()
156
163
self.file_2 = self.inv_2['fileid']
164
self.file_2b = self.inv_2['binfileid']
157
165
if has_symlinks():
158
166
self.link_1 = self.inv_1['linkid']
159
167
self.link_2 = self.inv_2['linkid']
388
400
# TODO: test two inventories with the same file revision
403
class TestDescribeChanges(TestCase):
405
def test_describe_change(self):
406
# we need to test the following change combinations:
412
# renamed/reparented and modified
413
# change kind (perhaps can't be done yet?)
414
# also, merged in combination with all of these?
415
old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
416
old_a.text_sha1 = '123132'
418
new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
419
new_a.text_sha1 = '123132'
422
self.assertChangeDescription('unchanged', old_a, new_a)
425
new_a.text_sha1 = 'abcabc'
426
self.assertChangeDescription('modified', old_a, new_a)
428
self.assertChangeDescription('added', None, new_a)
429
self.assertChangeDescription('removed', old_a, None)
430
# perhaps a bit questionable but seems like the most reasonable thing...
431
self.assertChangeDescription('unchanged', None, None)
433
# in this case it's both renamed and modified; show a rename and
435
new_a.name = 'newfilename'
436
self.assertChangeDescription('modified and renamed', old_a, new_a)
438
# reparenting is 'renaming'
439
new_a.name = old_a.name
440
new_a.parent_id = 'somedir-id'
441
self.assertChangeDescription('modified and renamed', old_a, new_a)
443
# reset the content values so its not modified
444
new_a.text_size = old_a.text_size
445
new_a.text_sha1 = old_a.text_sha1
446
new_a.name = old_a.name
448
new_a.name = 'newfilename'
449
self.assertChangeDescription('renamed', old_a, new_a)
451
# reparenting is 'renaming'
452
new_a.name = old_a.name
453
new_a.parent_id = 'somedir-id'
454
self.assertChangeDescription('renamed', old_a, new_a)
456
def assertChangeDescription(self, expected_change, old_ie, new_ie):
457
change = InventoryEntry.describe_change(old_ie, new_ie)
458
self.assertEqual(expected_change, change)
391
461
class TestExecutable(TestCaseWithTransport):
393
463
def test_stays_executable(self):
394
basic_inv = """<inventory format="5">
395
<file file_id="a-20051208024829-849e76f7968d7a86" name="a" executable="yes" />
396
<file file_id="b-20051208024829-849e76f7968d7a86" name="b" />
399
wt = self.make_branch_and_tree('b1')
401
open('b1/a', 'wb').write('a test\n')
402
open('b1/b', 'wb').write('b test\n')
403
os.chmod('b1/a', 0755)
404
os.chmod('b1/b', 0644)
405
# Manually writing the inventory, to ensure that
406
# the executable="yes" entry is set for 'a' and not for 'b'
407
open('b1/.bzr/inventory', 'wb').write(basic_inv)
409
464
a_id = "a-20051208024829-849e76f7968d7a86"
410
465
b_id = "b-20051208024829-849e76f7968d7a86"
466
wt = self.make_branch_and_tree('b1')
468
tt = TreeTransform(wt)
469
tt.new_file('a', tt.root, 'a test\n', a_id, True)
470
tt.new_file('b', tt.root, 'b test\n', b_id, False)
473
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
475
# reopen the tree and ensure it stuck.
411
476
wt = wt.bzrdir.open_workingtree()
412
477
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])