~bzr-pqm/bzr/bzr.dev

2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
1
# Copyright (C) 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
16
17
"""Tests for interface conformance of 'WorkingTree.add'"""
18
19
from bzrlib import (
20
    errors,
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
21
    inventory,
2255.11.1 by Martin Pool
fix missing import
22
    tests,
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
23
    )
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
24
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
25
26
27
class TestAdd(TestCaseWithWorkingTree):
28
29
    def get_tree_layout(self, tree):
30
        """Get the (path, file_id) pairs for the current tree."""
31
        tree.lock_read()
32
        try:
33
            return [(path, ie.file_id) for path, ie
34
                    in tree.iter_entries_by_dir()]
35
        finally:
36
            tree.unlock()
37
38
    def assertTreeLayout(self, expected, tree):
39
        """Check that the tree has the correct layout."""
40
        actual = self.get_tree_layout(tree)
41
        self.assertEqual(expected, actual)
42
43
    def test_add_one(self):
44
        tree = self.make_branch_and_tree('.')
45
        self.build_tree(['one'])
46
        tree.add('one', 'one-id')
47
        root_id = tree.get_root_id()
48
49
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
50
2255.7.16 by John Arbash Meinel
Make sure adding a duplicate file_id raises DuplicateFileId.
51
    def test_add_existing_id(self):
52
        """Adding an entry with a pre-existing id raises DuplicateFileId"""
53
        tree = self.make_branch_and_tree('.')
54
        self.build_tree(['a', 'b'])
55
        tree.add(['a'], ['an-id'])
56
        self.assertRaises(errors.DuplicateFileId,
57
                          tree.add, ['b'], ['an-id'])
58
        root_id = tree.get_root_id()
59
        # And the entry should not have been added.
60
        self.assertTreeLayout([('', root_id), ('a', 'an-id')], tree)
61
2255.7.17 by John Arbash Meinel
Add another test that makes sure we can add as long as the file_id isn't current.
62
    def test_add_old_id(self):
63
        """We can add an old id, as long as it doesn't exist now."""
64
        tree = self.make_branch_and_tree('.')
65
        self.build_tree(['a', 'b'])
66
        tree.add(['a'], ['an-id'])
67
        tree.commit('first', rev_id='rev-1')
68
        root_id = tree.get_root_id()
69
        # And the entry should not have been added.
70
        tree.unversion(['an-id'])
71
        tree.add(['b'], ['an-id'])
72
        self.assertTreeLayout([('', root_id), ('b', 'an-id')], tree)
73
        self.assertTreeLayout([('', root_id), ('a', 'an-id')],
74
                              tree.basis_tree())
75
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
76
    def test_add_one_list(self):
77
        tree = self.make_branch_and_tree('.')
78
        self.build_tree(['one'])
79
        tree.add(['one'], ['one-id'])
80
        root_id = tree.get_root_id()
81
82
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
83
84
    def test_add_one_new_id(self):
85
        tree = self.make_branch_and_tree('.')
86
        self.build_tree(['one'])
87
        tree.add(['one'])
88
        root_id = tree.get_root_id()
89
        one_id = tree.path2id('one')
90
91
        self.assertTreeLayout([('', root_id), ('one', one_id)], tree)
92
93
    def test_add_unicode(self):
94
        tree = self.make_branch_and_tree('.')
95
        try:
96
            self.build_tree([u'f\xf6'])
97
        except UnicodeError:
98
            raise tests.TestSkipped('Filesystem does not support filename.')
99
        tree.add([u'f\xf6'])
100
        root_id = tree.get_root_id()
101
        foo_id = tree.path2id(u'f\xf6')
102
103
        self.assertTreeLayout([('', root_id), (u'f\xf6', foo_id)], tree)
104
105
    def test_add_subdir(self):
106
        tree = self.make_branch_and_tree('.')
107
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
108
        tree.add(['dir'], ['dir-id'])
109
        tree.add(['dir/subdir'], ['subdir-id'])
110
        tree.add(['dir/subdir/foo'], ['foo-id'])
111
        root_id = tree.get_root_id()
112
113
        self.assertTreeLayout([('', root_id), ('dir', 'dir-id'),
114
                               ('dir/subdir', 'subdir-id'),
115
                               ('dir/subdir/foo', 'foo-id')], tree)
116
117
    def test_add_multiple(self):
118
        tree = self.make_branch_and_tree('.')
119
        self.build_tree(['a', 'b', 'dir/', 'dir/subdir/', 'dir/subdir/foo'])
120
        tree.add(['a', 'b', 'dir', 'dir/subdir', 'dir/subdir/foo'],
121
                 ['a-id', 'b-id', 'dir-id', 'subdir-id', 'foo-id'])
122
        root_id = tree.get_root_id()
123
124
        self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
125
                               ('dir', 'dir-id'), ('dir/subdir', 'subdir-id'),
126
                               ('dir/subdir/foo', 'foo-id')], tree)
127
128
    def test_add_invalid(self):
129
        tree = self.make_branch_and_tree('.')
130
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
131
        root_id = tree.get_root_id()
132
133
        self.assertRaises(errors.NotVersionedError,
134
                          tree.add, ['dir/subdir'])
135
        self.assertTreeLayout([('', root_id)], tree)
136
137
    def test_add_after_remove(self):
138
        tree = self.make_branch_and_tree('.')
139
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
140
        root_id = tree.get_root_id()
141
        tree.add(['dir'], ['dir-id'])
142
        tree.commit('dir', rev_id='rev-1')
143
        tree.unversion(['dir-id'])
144
        self.assertRaises(errors.NotVersionedError,
145
                          tree.add, ['dir/subdir'])
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
146
147
    def test_add_root(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
148
        # adding the root should be a no-op, or at least not
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
149
        # do anything whacky.
150
        tree = self.make_branch_and_tree('.')
151
        tree.lock_write()
152
        tree.add('')
5837.2.2 by Jelmer Vernooij
Fix more uses of Tree.__iter__
153
        self.assertEqual([tree.path2id('')], list(tree.all_file_ids()))
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
154
        # the root should have been changed to be a new unique root.
155
        self.assertNotEqual(inventory.ROOT_ID, tree.path2id(''))
156
        tree.unlock()
2323.4.1 by Robert Collins
dirstate correctness: allow adding paths present in the basis.
157
158
    def test_add_previously_added(self):
159
        # adding a path that was previously added should work
160
        tree = self.make_branch_and_tree('.')
161
        self.build_tree(['foo'])
162
        tree.add(['foo'], ['foo-id'])
163
        tree.unversion(['foo-id'])
164
        tree.add(['foo'], ['foo-id'])
165
        self.assertEqual('foo-id', tree.path2id('foo'))
166
167
    def test_add_present_in_basis(self):
168
        # adding a path that was present in the basis should work.
169
        tree = self.make_branch_and_tree('.')
170
        self.build_tree(['foo'])
171
        tree.add(['foo'], ['foo-id'])
172
        tree.commit('add foo')
173
        tree.unversion(['foo-id'])
174
        tree.add(['foo'], ['foo-id'])
175
        self.assertEqual('foo-id', tree.path2id('foo'))