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 'WorkingTree.add'"""
27
from bzrlib.workingtree_4 import WorkingTreeFormat4
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
31
class TestAdd(TestCaseWithWorkingTree):
33
def get_tree_layout(self, tree):
34
"""Get the (path, file_id) pairs for the current tree."""
37
return [(path, ie.file_id) for path, ie
38
in tree.iter_entries_by_dir()]
42
def assertTreeLayout(self, expected, tree):
43
"""Check that the tree has the correct layout."""
44
actual = self.get_tree_layout(tree)
45
self.assertEqual(expected, actual)
47
def test_add_one(self):
48
tree = self.make_branch_and_tree('.')
49
self.build_tree(['one'])
50
tree.add('one', 'one-id')
51
root_id = tree.get_root_id()
53
self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
55
def test_add_existing_id(self):
56
"""Adding an entry with a pre-existing id raises DuplicateFileId"""
57
tree = self.make_branch_and_tree('.')
58
self.build_tree(['a', 'b'])
59
tree.add(['a'], ['an-id'])
60
self.assertRaises(errors.DuplicateFileId,
61
tree.add, ['b'], ['an-id'])
62
root_id = tree.get_root_id()
63
# And the entry should not have been added.
64
self.assertTreeLayout([('', root_id), ('a', 'an-id')], tree)
66
def test_add_old_id(self):
67
"""We can add an old id, as long as it doesn't exist now."""
68
tree = self.make_branch_and_tree('.')
69
self.build_tree(['a', 'b'])
70
tree.add(['a'], ['an-id'])
71
tree.commit('first', rev_id='rev-1')
72
root_id = tree.get_root_id()
73
# And the entry should not have been added.
74
tree.unversion(['an-id'])
75
tree.add(['b'], ['an-id'])
76
self.assertTreeLayout([('', root_id), ('b', 'an-id')], tree)
77
self.assertTreeLayout([('', root_id), ('a', 'an-id')],
80
def test_add_one_list(self):
81
tree = self.make_branch_and_tree('.')
82
self.build_tree(['one'])
83
tree.add(['one'], ['one-id'])
84
root_id = tree.get_root_id()
86
self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
88
def test_add_one_new_id(self):
89
tree = self.make_branch_and_tree('.')
90
self.build_tree(['one'])
92
root_id = tree.get_root_id()
93
one_id = tree.path2id('one')
95
self.assertTreeLayout([('', root_id), ('one', one_id)], tree)
97
def test_add_unicode(self):
98
tree = self.make_branch_and_tree('.')
100
self.build_tree([u'f\xf6'])
102
raise tests.TestSkipped('Filesystem does not support filename.')
104
root_id = tree.get_root_id()
105
foo_id = tree.path2id(u'f\xf6')
107
self.assertTreeLayout([('', root_id), (u'f\xf6', foo_id)], tree)
109
def test_add_subdir(self):
110
tree = self.make_branch_and_tree('.')
111
self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
112
tree.add(['dir'], ['dir-id'])
113
tree.add(['dir/subdir'], ['subdir-id'])
114
tree.add(['dir/subdir/foo'], ['foo-id'])
115
root_id = tree.get_root_id()
117
self.assertTreeLayout([('', root_id), ('dir', 'dir-id'),
118
('dir/subdir', 'subdir-id'),
119
('dir/subdir/foo', 'foo-id')], tree)
121
def test_add_multiple(self):
122
tree = self.make_branch_and_tree('.')
123
self.build_tree(['a', 'b', 'dir/', 'dir/subdir/', 'dir/subdir/foo'])
124
tree.add(['a', 'b', 'dir', 'dir/subdir', 'dir/subdir/foo'],
125
['a-id', 'b-id', 'dir-id', 'subdir-id', 'foo-id'])
126
root_id = tree.get_root_id()
128
self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
129
('dir', 'dir-id'), ('dir/subdir', 'subdir-id'),
130
('dir/subdir/foo', 'foo-id')], tree)
132
def test_add_invalid(self):
133
tree = self.make_branch_and_tree('.')
134
self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
135
root_id = tree.get_root_id()
137
self.assertRaises(errors.NotVersionedError,
138
tree.add, ['dir/subdir'])
139
self.assertTreeLayout([('', root_id)], tree)
141
def test_add_after_remove(self):
142
tree = self.make_branch_and_tree('.')
143
self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
144
root_id = tree.get_root_id()
145
tree.add(['dir'], ['dir-id'])
146
tree.commit('dir', rev_id='rev-1')
147
tree.unversion(['dir-id'])
148
self.assertRaises(errors.NotVersionedError,
149
tree.add, ['dir/subdir'])
151
def test_add_root(self):
152
# adding the root should be a no-op, or at least not
153
# do anything whacky.
154
tree = self.make_branch_and_tree('.')
157
self.assertEqual([tree.path2id('')], list(tree))
158
# the root should have been changed to be a new unique root.
159
self.assertNotEqual(inventory.ROOT_ID, tree.path2id(''))