1
# Copyright (C) 2007 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
"""Tests for interface conformance of inventories of working trees."""
27
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
30
class TestRevert(TestCaseWithWorkingTree):
32
def test_dangling_id(self):
33
wt = self.make_branch_and_tree('b1')
35
self.addCleanup(wt.unlock)
36
self.assertEqual(len(wt.inventory), 1)
37
open('b1/a', 'wb').write('a test\n')
39
self.assertEqual(len(wt.inventory), 2)
40
wt.flush() # workaround revert doing wt._write_inventory for now.
43
self.assertEqual(len(wt.inventory), 1)
46
class TestSnapshot(TestCaseWithWorkingTree):
49
# for full testing we'll need a branch
50
# with a subdir to test parent changes.
51
# and a file, link and dir under that.
52
# but right now I only need one attribute
53
# to change, and then test merge patterns
54
# with fake parent entries.
55
super(TestSnapshot, self).setUp()
56
self.wt = self.make_branch_and_tree('.')
57
self.branch = self.wt.branch
58
self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
59
self.wt.add(['subdir', 'subdir/file'],
61
self.wt.commit('message_1', rev_id = '1')
62
self.tree_1 = self.branch.repository.revision_tree('1')
63
self.inv_1 = self.branch.repository.get_inventory('1')
64
self.file_1 = self.inv_1['fileid']
66
self.addCleanup(self.wt.unlock)
67
self.file_active = self.wt.inventory['fileid']
68
self.builder = self.branch.get_commit_builder([], timestamp=time.time(), revision_id='2')
70
def test_snapshot_new_revision(self):
71
# This tests that a simple commit with no parents makes a new
72
# revision value in the inventory entry
73
self.file_active.snapshot('2', 'subdir/file', {}, self.wt, self.builder)
74
# expected outcome - file_1 has a revision id of '2', and we can get
75
# its text of 'file contents' out of the weave.
76
self.assertEqual(self.file_1.revision, '1')
77
self.assertEqual(self.file_active.revision, '2')
78
# this should be a separate test probably, but lets check it once..
79
lines = self.branch.repository.weave_store.get_weave(
81
self.branch.repository.get_transaction()).get_lines('2')
82
self.assertEqual(lines, ['contents of subdir/file\n'])
84
def test_snapshot_unchanged(self):
85
#This tests that a simple commit does not make a new entry for
86
# an unchanged inventory entry
87
self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
88
self.wt, self.builder)
89
self.assertEqual(self.file_1.revision, '1')
90
self.assertEqual(self.file_active.revision, '1')
91
vf = self.branch.repository.weave_store.get_weave(
93
self.branch.repository.get_transaction())
94
self.assertRaises(errors.RevisionNotPresent,
98
def test_snapshot_merge_identical_different_revid(self):
99
# This tests that a commit with two identical parents, one of which has
100
# a different revision id, results in a new revision id in the entry.
101
# 1->other, commit a merge of other against 1, results in 2.
102
other_ie = inventory.InventoryFile('fileid', 'newname', self.file_1.parent_id)
103
other_ie = inventory.InventoryFile('fileid', 'file', self.file_1.parent_id)
104
other_ie.revision = '1'
105
other_ie.text_sha1 = self.file_1.text_sha1
106
other_ie.text_size = self.file_1.text_size
107
self.assertEqual(self.file_1, other_ie)
108
other_ie.revision = 'other'
109
self.assertNotEqual(self.file_1, other_ie)
110
versionfile = self.branch.repository.weave_store.get_weave(
111
'fileid', self.branch.repository.get_transaction())
112
versionfile.clone_text('other', '1', ['1'])
113
self.file_active.snapshot('2', 'subdir/file',
114
{'1':self.file_1, 'other':other_ie},
115
self.wt, self.builder)
116
self.assertEqual(self.file_active.revision, '2')
118
def test_snapshot_changed(self):
119
# This tests that a commit with one different parent results in a new
120
# revision id in the entry.
121
self.wt.rename_one('subdir/file', 'subdir/newname')
122
self.file_active = self.wt.inventory['fileid']
123
self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1},
124
self.wt, self.builder)
125
# expected outcome - file_1 has a revision id of '2'
126
self.assertEqual(self.file_active.revision, '2')
129
class TestApplyInventoryDelta(TestCaseWithWorkingTree):
132
wt = self.make_branch_and_tree('.')
134
self.addCleanup(wt.unlock)
135
root_id = wt.get_root_id()
136
wt.apply_inventory_delta([(None, 'bar/foo', 'foo-id',
137
inventory.InventoryFile('foo-id', 'foo', parent_id='bar-id')),
138
(None, 'bar', 'bar-id', inventory.InventoryDirectory('bar-id',
139
'bar', parent_id=root_id))])
140
self.assertEqual('bar/foo', wt.inventory.id2path('foo-id'))
141
self.assertEqual('bar', wt.inventory.id2path('bar-id'))
143
def test_remove(self):
144
wt = self.make_branch_and_tree('.')
146
self.addCleanup(wt.unlock)
147
self.build_tree(['foo/', 'foo/bar'])
148
wt.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
149
wt.apply_inventory_delta([('foo', None, 'foo-id', None),
150
('foo/bar', None, 'bar-id', None)])
151
self.assertIs(None, wt.path2id('foo'))
153
def test_rename_file(self):
154
wt = self.make_branch_and_tree('.')
156
root_id = wt.get_root_id()
157
self.addCleanup(wt.unlock)
158
self.build_tree(['foo/', 'foo/bar', 'baz/'])
159
wt.add(['foo', 'foo/bar', 'baz'],
160
['foo-id', 'bar-id', 'baz-id'])
161
wt.apply_inventory_delta([('foo/bar', 'baz/bar', 'bar-id',
162
inventory.InventoryFile('bar-id', 'bar', 'baz-id'))])
163
self.assertEqual('baz/bar', wt.id2path('bar-id'))
165
def test_rename_swap(self):
166
"""Test the swap-names edge case.
168
foo and bar should swap names, but retain their children. If this
169
works, any simpler rename ought to work.
171
wt = self.make_branch_and_tree('.')
173
root_id = wt.get_root_id()
174
self.addCleanup(wt.unlock)
175
self.build_tree(['foo/', 'foo/bar', 'baz/', 'baz/qux'])
176
wt.add(['foo', 'foo/bar', 'baz', 'baz/qux'],
177
['foo-id', 'bar-id', 'baz-id', 'qux-id'])
178
wt.apply_inventory_delta([('foo', 'baz', 'foo-id',
179
inventory.InventoryDirectory('foo-id', 'baz', root_id)),
180
('baz', 'foo', 'baz-id',
181
inventory.InventoryDirectory('baz-id', 'foo', root_id))])
182
self.assertEqual('baz/bar', wt.id2path('bar-id'))
183
self.assertEqual('foo/qux', wt.id2path('qux-id'))
185
def test_child_rename_ordering(self):
186
"""Test the rename-parent, move child edge case.
188
(A naive implementation may move the parent first, and then be
189
unable to find the child.)
191
wt = self.make_branch_and_tree('.')
192
root_id = wt.get_root_id()
193
self.build_tree(['dir/', 'dir/child', 'other/'])
194
wt.add(['dir', 'dir/child', 'other'],
195
['dir-id', 'child-id', 'other-id'])
196
wt.apply_inventory_delta([('dir', 'dir2', 'dir-id',
197
inventory.InventoryDirectory('dir-id', 'dir2', root_id)),
198
('dir/child', 'other/child', 'child-id',
199
inventory.InventoryFile('child-id', 'child', 'other-id'))])
200
self.assertEqual('dir2', wt.id2path('dir-id'))
201
self.assertEqual('other/child', wt.id2path('child-id'))
203
def test_replace_root(self):
204
wt = self.make_branch_and_tree('.')
206
self.addCleanup(wt.unlock)
208
root_id = wt.get_root_id()
209
wt.apply_inventory_delta([('', None, root_id, None),
210
(None, '', 'root-id',
211
inventory.InventoryDirectory('root-id', '', None))])