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
24
import bzrlib.inventory as inventory
25
from bzrlib.osutils import has_symlinks, rename, pathjoin
26
from bzrlib.tests import TestCase, TestCaseWithTransport
29
class TestInventory(TestCase):
31
def test_is_within(self):
32
from bzrlib.osutils import is_inside_any
34
SRC_FOO_C = pathjoin('src', 'foo.c')
35
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
39
self.assert_(is_inside_any(dirs, fn))
41
for dirs, fn in [(['src'], 'srccontrol'),
42
(['src'], 'srccontrol/foo')]:
43
self.assertFalse(is_inside_any(dirs, fn))
46
"""Test detection of files within selected directories."""
49
for args in [('src', 'directory', 'src-id'),
50
('doc', 'directory', 'doc-id'),
51
('src/hello.c', 'file'),
52
('src/bye.c', 'file', 'bye-id'),
53
('Makefile', 'file')]:
56
self.assertEqual(inv.path2id('src'), 'src-id')
57
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
59
self.assert_('src-id' in inv)
62
def test_version(self):
63
"""Inventory remembers the text's version."""
65
ie = inv.add_path('foo.txt', 'file')
69
class TestInventoryEntry(TestCase):
71
def test_file_kind_character(self):
72
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
73
self.assertEqual(file.kind_character(), '')
75
def test_dir_kind_character(self):
76
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
77
self.assertEqual(dir.kind_character(), '/')
79
def test_link_kind_character(self):
80
dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
81
self.assertEqual(dir.kind_character(), '')
83
def test_dir_detect_changes(self):
84
left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
86
left.executable = True
87
left.symlink_target='foo'
88
right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
90
right.symlink_target='bar'
91
self.assertEqual((False, False), left.detect_changes(right))
92
self.assertEqual((False, False), right.detect_changes(left))
94
def test_file_detect_changes(self):
95
left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
97
right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
99
self.assertEqual((False, False), left.detect_changes(right))
100
self.assertEqual((False, False), right.detect_changes(left))
101
left.executable = True
102
self.assertEqual((False, True), left.detect_changes(right))
103
self.assertEqual((False, True), right.detect_changes(left))
104
right.text_sha1 = 321
105
self.assertEqual((True, True), left.detect_changes(right))
106
self.assertEqual((True, True), right.detect_changes(left))
108
def test_symlink_detect_changes(self):
109
left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
111
left.executable = True
112
left.symlink_target='foo'
113
right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
114
right.text_sha1 = 321
115
right.symlink_target='foo'
116
self.assertEqual((False, False), left.detect_changes(right))
117
self.assertEqual((False, False), right.detect_changes(left))
118
left.symlink_target = 'different'
119
self.assertEqual((True, False), left.detect_changes(right))
120
self.assertEqual((True, False), right.detect_changes(left))
122
def test_file_has_text(self):
123
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
124
self.failUnless(file.has_text())
126
def test_directory_has_text(self):
127
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
128
self.failIf(dir.has_text())
130
def test_link_has_text(self):
131
link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
132
self.failIf(link.has_text())
135
class TestEntryDiffing(TestCaseWithTransport):
138
super(TestEntryDiffing, self).setUp()
139
self.wt = self.make_branch_and_tree('.')
140
self.branch = self.wt.branch
141
print >> open('file', 'wb'), 'foo'
142
self.wt.add(['file'], ['fileid'])
144
os.symlink('target1', 'symlink')
145
self.wt.add(['symlink'], ['linkid'])
146
self.wt.commit('message_1', rev_id = '1')
147
print >> open('file', 'wb'), 'bar'
150
os.symlink('target2', 'symlink')
151
self.tree_1 = self.branch.repository.revision_tree('1')
152
self.inv_1 = self.branch.repository.get_inventory('1')
153
self.file_1 = self.inv_1['fileid']
154
self.tree_2 = self.wt
155
self.inv_2 = self.tree_2.read_working_inventory()
156
self.file_2 = self.inv_2['fileid']
158
self.link_1 = self.inv_1['linkid']
159
self.link_2 = self.inv_2['linkid']
161
def test_file_diff_deleted(self):
163
self.file_1.diff(internal_diff,
164
"old_label", self.tree_1,
165
"/dev/null", None, None,
167
self.assertEqual(output.getvalue(), "--- old_label\t\n"
173
def test_file_diff_added(self):
175
self.file_1.diff(internal_diff,
176
"new_label", self.tree_1,
177
"/dev/null", None, None,
178
output, reverse=True)
179
self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
185
def test_file_diff_changed(self):
187
self.file_1.diff(internal_diff,
188
"/dev/null", self.tree_1,
189
"new_label", self.file_2, self.tree_2,
191
self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
198
def test_link_diff_deleted(self):
199
if not has_symlinks():
202
self.link_1.diff(internal_diff,
203
"old_label", self.tree_1,
204
"/dev/null", None, None,
206
self.assertEqual(output.getvalue(),
207
"=== target was 'target1'\n")
209
def test_link_diff_added(self):
210
if not has_symlinks():
213
self.link_1.diff(internal_diff,
214
"new_label", self.tree_1,
215
"/dev/null", None, None,
216
output, reverse=True)
217
self.assertEqual(output.getvalue(),
218
"=== target is 'target1'\n")
220
def test_link_diff_changed(self):
221
if not has_symlinks():
224
self.link_1.diff(internal_diff,
225
"/dev/null", self.tree_1,
226
"new_label", self.link_2, self.tree_2,
228
self.assertEqual(output.getvalue(),
229
"=== target changed 'target1' => 'target2'\n")
232
class TestSnapshot(TestCaseWithTransport):
235
# for full testing we'll need a branch
236
# with a subdir to test parent changes.
237
# and a file, link and dir under that.
238
# but right now I only need one attribute
239
# to change, and then test merge patterns
240
# with fake parent entries.
241
super(TestSnapshot, self).setUp()
242
self.wt = self.make_branch_and_tree('.')
243
self.branch = self.wt.branch
244
self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
245
self.wt.add(['subdir', 'subdir/file'],
249
self.wt.commit('message_1', rev_id = '1')
250
self.tree_1 = self.branch.repository.revision_tree('1')
251
self.inv_1 = self.branch.repository.get_inventory('1')
252
self.file_1 = self.inv_1['fileid']
253
self.file_active = self.wt.inventory['fileid']
255
def test_snapshot_new_revision(self):
256
# This tests that a simple commit with no parents makes a new
257
# revision value in the inventory entry
258
self.file_active.snapshot('2', 'subdir/file', {}, self.wt,
259
self.branch.repository.weave_store,
260
self.branch.get_transaction())
261
# expected outcome - file_1 has a revision id of '2', and we can get
262
# its text of 'file contents' out of the weave.
263
self.assertEqual(self.file_1.revision, '1')
264
self.assertEqual(self.file_active.revision, '2')
265
# this should be a separate test probably, but lets check it once..
266
lines = self.branch.repository.weave_store.get_weave(
268
self.branch.get_transaction()).get_lines('2')
269
self.assertEqual(lines, ['contents of subdir/file\n'])
271
def test_snapshot_unchanged(self):
272
#This tests that a simple commit does not make a new entry for
273
# an unchanged inventory entry
274
self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
276
self.branch.repository.weave_store,
277
self.branch.get_transaction())
278
self.assertEqual(self.file_1.revision, '1')
279
self.assertEqual(self.file_active.revision, '1')
280
self.assertRaises(errors.RevisionNotPresent,
281
self.branch.repository.weave_store.get_lines,
282
'fileid', '2', self.branch.get_transaction())
284
def test_snapshot_merge_identical_different_revid(self):
285
# This tests that a commit with two identical parents, one of which has
286
# a different revision id, results in a new revision id in the entry.
287
# 1->other, commit a merge of other against 1, results in 2.
288
other_ie = inventory.InventoryFile('fileid', 'newname', self.file_1.parent_id)
289
other_ie = inventory.InventoryFile('fileid', 'file', self.file_1.parent_id)
290
other_ie.revision = '1'
291
other_ie.text_sha1 = self.file_1.text_sha1
292
other_ie.text_size = self.file_1.text_size
293
self.assertEqual(self.file_1, other_ie)
294
other_ie.revision = 'other'
295
self.assertNotEqual(self.file_1, other_ie)
296
versionfile = self.branch.repository.weave_store.get_weave(
297
'fileid', self.branch.repository.get_transaction())
298
versionfile.clone_text('other', '1', ['1'])
299
self.file_active.snapshot('2', 'subdir/file',
300
{'1':self.file_1, 'other':other_ie},
302
self.branch.repository.weave_store,
303
self.branch.get_transaction())
304
self.assertEqual(self.file_active.revision, '2')
306
def test_snapshot_changed(self):
307
# This tests that a commit with one different parent results in a new
308
# revision id in the entry.
309
self.file_active.name='newname'
310
rename('subdir/file', 'subdir/newname')
311
self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1},
313
self.branch.repository.weave_store,
314
self.branch.get_transaction())
315
# expected outcome - file_1 has a revision id of '2'
316
self.assertEqual(self.file_active.revision, '2')
319
class TestPreviousHeads(TestCaseWithTransport):
322
# we want several inventories, that respectively
323
# give use the following scenarios:
324
# A) fileid not in any inventory (A),
325
# B) fileid present in one inventory (B) and (A,B)
326
# C) fileid present in two inventories, and they
327
# are not mutual descendents (B, C)
328
# D) fileid present in two inventories and one is
329
# a descendent of the other. (B, D)
330
super(TestPreviousHeads, self).setUp()
331
self.wt = self.make_branch_and_tree('.')
332
self.branch = self.wt.branch
333
self.build_tree(['file'])
334
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
335
self.inv_A = self.branch.repository.get_inventory('A')
336
self.wt.add(['file'], ['fileid'])
337
self.wt.commit('add file', rev_id='B')
338
self.inv_B = self.branch.repository.get_inventory('B')
339
self.branch.lock_write()
341
self.branch.control_files.put_utf8('revision-history', 'A\n')
344
self.assertEqual(self.branch.revision_history(), ['A'])
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.get_transaction())
354
def get_previous_heads(self, inventories):
355
return self.file_active.find_previous_heads(inventories, self.weave)
357
def test_fileid_in_no_inventory(self):
358
self.assertEqual({}, self.get_previous_heads([self.inv_A]))
360
def test_fileid_in_one_inventory(self):
361
self.assertEqual({'B':self.inv_B['fileid']},
362
self.get_previous_heads([self.inv_B]))
363
self.assertEqual({'B':self.inv_B['fileid']},
364
self.get_previous_heads([self.inv_A, self.inv_B]))
365
self.assertEqual({'B':self.inv_B['fileid']},
366
self.get_previous_heads([self.inv_B, self.inv_A]))
368
def test_fileid_in_two_inventories_gives_both_entries(self):
369
self.assertEqual({'B':self.inv_B['fileid'],
370
'C':self.inv_C['fileid']},
371
self.get_previous_heads([self.inv_B, self.inv_C]))
372
self.assertEqual({'B':self.inv_B['fileid'],
373
'C':self.inv_C['fileid']},
374
self.get_previous_heads([self.inv_C, self.inv_B]))
376
def test_fileid_in_two_inventories_already_merged_gives_head(self):
377
self.assertEqual({'D':self.inv_D['fileid']},
378
self.get_previous_heads([self.inv_B, self.inv_D]))
379
self.assertEqual({'D':self.inv_D['fileid']},
380
self.get_previous_heads([self.inv_D, self.inv_B]))
382
# TODO: test two inventories with the same file revision
385
class TestExecutable(TestCaseWithTransport):
387
def test_stays_executable(self):
388
basic_inv = """<inventory format="5">
389
<file file_id="a-20051208024829-849e76f7968d7a86" name="a" executable="yes" />
390
<file file_id="b-20051208024829-849e76f7968d7a86" name="b" />
393
wt = self.make_branch_and_tree('b1')
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
# Manually writing the inventory, to ensure that
400
# the executable="yes" entry is set for 'a' and not for 'b'
401
open('b1/.bzr/inventory', 'wb').write(basic_inv)
403
a_id = "a-20051208024829-849e76f7968d7a86"
404
b_id = "b-20051208024829-849e76f7968d7a86"
405
wt = wt.bzrdir.open_workingtree()
406
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
408
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
409
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
411
wt.commit('adding a,b', rev_id='r1')
413
rev_tree = b.repository.revision_tree('r1')
414
self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
415
self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
417
self.failUnless(rev_tree.inventory[a_id].executable)
418
self.failIf(rev_tree.inventory[b_id].executable)
420
# Make sure the entries are gone
423
self.failIf(wt.has_id(a_id))
424
self.failIf(wt.has_filename('a'))
425
self.failIf(wt.has_id(b_id))
426
self.failIf(wt.has_filename('b'))
428
# Make sure that revert is able to bring them back,
429
# and sets 'a' back to being executable
431
wt.revert(['a', 'b'], rev_tree, backups=False)
432
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
434
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
435
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
437
# Now remove them again, and make sure that after a
438
# commit, they are still marked correctly
441
wt.commit('removed', rev_id='r2')
443
self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
444
self.failIf(wt.has_id(a_id))
445
self.failIf(wt.has_filename('a'))
446
self.failIf(wt.has_id(b_id))
447
self.failIf(wt.has_filename('b'))
449
# Now revert back to the previous commit
450
wt.revert([], rev_tree, backups=False)
451
self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
453
self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
454
self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
456
# Now make sure that 'bzr branch' also preserves the
458
# TODO: Maybe this should be a blackbox test
459
d2 = b.bzrdir.clone('b2', revision_id='r1')
460
t2 = d2.open_workingtree()
462
self.assertEquals('r1', b2.last_revision())
464
self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
465
self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
466
self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
468
# Make sure pull will delete the files
470
self.assertEquals('r2', b2.last_revision())
471
self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
473
# Now commit the changes on the first branch
474
# so that the second branch can pull the changes
475
# and make sure that the executable bit has been copied
476
wt.commit('resurrected', rev_id='r3')
479
self.assertEquals('r3', b2.last_revision())
480
self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
482
self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
483
self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
485
class TestRevert(TestCaseWithTransport):
486
def test_dangling_id(self):
487
wt = self.make_branch_and_tree('b1')
488
self.assertEqual(len(wt.inventory), 1)
489
open('b1/a', 'wb').write('a test\n')
491
self.assertEqual(len(wt.inventory), 2)
494
self.assertEqual(len(wt.inventory), 1)