~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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
    )
27
from bzrlib.workingtree_4 import WorkingTreeFormat4
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
29
30
31
class TestAdd(TestCaseWithWorkingTree):
32
33
    def get_tree_layout(self, tree):
34
        """Get the (path, file_id) pairs for the current tree."""
35
        tree.lock_read()
36
        try:
37
            return [(path, ie.file_id) for path, ie
38
                    in tree.iter_entries_by_dir()]
39
        finally:
40
            tree.unlock()
41
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)
46
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()
52
53
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
54
2255.7.16 by John Arbash Meinel
Make sure adding a duplicate file_id raises DuplicateFileId.
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)
65
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.
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')],
78
                              tree.basis_tree())
79
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
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()
85
86
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
87
88
    def test_add_one_new_id(self):
89
        tree = self.make_branch_and_tree('.')
90
        self.build_tree(['one'])
91
        tree.add(['one'])
92
        root_id = tree.get_root_id()
93
        one_id = tree.path2id('one')
94
95
        self.assertTreeLayout([('', root_id), ('one', one_id)], tree)
96
97
    def test_add_unicode(self):
98
        tree = self.make_branch_and_tree('.')
99
        try:
100
            self.build_tree([u'f\xf6'])
101
        except UnicodeError:
102
            raise tests.TestSkipped('Filesystem does not support filename.')
103
        tree.add([u'f\xf6'])
104
        root_id = tree.get_root_id()
105
        foo_id = tree.path2id(u'f\xf6')
106
107
        self.assertTreeLayout([('', root_id), (u'f\xf6', foo_id)], tree)
108
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()
116
117
        self.assertTreeLayout([('', root_id), ('dir', 'dir-id'),
118
                               ('dir/subdir', 'subdir-id'),
119
                               ('dir/subdir/foo', 'foo-id')], tree)
120
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()
127
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)
131
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()
136
137
        self.assertRaises(errors.NotVersionedError,
138
                          tree.add, ['dir/subdir'])
139
        self.assertTreeLayout([('', root_id)], tree)
140
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'])
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
150
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('.')
155
        tree.lock_write()
156
        tree.add('')
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(''))
160
        tree.unlock()
2323.4.1 by Robert Collins
dirstate correctness: allow adding paths present in the basis.
161
162
    def test_add_previously_added(self):
163
        # adding a path that was previously added should work
164
        tree = self.make_branch_and_tree('.')
165
        self.build_tree(['foo'])
166
        tree.add(['foo'], ['foo-id'])
167
        tree.unversion(['foo-id'])
168
        tree.add(['foo'], ['foo-id'])
169
        self.assertEqual('foo-id', tree.path2id('foo'))
170
171
    def test_add_present_in_basis(self):
172
        # adding a path that was present in the basis should work.
173
        tree = self.make_branch_and_tree('.')
174
        self.build_tree(['foo'])
175
        tree.add(['foo'], ['foo-id'])
176
        tree.commit('add foo')
177
        tree.unversion(['foo-id'])
178
        tree.add(['foo'], ['foo-id'])
179
        self.assertEqual('foo-id', tree.path2id('foo'))