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