1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from cStringIO import StringIO
20
from bzrlib.branch import Branch
21
import bzrlib.errors as errors
22
from bzrlib.diff import internal_diff
23
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
24
InventoryDirectory, InventoryEntry)
25
import bzrlib.inventory as inventory
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
32
class TestInventory(TestCase):
34
def test_is_within(self):
35
from bzrlib.osutils import is_inside_any
37
SRC_FOO_C = pathjoin('src', 'foo.c')
38
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
42
self.assert_(is_inside_any(dirs, fn))
44
for dirs, fn in [(['src'], 'srccontrol'),
45
(['src'], 'srccontrol/foo')]:
46
self.assertFalse(is_inside_any(dirs, fn))
49
"""Test detection of files within selected directories."""
52
for args in [('src', 'directory', 'src-id'),
53
('doc', 'directory', 'doc-id'),
54
('src/hello.c', 'file'),
55
('src/bye.c', 'file', 'bye-id'),
56
('Makefile', 'file')]:
59
self.assertEqual(inv.path2id('src'), 'src-id')
60
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
62
self.assert_('src-id' in inv)
65
def test_version(self):
66
"""Inventory remembers the text's version."""
68
ie = inv.add_path('foo.txt', 'file')
72
class TestInventoryEntry(TestCase):
74
def test_file_kind_character(self):
75
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
76
self.assertEqual(file.kind_character(), '')
78
def test_dir_kind_character(self):
79
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
80
self.assertEqual(dir.kind_character(), '/')
82
def test_link_kind_character(self):
83
dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
84
self.assertEqual(dir.kind_character(), '')
86
def test_dir_detect_changes(self):
87
left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
89
left.executable = True
90
left.symlink_target='foo'
91
right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
93
right.symlink_target='bar'
94
self.assertEqual((False, False), left.detect_changes(right))
95
self.assertEqual((False, False), right.detect_changes(left))
97
def test_file_detect_changes(self):
98
left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
100
right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
101
right.text_sha1 = 123
102
self.assertEqual((False, False), left.detect_changes(right))
103
self.assertEqual((False, False), right.detect_changes(left))
104
left.executable = True
105
self.assertEqual((False, True), left.detect_changes(right))
106
self.assertEqual((False, True), right.detect_changes(left))
107
right.text_sha1 = 321
108
self.assertEqual((True, True), left.detect_changes(right))
109
self.assertEqual((True, True), right.detect_changes(left))
111
def test_symlink_detect_changes(self):
112
left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
114
left.executable = True
115
left.symlink_target='foo'
116
right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
117
right.text_sha1 = 321
118
right.symlink_target='foo'
119
self.assertEqual((False, False), left.detect_changes(right))
120
self.assertEqual((False, False), right.detect_changes(left))
121
left.symlink_target = 'different'
122
self.assertEqual((True, False), left.detect_changes(right))
123
self.assertEqual((True, False), right.detect_changes(left))
125
def test_file_has_text(self):
126
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
127
self.failUnless(file.has_text())
129
def test_directory_has_text(self):
130
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
131
self.failIf(dir.has_text())
133
def test_link_has_text(self):
134
link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
135
self.failIf(link.has_text())
138
class TestEntryDiffing(TestCaseWithTransport):
141
super(TestEntryDiffing, self).setUp()
142
self.wt = self.make_branch_and_tree('.')
143
self.branch = self.wt.branch
144
print >> open('file', 'wb'), 'foo'
145
print >> open('binfile', 'wb'), 'foo'
146
self.wt.add(['file'], ['fileid'])
147
self.wt.add(['binfile'], ['binfileid'])
149
os.symlink('target1', 'symlink')
150
self.wt.add(['symlink'], ['linkid'])
151
self.wt.commit('message_1', rev_id = '1')
152
print >> open('file', 'wb'), 'bar'
153
print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
156
os.symlink('target2', 'symlink')
157
self.tree_1 = self.branch.repository.revision_tree('1')
158
self.inv_1 = self.branch.repository.get_inventory('1')
159
self.file_1 = self.inv_1['fileid']
160
self.file_1b = self.inv_1['binfileid']
161
self.tree_2 = self.wt
162
self.inv_2 = self.tree_2.read_working_inventory()
163
self.file_2 = self.inv_2['fileid']
164
self.file_2b = self.inv_2['binfileid']
166
self.link_1 = self.inv_1['linkid']
167
self.link_2 = self.inv_2['linkid']
169
def test_file_diff_deleted(self):
171
self.file_1.diff(internal_diff,
172
"old_label", self.tree_1,
173
"/dev/null", None, None,
175
self.assertEqual(output.getvalue(), "--- old_label\t\n"
181
def test_file_diff_added(self):
183
self.file_1.diff(internal_diff,
184
"new_label", self.tree_1,
185
"/dev/null", None, None,
186
output, reverse=True)
187
self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
193
def test_file_diff_changed(self):
195
self.file_1.diff(internal_diff,
196
"/dev/null", self.tree_1,
197
"new_label", self.file_2, self.tree_2,
199
self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
206
def test_file_diff_binary(self):
208
self.file_1.diff(internal_diff,
209
"/dev/null", self.tree_1,
210
"new_label", self.file_2b, self.tree_2,
212
self.assertEqual(output.getvalue(),
213
"Binary files /dev/null and new_label differ\n")
214
def test_link_diff_deleted(self):
215
if not has_symlinks():
218
self.link_1.diff(internal_diff,
219
"old_label", self.tree_1,
220
"/dev/null", None, None,
222
self.assertEqual(output.getvalue(),
223
"=== target was 'target1'\n")
225
def test_link_diff_added(self):
226
if not has_symlinks():
229
self.link_1.diff(internal_diff,
230
"new_label", self.tree_1,
231
"/dev/null", None, None,
232
output, reverse=True)
233
self.assertEqual(output.getvalue(),
234
"=== target is 'target1'\n")
236
def test_link_diff_changed(self):
237
if not has_symlinks():
240
self.link_1.diff(internal_diff,
241
"/dev/null", self.tree_1,
242
"new_label", self.link_2, self.tree_2,
244
self.assertEqual(output.getvalue(),
245
"=== target changed 'target1' => 'target2'\n")
248
class TestSnapshot(TestCaseWithTransport):
251
# for full testing we'll need a branch
252
# with a subdir to test parent changes.
253
# and a file, link and dir under that.
254
# but right now I only need one attribute
255
# to change, and then test merge patterns
256
# with fake parent entries.
257
super(TestSnapshot, self).setUp()
258
self.wt = self.make_branch_and_tree('.')
259
self.branch = self.wt.branch
260
self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
261
self.wt.add(['subdir', 'subdir/file'],
265
self.wt.commit('message_1', rev_id = '1')
266
self.tree_1 = self.branch.repository.revision_tree('1')
267
self.inv_1 = self.branch.repository.get_inventory('1')
268
self.file_1 = self.inv_1['fileid']
269
self.file_active = self.wt.inventory['fileid']
271
def test_snapshot_new_revision(self):
272
# This tests that a simple commit with no parents makes a new
273
# revision value in the inventory entry
274
self.file_active.snapshot('2', 'subdir/file', {}, self.wt,
275
self.branch.repository.weave_store,
276
self.branch.get_transaction())
277
# expected outcome - file_1 has a revision id of '2', and we can get
278
# its text of 'file contents' out of the weave.
279
self.assertEqual(self.file_1.revision, '1')
280
self.assertEqual(self.file_active.revision, '2')
281
# this should be a separate test probably, but lets check it once..
282
lines = self.branch.repository.weave_store.get_weave(
284
self.branch.get_transaction()).get_lines('2')
285
self.assertEqual(lines, ['contents of subdir/file\n'])
287
def test_snapshot_unchanged(self):
288
#This tests that a simple commit does not make a new entry for
289
# an unchanged inventory entry
290
self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
292
self.branch.repository.weave_store,
293
self.branch.get_transaction())
294
self.assertEqual(self.file_1.revision, '1')
295
self.assertEqual(self.file_active.revision, '1')
296
vf = self.branch.repository.weave_store.get_weave(
298
self.branch.repository.get_transaction())
299
self.assertRaises(errors.RevisionNotPresent,
303
def test_snapshot_merge_identical_different_revid(self):
304
# This tests that a commit with two identical parents, one of which has
305
# a different revision id, results in a new revision id in the entry.
306
# 1->other, commit a merge of other against 1, results in 2.
307
other_ie = inventory.InventoryFile('fileid', 'newname', self.file_1.parent_id)
308
other_ie = inventory.InventoryFile('fileid', 'file', self.file_1.parent_id)
309
other_ie.revision = '1'
310
other_ie.text_sha1 = self.file_1.text_sha1
311
other_ie.text_size = self.file_1.text_size
312
self.assertEqual(self.file_1, other_ie)
313
other_ie.revision = 'other'
314
self.assertNotEqual(self.file_1, other_ie)
315
versionfile = self.branch.repository.weave_store.get_weave(
316
'fileid', self.branch.repository.get_transaction())
317
versionfile.clone_text('other', '1', ['1'])
318
self.file_active.snapshot('2', 'subdir/file',
319
{'1':self.file_1, 'other':other_ie},
321
self.branch.repository.weave_store,
322
self.branch.get_transaction())
323
self.assertEqual(self.file_active.revision, '2')
325
def test_snapshot_changed(self):
326
# This tests that a commit with one different parent results in a new
327
# revision id in the entry.
328
self.file_active.name='newname'
329
rename('subdir/file', 'subdir/newname')
330
self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1},
332
self.branch.repository.weave_store,
333
self.branch.get_transaction())
334
# expected outcome - file_1 has a revision id of '2'
335
self.assertEqual(self.file_active.revision, '2')
338
class TestPreviousHeads(TestCaseWithTransport):
341
# we want several inventories, that respectively
342
# give use the following scenarios:
343
# A) fileid not in any inventory (A),
344
# B) fileid present in one inventory (B) and (A,B)
345
# C) fileid present in two inventories, and they
346
# are not mutual descendents (B, C)
347
# D) fileid present in two inventories and one is
348
# a descendent of the other. (B, D)
349
super(TestPreviousHeads, self).setUp()
350
self.wt = self.make_branch_and_tree('.')
351
self.branch = self.wt.branch
352
self.build_tree(['file'])
353
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
354
self.inv_A = self.branch.repository.get_inventory('A')
355
self.wt.add(['file'], ['fileid'])
356
self.wt.commit('add file', rev_id='B')
357
self.inv_B = self.branch.repository.get_inventory('B')
358
uncommit(self.branch, tree=self.wt)
359
self.assertEqual(self.branch.revision_history(), ['A'])
360
self.wt.commit('another add of file', rev_id='C')
361
self.inv_C = self.branch.repository.get_inventory('C')
362
self.wt.add_pending_merge('B')
363
self.wt.commit('merge in B', rev_id='D')
364
self.inv_D = self.branch.repository.get_inventory('D')
365
self.file_active = self.wt.inventory['fileid']
366
self.weave = self.branch.repository.weave_store.get_weave('fileid',
367
self.branch.repository.get_transaction())
369
def get_previous_heads(self, inventories):
370
return self.file_active.find_previous_heads(
372
self.branch.repository.weave_store,
373
self.branch.repository.get_transaction())
375
def test_fileid_in_no_inventory(self):
376
self.assertEqual({}, self.get_previous_heads([self.inv_A]))
378
def test_fileid_in_one_inventory(self):
379
self.assertEqual({'B':self.inv_B['fileid']},
380
self.get_previous_heads([self.inv_B]))
381
self.assertEqual({'B':self.inv_B['fileid']},
382
self.get_previous_heads([self.inv_A, self.inv_B]))
383
self.assertEqual({'B':self.inv_B['fileid']},
384
self.get_previous_heads([self.inv_B, self.inv_A]))
386
def test_fileid_in_two_inventories_gives_both_entries(self):
387
self.assertEqual({'B':self.inv_B['fileid'],
388
'C':self.inv_C['fileid']},
389
self.get_previous_heads([self.inv_B, self.inv_C]))
390
self.assertEqual({'B':self.inv_B['fileid'],
391
'C':self.inv_C['fileid']},
392
self.get_previous_heads([self.inv_C, self.inv_B]))
394
def test_fileid_in_two_inventories_already_merged_gives_head(self):
395
self.assertEqual({'D':self.inv_D['fileid']},
396
self.get_previous_heads([self.inv_B, self.inv_D]))
397
self.assertEqual({'D':self.inv_D['fileid']},
398
self.get_previous_heads([self.inv_D, self.inv_B]))
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)
461
class TestExecutable(TestCaseWithTransport):
463
def test_stays_executable(self):
464
a_id = "a-20051208024829-849e76f7968d7a86"
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.
476
wt = wt.bzrdir.open_workingtree()
477
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
479
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
480
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
482
wt.commit('adding a,b', rev_id='r1')
484
rev_tree = b.repository.revision_tree('r1')
485
self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
486
self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
488
self.failUnless(rev_tree.inventory[a_id].executable)
489
self.failIf(rev_tree.inventory[b_id].executable)
491
# Make sure the entries are gone
494
self.failIf(wt.has_id(a_id))
495
self.failIf(wt.has_filename('a'))
496
self.failIf(wt.has_id(b_id))
497
self.failIf(wt.has_filename('b'))
499
# Make sure that revert is able to bring them back,
500
# and sets 'a' back to being executable
502
wt.revert(['a', 'b'], rev_tree, backups=False)
503
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
505
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
506
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
508
# Now remove them again, and make sure that after a
509
# commit, they are still marked correctly
512
wt.commit('removed', rev_id='r2')
514
self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
515
self.failIf(wt.has_id(a_id))
516
self.failIf(wt.has_filename('a'))
517
self.failIf(wt.has_id(b_id))
518
self.failIf(wt.has_filename('b'))
520
# Now revert back to the previous commit
521
wt.revert([], rev_tree, backups=False)
522
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
524
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
525
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
527
# Now make sure that 'bzr branch' also preserves the
529
# TODO: Maybe this should be a blackbox test
530
d2 = b.bzrdir.clone('b2', revision_id='r1')
531
t2 = d2.open_workingtree()
533
self.assertEquals('r1', b2.last_revision())
535
self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
536
self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
537
self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
539
# Make sure pull will delete the files
541
self.assertEquals('r2', b2.last_revision())
542
self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
544
# Now commit the changes on the first branch
545
# so that the second branch can pull the changes
546
# and make sure that the executable bit has been copied
547
wt.commit('resurrected', rev_id='r3')
550
self.assertEquals('r3', b2.last_revision())
551
self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
553
self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
554
self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
557
class TestRevert(TestCaseWithTransport):
559
def test_dangling_id(self):
560
wt = self.make_branch_and_tree('b1')
561
self.assertEqual(len(wt.inventory), 1)
562
open('b1/a', 'wb').write('a test\n')
564
self.assertEqual(len(wt.inventory), 2)
567
self.assertEqual(len(wt.inventory), 1)